Using a List as a dict value in dialogue

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Using a List as a dict value in dialogue

#1 Post by Onishion » Wed Oct 26, 2022 7:56 pm

I'm trying to make a dict that uses lists as the values, and then reference that list within a dialogue string. The closest I've gotten allows me to create the dict and access it using a fixed number in the string, but trying to use a variable to select the position in the list breaks it. I have a few less elegant workarounds, but if I could actually get this to work as intended, it would be much cleaner.

$ Directions = {'up':['up','down'],'left':['left','right']}
$ Invert = 0
"We're going [Directions[up][0]]!" #works, says "We're going up!"
"We're going [Directions[up][1]]!" #works, says "We're going down!"
"We're going [Directions[up][Invert]]!" #fails, regardless of what "Invert" is set to, says "list indecies must be integers, not unicode"

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Using a List as a dict value in dialogue

#2 Post by philat » Wed Oct 26, 2022 9:57 pm

It's a limitation of square bracket interpolation - you should use renpy.say to use python .format interpolation.

Code: Select all

$renpy.say(None, "We're going {}!".format(Directions["up"][Invert]) #also do note up should be in quotes
That said, not sure this is worth the effort given the simplicity of the variables in this specific case.

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: Using a List as a dict value in dialogue

#3 Post by Onishion » Wed Oct 26, 2022 10:26 pm

Ok, thanks. I'll see how this fits in with the rest.

Post Reply

Who is online

Users browsing this forum: Google [Bot]