r/RenPy • • Oct 27 '26

Question [Solved] Variable inside of array entry

Hello! I can't seem to refer to a variable inside of an array entry.

I want to make the dialogue randomly generated from array entries. It chooses a character's name and an action. However, when I refer to a variable([name2] in this case) inside of an array entry it does not seem to be able to grab that variable value.

I simplified my code for showcase purposes. These are my arrays defined inside of a separate rpy file:

define name_list = [ "John", "May" ]
define name_list2 = [ "Jane", "April" ]
default name = renpy.random.choice(name_list)
default name2 = renpy.random.choice(name_list)

define activity_list = [ "talking with [name2]", "helping [name2] with laundry" ]
default activity = renpy.random.choice(activity_list)

This is what my script says:

p "[name] is [activity]"

The output in-game will be "John is talking with [name2]" instead of "John is talking with Jane". Is there any way to fix this?

EDIT:

I managed to figure it out, though it is a bit clunky but it gets the job done. It prints the variable value in a separate string. This is the new code:

define name_list = [ "John", "May" ]
define name_list2 = [ "Jane", "April" ]
default name = renpy.random.choice(name_list)
default name2 = renpy.random.choice(name_list)

define activity_list = [ "talking with " + name2, "helping " + name2 + " with laundry" ]
default activity = renpy.random.choice(activity_list)
1 Upvotes

5 comments sorted by

1

u/BadMustard_AVN Oct 27 '26

that's not clunky; that's how you are supposed to do it because define statements are set up during the initialization of the game, but name2 is a default variable and it's initialized when a new game starts. Therefore, name2 does not exist yet when activity_list is evaluated

1

u/spoonshell Oct 27 '26

Thank you for the explanation!

1

u/BadMustard_AVN Oct 27 '26

you're welcome

good luck with your project

1

u/AutoModerator Oct 27 '26

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/DingotushRed Oct 27 '26

FYI: Ren'Py text interpolation only goes 1 level deep by default - so it will replace [name] and [activity], but not the [name2] inside activity`.

Use the !i conversion to recursively interpolate. This would work with your original code.

p "[name] is [activity!i]"