[SOLVED]Variable as a part of another variable's name?

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
Adabelitoo
Regular
Posts: 91
Joined: Sat Apr 13, 2019 2:32 pm
Contact:

[SOLVED]Variable as a part of another variable's name?

#1 Post by Adabelitoo »

I was looking for that and I found this post with the same question, but I didn't understand exactly how that works.

viewtopic.php?t=65398

This is my situation, I'd like to adapt and understand it applied to this.

Code: Select all

default rp_linda = 0
default rp_bob = 0
default rp_rose = 0
default rp_max = 0

defaul num_random = 0
default cha_name = " "

label question:
    Linda "Puppies are cute! Right?"
    menu:
        "Yes!":
            $ rp_linda += 1
            $ rp_bob += 1
        "No!":
            $ cha_name = "linda"
            call 1in4chance
            $ cha_name = "bob"
            call 1in4chance

label 1in4chance:
    $ num_random = renpy.random.randint(1, 4)
    if num_random == 4:
        pass
    else:
        $ rp_[cha_name] -= 1		#which ends in rp_linda and then rp_bob but can be called later for max and rose if necessary
            
    return
Thanks for reading.
Last edited by Adabelitoo on Sat May 11, 2024 3:51 am, edited 1 time in total.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1009
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Variable as a part of another variable's name?

#2 Post by m_from_space »

Adabelitoo wrote: Wed May 08, 2024 5:35 pm I was looking for that and I found this post with the same question, but I didn't understand exactly how that works.
When you create a variable in Renpy script language using the keyword "default", Renpy will then create an attribute of that name inside its "store" object.

So for example if you do this:

Code: Select all

default rp_linda = 0
then this variable is also available via store.rp_linda.

Now in Python there is a method to access attributes of objects via their name (as a string) using the "getattr" and "setattr" functions. So as an example:

Code: Select all

# two ways of changing the variables contents inside of the store:

# this is the normal way, but we cannot use the name dynamically
$ store.rp_linda = 1

# here we just pass the variable name as a string, which is handy
$ setattr(store, "rp_linda", 1)

# and this is how we could read it using a string
$ current_value = getattr(store, "rp_linda")
So since this setup uses strings, it allows us to dynamically change variable names when reading or writing them. Here is your example code using this technique:

Code: Select all

label 1in4chance:
    $ num_random = renpy.random.randint(1, 4)
    if num_random == 4:
        pass
    else:
        # the idea: $ rp_[cha_name] -= 1
        # let's just use three lines of code to make it clear
        $ variable_name = f"rp_{cha_name}"
        $ current_value = getattr(store, variable_name)
        $ setattr(store, variable_name, current_value - 1)
    return
But to be honest, if you just have 4 variables, you could also use conditions, right... readable code is better than fancy looking one. It probably takes the CPU less time to execute simple if-else statements as well.

Code: Select all

if cha_name == "linda":
    $ rp_linda -= 1
elif cha_name == "bob":
    $ rp_bob -= 1
# etc.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2420
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Variable as a part of another variable's name?

#3 Post by Ocelot »

The Pythonic way is to use dictionaries:

Code: Select all

default rp = {
    "linda": 0,
    "bob":   0,
    "rose":  0,
    "max":   0,
}

defaul num_random = 0
default cha_name = " "

label question:
    Linda "Puppies are cute! Right?"
    menu:
        "Yes!":
            $ rp['linda'] += 1
            $ rp['bob'] += 1
        "No!":
            $ cha_name = "linda"
            call 1in4chance
            $ cha_name = "bob"
            call 1in4chance

label 1in4chance:
    $ num_random = renpy.random.randint(1, 4)
    if num_random == 4:
        pass
    else:
        $ rp[cha_name] -= 1
            
    return
< < insert Rick Cook quote here > >

User avatar
Adabelitoo
Regular
Posts: 91
Joined: Sat Apr 13, 2019 2:32 pm
Contact:

Re: Variable as a part of another variable's name?

#4 Post by Adabelitoo »

