Checking Persistent 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
JustinHanks
Newbie
Posts: 7
Joined: Tue Nov 01, 2016 12:31 pm
Contact:

Checking Persistent Variables

#1 Post 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.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Checking Persistent Variables

#2 Post 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.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

JustinHanks
Newbie
Posts: 7
Joined: Tue Nov 01, 2016 12:31 pm
Contact:

Re: Checking Persistent Variables

#3 Post 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. :(

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

Re: Checking Persistent Variables

#4 Post 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
< < insert Rick Cook quote here > >

JustinHanks
Newbie
Posts: 7
Joined: Tue Nov 01, 2016 12:31 pm
Contact:

Re: Checking Persistent Variables

#5 Post by JustinHanks »

This is perfect... thank you!

Post Reply

Who is online

Users browsing this forum: No registered users