Declare 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
User avatar
mitoky
Veteran
Posts: 316
Joined: Sat Feb 07, 2015 9:12 pm
Projects: The Purring Demon's Love, circus eterie
Contact:

Declare persistent variables?

#1 Post by mitoky »

Hello!

I know persistent variables when not decalred, default to None. So i usually had never declared them until recently. But i see here and there people do it and then again some not. I assume both is ok to do, but i wonder what is better?

Declare persistent variables or not?

mikolajspy
Regular
Posts: 169
Joined: Sun Jun 04, 2017 12:05 pm
Completed: Too many, check signature
Deviantart: mikolajspy
Location: Wrocław, Poland
Contact:

Re: Declare persistent variables?

#2 Post by mikolajspy »

I'm not programming expert, but in most languages it's better to properly declare any variable that you're going to use globally.
I believe it matters especially when you use persistent in some True/False check or as a counter.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Declare persistent variables?

#3 Post by kivik »

Personally I see each persistent variable having 3 states:

None: Player never hit the code that sets the variable
True: Player hit the code that sets the variable once
False: Player has hit the code at some point, but it's been reset to False

You'd test the 3 states by:

Code: Select all

if persistent.var:
    "True"
elif persistent.var is None:
    "None"
else:
    "False"
If you're not interested in the None state, then just ditch the is None statement.

Post Reply

Who is online

Users browsing this forum: Google [Bot]