Thank you both for your answers.
m_from_space wrote: Thu May 09, 2024 11:27 am But to be honest, if you just have 4 variables, you could also use conditions, right... readable code is better than fancy looking one. It probably takes the CPU less time to execute simple if-else statements as well.

Code: Select all

if cha_name == "linda":
    $ rp_linda -= 1
elif cha_name == "bob":
    $ rp_bob -= 1
# etc.
This was my plan B. There are only 4 characters now but I don't know how many characters I'll add later so I wanted to do some fancy looking code to make it work with all of them.
Ocelot wrote: Thu May 09, 2024 2:00 pm The Pythonic way is to use dictionaries:

Code: Select all

default rp = {
    "linda": 0,
    "bob":   0,
    "rose":  0,
    "max":   0,
}
I haven't finished testing this yet but it seems to work, the problem is now that when I try to check if the values were modified correctly in the Variable Viewer in the Developer Menu, it shows:

rp = {'linda': 0,...:0, 'bob':0}

Instead of

rp_linda = 0
rp_max = 0
rp_rose = 0
rp_bob = 0

So I can't check if the values for the other characters changed as I expected. Any way to work around this? If not, I guess I'll go with "getattr" and "setattr".

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2420
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Variable as a part of another variable's name?

#5 Post by Ocelot »

1) You can use rp["bob"] in developer console to check specific variable you want.
2) You can use long command in developer console to switch to the long display mode. After that " rp " will show you full info instead of shortened data.
< < insert Rick Cook quote here > >

User avatar
Adabelitoo
Regular
Posts: 91
Joined: Sat Apr 13, 2019 2:32 pm
Contact:

Re: Variable as a part of another variable's name?

#6 Post by Adabelitoo »

Ocelot wrote: Thu May 09, 2024 5:41 pm 1) You can use rp["bob"] in developer console to check specific variable you want.
2) You can use long command in developer console to switch to the long display mode. After that " rp " will show you full info instead of shortened data.
How can I do the second thing?

jeffster
Veteran
Posts: 440
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Variable as a part of another variable's name?

#7 Post by jeffster »

Adabelitoo wrote: Thu May 09, 2024 6:00 pm How can I do the second thing?
You type

Code: Select all

long
in the console and press Enter.
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

User avatar
Adabelitoo
Regular
Posts: 91
Joined: Sat Apr 13, 2019 2:32 pm
Contact:

Re: Variable as a part of another variable's name?

#8 Post by Adabelitoo »

jeffster wrote: Thu May 09, 2024 8:21 pm
Adabelitoo wrote: Thu May 09, 2024 6:00 pm How can I do the second thing?
You type

Code: Select all

long
in the console and press Enter.
This doesn't work. You mean presing Shift+o to open the console and then type long there, right? Because it still looks the same.

jeffster
Veteran
Posts: 440
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Variable as a part of another variable's name?

#9 Post by jeffster »

Adabelitoo wrote: Fri May 10, 2024 5:19 pm
jeffster wrote: Thu May 09, 2024 8:21 pm
Adabelitoo wrote: Thu May 09, 2024 6:00 pm How can I do the second thing?
You type

Code: Select all

long
in the console and press Enter.
This doesn't work. You mean presing Shift+o to open the console and then type long there, right? Because it still looks the same.
For me it works. I open the console, then type "long"<Enter>, then type "rp"<Enter>, and I see the whole output of rp without ellipsis.
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

User avatar
Adabelitoo
Regular
Posts: 91
Joined: Sat Apr 13, 2019 2:32 pm
Contact:

Re: Variable as a part of another variable's name?

#10 Post by Adabelitoo »

jeffster wrote: Fri May 10, 2024 9:45 pm For me it works. I open the console, then type "long"<Enter>, then type "rp"<Enter>, and I see the whole output of rp without ellipsis.
My bad. I thought I would see the whole output in the variable viewer. I didn't realise you were still talking about the console. Yes it works as you said. Thanks.

Thank you everyone. Marked as solved.

Post Reply

Who is online

Users browsing this forum: No registered users