Is it possible to check if a variable was defined?

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
SLim_Games
Newbie
Posts: 14
Joined: Tue Mar 20, 2018 11:37 am
itch: slim-games
Contact:

Is it possible to check if a variable was defined?

#1 Post by SLim_Games » Tue Mar 20, 2018 11:56 am

I got the problem that i set the variable $ choice1 = "1" but the player sometimes never reaches this point.and if I check the choice later in the game(if choice1 == "1") I get the "name choice1 is not defined" error. I know that I can set $choice1 = "0" at the beginning of my script. But I rather not, because my game gets updated about every month and the players should be able to use their old save files to continue
here is an example:

Code: Select all

menu:
    "buy the apple":
        $ choice1 = "1"
        "you selected option 1"
        jump option1
    "Steal the apple":
        $ choice1 ="2"
        "you selected option 2"
        jump option2
So is it possible to check if a variable has been defined for example:

Code: Select all

if $ choice1 has no value
    $ choice1 = "0":
something like that.. or is setting the variable at the beginning the only option?
Play my game (nsfw) for free at: https://www.patreon.com/SLim_Games

User avatar
trooper6
Lemma-Class Veteran
Posts: 3185
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Is it possible to check if a variable was defined?

#2 Post by trooper6 » Tue Mar 20, 2018 12:55 pm

You need to define your variables. The current best practice is to use default for variables that will change, define for those that won't. So in your case:

Code: Select all

default choice1 = "0"

label start:
    menu:
        "buy the apple":
            $ choice1 = "1"
            "you selected option 1"
            jump option1
        "Steal the apple":
            $ choice1 ="2"
            "you selected option 2"
            jump option2
Note: by using default it will only set the variable as "0" if it had not been previously set. So it should be fine with saves.

Second note: I notice you are defining your variable as a string ("1") rather than as a number (1). Is that really what you want to do?
A Close Shave:
*Last Thing Done (Jul 7) Finished Emotion Matrix for Joint Customer Sprite.
*Currently Doing: Print Matrices, & decide emotions to use for Joint Sprite, think about blinking and lip flap. Maybe also about TransitionConditionSwitch?
*First Next thing to do: Code in all the sprite emotion and camera movement for Close Shave Sprite Sections
*Next Thing to Do: Code in all CG and special animation stuff
*Other Next Thing: Do SFX
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

SLim_Games
Newbie
Posts: 14
Joined: Tue Mar 20, 2018 11:37 am
itch: slim-games
Contact:

Re: Is it possible to check if a variable was defined?

#3 Post by SLim_Games » Tue Mar 20, 2018 1:09 pm

trooper6 wrote:
Tue Mar 20, 2018 12:55 pm
You need to define your variables. The current best practice is to use default for variables that will change, define for those that won't. So in your case:

Code: Select all

default choice1 = "0"

label start:
    menu:
        "buy the apple":
            $ choice1 = "1"
            "you selected option 1"
            jump option1
        "Steal the apple":
            $ choice1 ="2"
            "you selected option 2"
            jump option2
Note: by using default it will only set the variable as "0" if it had not been previously set. So it should be fine with saves.

Second note: I notice you are defining your variable as a string ("1") rather than as a number (1). Is that really what you want to do?
thanks, this is perfect. This was my first choice I ever made and I made this mistake to save it as a string. but I realised that you can save them as numbers later on. but thanks :)
Play my game (nsfw) for free at: https://www.patreon.com/SLim_Games

Post Reply

Who is online

Users browsing this forum: renpyhelp, yumepeh