Page 1 of 1

Checking Persistent Variables

Posted: Sun Jun 25, 2017 6:46 pm
by JustinHanks
Sorry if this is a dumb question, but how do I check the persistent scope for a variable using a variable? I'm more familiar with other programming languages and haven't been able to figure out the python/ren'py syntax...


Instead of...

if persistent.unlock_music

something like...

$ myvar = "unlock_music"
if persistent[myvar]


Thanks.

Re: Checking Persistent Variables

Posted: Sun Jun 25, 2017 7:46 pm
by Imperf3kt
Let me see if I understood you correct.
You want to use a variable as a check for a persistent variable?

Can't say I have any experience with this, nor do I expect it to work, but can you try

Code: Select all

default myvar = "bob"
label start:

Code: Select all

$ myvar = "unlock_music"
$ persistent.[myvar] = True

if persistent.[myvar]:
    $ do.something
But what is your goal with this? It seems unnecessarily complicated.
define myvar = persistent.[myvar] may work, but might confuse renpy.

Re: Checking Persistent Variables

Posted: Sun Jun 25, 2017 8:03 pm
by JustinHanks
I have a screen that displays character stats, so I'm passing in a variable to know which character to display...

use characters(character)

One of the things I'm using the "character" variable for is to check if the character is unlocked or if I need to display a locked message, so using something like "if persistent[character]" prevents me from having to hard-code a check for every single character. The benefit is if I add more characters in the future, I don't need to update this screen.

Every other programming language I've used has a way to do this, so I'm assuming it's possible, I just can't figure out the syntax. Is there maybe a function like persistent.get(character) I could use?

And thanks for your suggestion, but it did not work. :(

Re: Checking Persistent Variables

Posted: Mon Jun 26, 2017 12:14 pm
by Ocelot
You can use python built-in function: https://docs.python.org/2/library/funct ... ml#getattr

Code: Select all

if getattr(persistent, character, None):
    # Something

Re: Checking Persistent Variables

Posted: Tue Jun 27, 2017 8:27 am
by JustinHanks
This is perfect... thank you!