Nested variables within strings.

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:

Nested variables within strings.

#1 Post by Onishion »

I've been using a method for selecting random lines within a dialog, and it mostly works great, but I hit a snag when trying to use a variable within that dialog, in that basically it does not use the variable and instead just says the variable name. Here's what I'm working with in simple form:

Code: Select all

$ Weapon = "sword"
$ Enemy = "slime"
$ Line = renpy.random.choice(["You swing your [Weapon].",                 
    "You leap over the [Enemy]'s attack and bring your [Weapon] down on its head.",
    "You dodge to the side and then stab the enemy."])  

"[Line]"
Now I use it in a more complex fashion later, but not in a way that should change anything. Anyways, the intended behavior is obviously that if option 2 gets chosen, when it gets to "[Line]" it will say "You leap over the slime's attack and bring your sword down on its head.", instead it says "You leap over the [Enemy]'s attack and bring your [Weapon] down on its head." I'm guessing it's because when I reference a variable in a string, that is then added into a variable, it has trouble referencing those variables? Like maybe it doesn't parse what the actual variable is when I assign the Line, but rather when it's displayed or something? Is there some simple workaround for this? If not I can just rewrite the portions that don't work, but I could make some more interesting narrative if I can make this work.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Nested variables within strings.

#2 Post by trooper6 »

You can do this two different ways.
Option 1, with concatenation:

Code: Select all

    $ Line = renpy.random.choice(["You swing your "+Weapon+".",                 
    "You leap over the "+Enemy+"'s attack and bring your "+Weapon+" down on its head.",
    "You dodge to the side and then stab the enemy."]) 
Option 2, with formatting options:

Code: Select all

    $ Line = renpy.random.choice(["You swing your {0}.".format(Weapon),                 
    "You leap over the {0}'s attack and bring your {1} down on its head.".format(Enemy, Weapon),
    "You dodge to the side and then stab the enemy."])  
My understanding is that Option 2 is faster than Option 1.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

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

Re: Nested variables within strings.

#3 Post by Onishion »

Huh. I've never seen anything like either of those options. ;) I'll give them a shot though, thanks. Are both of those "baked in" at the point that I write that, or are they computed later in the process? Like if I did:

Code: Select all

 $ Line = renpy.random.choice(["You swing your {0}.".format(Weapon),                 
    "You leap over the {0}'s attack and bring your {1} down on its head.".format(Enemy, Weapon),
    "You dodge to the side and then stab the enemy."])  

 $ Line = Line + renpy.random.choice([" It hits.",                 
    " It misses."])  

"[Line]"
Would that properly add the two sentences together, or would it leave the variables broken?

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Nested variables within strings.

#4 Post by trooper6 »

Have you taken the basic Python course from codeacademy.com? If you haven't, you should. It includes information on strings and how to play with them.
You can further concatenate the variable. It works.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Ocelot