[Solved] Defining Variables In Screens
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.
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.
- haydengames3
- Newbie
- Posts: 13
- Joined: Wed Mar 17, 2021 9:56 am
- Contact:
[Solved] Defining Variables In Screens
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?
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
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.
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
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.
Re: [Question} Defining Variables In Screens
If you need to use a screen variable, then you should change it via SetScreenVariable action.
https://www.renpy.org/doc/html/screens.html#default
https://www.renpy.org/doc/html/screen_a ... enVariable
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/screen_a ... enVariable
- haydengames3
- Newbie
- Posts: 13
- Joined: Wed Mar 17, 2021 9:56 am
- Contact:
Re: [Question} Defining Variables In Screens
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?
Re: [Question} Defining Variables In Screens
No, if you define it using 'default' statement in a screen; yes, if you set it like
Code: Select all
$ flag = True
- haydengames3
- Newbie
- Posts: 13
- Joined: Wed Mar 17, 2021 9:56 am
- Contact:
Re: [Question} Defining Variables In Screens
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.
Re: [Question} Defining Variables In Screens
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)
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)
- haydengames3
- Newbie
- Posts: 13
- Joined: Wed Mar 17, 2021 9:56 am
- Contact:
Re: [Question} Defining Variables In Screens
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?
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?
- 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
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 > >
- haydengames3
- Newbie
- Posts: 13
- Joined: Wed Mar 17, 2021 9:56 am
- Contact:
Re: [Question} Defining Variables In Screens
Thank you all for your responses, I shall try to work it out from here.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot]