[Solved] Defining Variables In Screens

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
haydengames3
Newbie
Posts: 13
Joined: Wed Mar 17, 2021 9:56 am
Contact:

[Solved] Defining Variables In Screens

#1 Post by haydengames3 »

Hello, I've been trying to create a boolean inside of a screen to create a switch button, however when I create the variable at the start of the screen, it seemingly gets ignored and says that the variable doesn't exist. I've tried doing this without a vbox and it works fine, so I'm guessing it has something to do with the class. Is there a way to create a variable locally and/or globally in a screen that has these conditions such as vbox and frame?

--------------------------------------------------
image.png
--------------------------------------------------
image2.png
--------------------------------------------------

Thanks again to anyone who can help me with these issues, you are saving me from a lot of stress and I deeply appreciate it.
Last edited by haydengames3 on Sat Mar 27, 2021 1:44 pm, edited 1 time in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: [Question} Defining Variables In Screens

#2 Post by Alex »

If you need to use a screen variable, then you should change it via SetScreenVariable action.

Code: Select all

scren test_scr():
    default my_screen_var = 0 # define screen variable
    default my_flag = False

    textbutton "Test: ([my_screen_var])":
        if not my_flag:
            action SetScreenVariable("my_flag", True)
        ellse:
            action [SetScreenVariable("my_screen_var", my_screen_var + 1), SetScreenVariable("my_flag", False)]
https://www.renpy.org/doc/html/screens.html#default
https://www.renpy.org/doc/html/screen_a ... enVariable

User avatar
haydengames3
Newbie
Posts: 13
Joined: Wed Mar 17, 2021 9:56 am
Contact:

Re: [Question} Defining Variables In Screens

#3 Post by haydengames3 »

Ah, so it has it's own function, thank you for clearing this up. I do have another question, a screen is constantly updating, right? So if a variable is defined in a screen, wouldn't it get redefined over and over again?

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: [Question} Defining Variables In Screens

#4 Post by Alex »

haydengames3 wrote: Sat Mar 27, 2021 3:49 am ... wouldn't it get redefined over and over again?
No, if you define it using 'default' statement in a screen; yes, if you set it like

Code: Select all

$ flag = True
When you hide the screen and show it again screen variables will get the default values.

User avatar
haydengames3
Newbie
Posts: 13
Joined: Wed Mar 17, 2021 9:56 am
Contact:

Re: [Question} Defining Variables In Screens

#5 Post by haydengames3 »

Good to know, thank you again for clearing this up. One last thing though, variables they that are changed within a screen, but are not defined in said screen are not saved once the game is saved and loaded. I'm guessing this is because screens themselves are functions and it would need to return() the value in order to update it.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: [Question} Defining Variables In Screens

#6 Post by Alex »

You can change global variables (defined outside the screen) inside the screen using SetVariable action, and they should be saved properly.

Check this article about saving issues - https://www.renpy.org/doc/html/save_load_rollback.html
(https://www.renpy.org/doc/html/save_loa ... after-load)

User avatar
haydengames3
Newbie
Posts: 13
Joined: Wed Mar 17, 2021 9:56 am
Contact:

Re: [Question} Defining Variables In Screens

#7 Post by haydengames3 »

This has created an interesting side effect. I changed my variable from 'init python' to 'define' and ran the code below inside a screen.

textbutton "Increase HP":
action SetVariable("hp",hp + 1)

Now it only increases the first time it's clicked (Greyed out otherwise) and if the number has already increased outside of the screen and it is clicked again, it will reset the number back to + 1 of the original value, any ideas why this is happening?

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

Re: [Question} Defining Variables In Screens

#8 Post by Ocelot »

When the screen is shown (and any time it is evaluated, for prediction or any other reason), it parses screen, evaluating screen elements and their properties. When action is evaluated, it creates a SetVariable object and writes down two values: name of the variable and value to be set. Those values are calculated and recorded at the moment of screen parsing, so hp + 1 is replaced with an actual number. Now, any changes to the hp value are irrelevant, unless screen is parsed again.
< < insert Rick Cook quote here > >

User avatar
haydengames3
Newbie
Posts: 13
Joined: Wed Mar 17, 2021 9:56 am
Contact:

Re: [Question} Defining Variables In Screens

#9 Post by haydengames3 »

Thank you all for your responses, I shall try to work it out from here.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot]