Array in ren'py acting weird

Hi! I’m working on a game but I keep getting a specific error I can’t seem to get around.

I have an array of articles that the player can find in today’s news, and I used

python:

import random

articles = renpy.random.sample(news_articles, 3)

“It takes a while to read today’s news. the articles include [articles]”

to get them to appear to the player. However, all the array terms appear in square brackets [] and with u’ before them. How can I fix this?

It looks like you’re trying to print an unformatted array, which isn’t likely to produce readable results on its own. An array is more or less just a collection of objects, and the engine likely isn’t set up to default to printing an array’s contents as a nice, comma-separated plain-text string.

Short of figuring out how to format that array in a readable manner, what you could try to do - since the array will, apparently, always contain three headlines - is to write the line like this:

“It takes a while to read today’s news. The articles include [articles[0]], [articles[1]], and [articles[2]].”

You can spruce it up a bit by adding extra quotations, italics, etc. around the headlines, but otherwise I believe that’s syntax that Ren’Py will accept.

The original array looks like this:

define news_articles = [“A local town elected their new mayor.”, “The economy has been improving.”, “The Board Of Education allocated new funding to schools.”,
“Ghost hunter fell to their death in investigation gone wrong.”, “President loses a fight with an umbrella.”, “Astronaut reveals that UFOs are in fact real.”,
“New research shows humanity has even less time to stop climate change.”, “Another politician gets away with sexual harassment.”, “Wildfires in California.”,
“Football team handles ball well, wins match.”]

The idea is to take three random ones each time, rather than just print the first three. Any idea how I can do that?

Sorry, I should’ve been more clear with my example; I only meant for it to be a substitute for the printed line, not the entire block. Your initial code is correct as far as creating an array ‘news_articles’ with all the articles, and then populating a second array ‘articles’ with a random selection from the first. The problem only occurs when you try to print ‘articles’ as-is, because Ren’Py’s idea of printing an array doesn’t necessarily match what the user would expect the output to look like.

A full example would be closer to this:

python:
    import random
    articles = renpy.random.sample(news_articles, 3)

“It takes a while to read today’s news. The articles include [articles[0]], [articles[1]], and [articles[2]].”

In this case, ‘articles’ is still a selection of three random items from the larger news_articles array. But since printing it with [articles] doesn’t work the way we want it to, we directly print the individual items in the ‘articles’ array instead. It’s not the most elegant solution, but unless you’re going to try to directly print a lot of different arrays into dialogue boxes, it’s a much more feasible solution than overriding how arrays are printed.

1 Like

Why are you importing random? If your using the built in renpy random number generator then you don’t need to import anything. And if you are not using renpys built in random number generator be aware it wont work across multiple rollbacks.

All you really need would be:

$ articles = renpy.random.sample(news_articles, 3)
“It takes a while to read today’s news. The articles include [articles[0]], [articles[1]], and [articles[2]].”
1 Like

Ohhh, I see. That makes sense, thank you!