Proper way to declare screen only, local 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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Proper way to declare screen only, local variables?

#1 Post by henvu50 »

I have variables that are only used in a screen. How should I declare those variables? Do they have to be declared outside of the screen or is it safe to declare variables within a screen?

Code: Select all


#What is the proper way to declare screen only, local variables?  
#Where should local scope, screen only variables be declared? Outside of screen or within?

screen testWindow:

  vbox: 
     # should I declare screen only variables with default 
     default test_var1 = 0
     # .... or $ ?
     $ test_var2 = 0


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

Re: Proper way to declare screen only, local variables?

#2 Post by Imperf3kt »

If they are only used within the screen (including changing them) then yes, you should place them in the screen. Depending on what you are doing, you will actually need to do so in order for some things to update themselves properly.
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

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Proper way to declare screen only, local variables?

#3 Post by henvu50 »

Imperf3kt wrote: Tue Nov 06, 2018 4:35 am If they are only used within the screen (including changing them) then yes, you should place them in the screen. Depending on what you are doing, you will actually need to do so in order for some things to update themselves properly.
So far using $ seems to be important, since I need the variables to reset to False on every screen update. If I use default, the values get remembered and I lose the functionality I need.

Like you said, I think it depends on what you're doing if you use $ or default as declarations.

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

Re: Proper way to declare screen only, local variables?

#4 Post by Imperf3kt »

If its within a screen, I prefer to use a python block

Example (just because I happen to have it open / be working on it)
Image

This, by the way, when shown in-game, results in the following:
Image
Assuming your device has a battery of course... Otherwise it'll say 100%
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

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

Re: Proper way to declare screen only, local variables?

#5 Post by trooper6 »

You know, you can use default inside a screen. It is right in the documentation: https://www.renpy.org/doc/html/screens.html#default
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Proper way to declare screen only, local variables?

#6 Post by Remix »

Have you also considered passing the value as a screen parameter?

Code: Select all

screen testWindow( test_var = 0 ):
    text "Passed value was [test_var]"

label start:
    show screen testWindow( 5 )
    "..."
I'm not sure whether test_var would be local or global using that approach though... Maybe worth checking if you use parameters
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]