[Solved] "this is [x]" fully when x= "a bag of [fruit]"

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
wjannsz
Newbie
Posts: 10
Joined: Sat Jul 14, 2012 7:33 am
Contact:

[Solved] "this is [x]" fully when x= "a bag of [fruit]"

#1 Post by wjannsz »

Hi All,
Please excuse my terminology if I happen to name the wrong things :)
I learnt that you can embed strings in texts using []
Eg.

Code: Select all

fruit = "apples"
e "I really like [fruit]" #goes into I really like apples.
so the question is if I could display a variable in a renpy text line when it contains another variable.
like

Code: Select all

$ fruit = "apples" # or inputted dynamically
$ basket = "a bag of [fruit]"
e "this is [basket]"
and it gives me "this is a bag of apples."

How else could I approach to achieve similar results so I can call variables in variables and print them out in a text line to show? At the moment, the line will show "this is a bag of [fruit], because it doesn't recognise past the first layer of strings. :(

Furthermore, to explore this function further, I'm wondering if I could make the fruit variable a list

Code: Select all

fruitlist = [apples, bananas, coconut]
fruit = fruitlist[1]
e "I really like [fruit]" #will display as I really like bananas
or making a nested list

Code: Select all

foodlist = [(bananas, apples), (soup, bread)]
fruit=foodlist[0][0] #picks the fruit tuple in foodlist
main = foodlist[1][0] # picks the main meal tuple in foodlist.
e "I usually have [fruit] after [main]."

I'm not sure if I'm putting my thoughts in the right direction. Please help shed some light in this area :D many thanks.
Last edited by wjannsz on Sun Feb 09, 2014 9:10 pm, edited 1 time in total.

KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: displaying "this is [x]" fully when x= "a bag of [fruit]

#2 Post by KimiYoriBaka »

Code: Select all

$ fruit = "apples" # or inputted dynamically
$ basket = "a bag of [fruit]"
e "this is [basket]"
strings used in say statements aren't always treated the same way as strings in python expression. I'm actually surprised your code didn't work anyway, since I thought the embedding of strings in renpy was based on python, but it might be a different version of python or just a shallow implementation.

anyway, this is the more universal way to do it:

Code: Select all

$ fruit = "apples" # or inputted dynamically
$ basket = "a bag of " + fruit
e "this is [basket]"
note that if you want to add a variable that isn't a string, it needs to be converted using str().

regarding using the lists, what you've got should work, as long as you use lists of strings and python statements:

Code: Select all

$ fruitlist = ["apples", "bananas", "coconut"]
$ fruit = fruitlist[1]
e "I really like [fruit]" #will display as I really like bananas

wjannsz
Newbie
Posts: 10
Joined: Sat Jul 14, 2012 7:33 am
Contact:

Re: displaying "this is [x]" fully when x= "a bag of [fruit]

#3 Post by wjannsz »

thank you, KimiYoriBaka!
this is exactly what I wanted and it works perfectly :DDD
good day to you too!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]