[SOLVED] Assign numbered variables

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
User avatar
Bruni Multimedia
Regular
Posts: 89
Joined: Mon May 15, 2017 12:23 pm
Projects: General Practitioner, The Coroner Saga
Organization: Bruni Multimedia
itch: brunimultimedia
Contact:

[SOLVED] Assign numbered variables

#1 Post by Bruni Multimedia » Wed Sep 05, 2018 5:53 am

Hi.
Let's say I have the following variables:

Code: Select all

goofy_1_charm = 0
goofy_2_charm = 0
goofy_3_charm = 0
donald_1_charm = 0
donald_2_charm = 0
donald_3_charm = 0
And I want to use a counter to assign a value to them dinamically:

Code: Select all

$ counter = 0
I tried:

Code: Select all

if cond_1 == True:
        $ counter += 1
        $ eval("goofy"+str(counter)+"_charm") = value
        $ eval("donald"+str(counter)+"_charm") = value
if cond_2 == True:
	$ counter += 1        
        $ eval("goofy"+str(counter)+"_charm") = value
        $ eval("donald"+str(counter)+"_charm") = value        

However it looks like I can safely use "eval" as a value but I cannot assign to a function call.

Any idea how to work around this?
Last edited by Bruni Multimedia on Fri Sep 07, 2018 8:07 am, edited 1 time in total.

User avatar
MaydohMaydoh
Regular
Posts: 101
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Assign numbered variables

#2 Post by MaydohMaydoh » Wed Sep 05, 2018 8:05 am

You would probably be better using a dictionary.

Code: Select all

goofy = { "charm_1" : 0, "charm_2" : 0, "charm_3" : 0 }

if cond_1:
    $ counter += 1
    $ goofy["charm_" + str(counter)] = value
or even a list would do,

Code: Select all

goofy_charm = [0, 0, 0]

if cond_1:
    $ counter += 1
    $ goofy_charm[counter] = value
And remember the first item in the list has an index of 0 not 1. But in your case a dictionary would probably be better as you can easily add more values.

User avatar
Bruni Multimedia
Regular
Posts: 89
Joined: Mon May 15, 2017 12:23 pm
Projects: General Practitioner, The Coroner Saga
Organization: Bruni Multimedia
itch: brunimultimedia
Contact:

Re: Assign numbered variables

#3 Post by Bruni Multimedia » Wed Sep 05, 2018 12:53 pm

Awesome! I shall try that asap.

I wasn't aware of this dictionary thing, it will help me a lot with many future features :)

User avatar
Bruni Multimedia
Regular
Posts: 89
Joined: Mon May 15, 2017 12:23 pm
Projects: General Practitioner, The Coroner Saga
Organization: Bruni Multimedia
itch: brunimultimedia
Contact:

Re: Assign numbered variables

#4 Post by Bruni Multimedia » Fri Sep 07, 2018 8:07 am

Worked like a charm, thank you very much!

Post Reply

Who is online

Users browsing this forum: No registered users