scope of variables in screen

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
RewindTheGame
Regular
Posts: 80
Joined: Thu Dec 31, 2020 3:37 pm
Contact:

scope of variables in screen

#1 Post by RewindTheGame »

I'm having real difficulty understanding (and changing) the value of a variable inside a screen, and hoping someone can explain what I'm doing. This is going to be pseudocode because I don't have the code in front of me, but hopefully it'll be clear what I'm saying:

Code: Select all


health=50
show screen testscreen
"Health Value: [health]"
... rest of code

screen testscreen
	text "Health value: [health]
	$health=health-1
	
The thing that's confusing me is that the screen displays health as 50 which means that it is reporting the value of the variable I set outside the screen. But when I reduce the value by one, the main code then reports that health is still 50. This would suggest that the screen reports one version of the health variable but changes another which goes against everything I know about how computer languages should work. That's not consistent. I'm sure I need to do something to make the health variable global rather than local or something, but I'm confused by the fact that the screen is able to report the value of a variable from outside the screen but treat it like a new variable if it's changed.

If there's a local version of the health variable within the screen, it should report its value as zero or not set. If there isn't and it's using the global variable, then changing it should change it everywhere. I'm stumped.

Help!

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

Re: scope of variables in screen

#2 Post by Ocelot »

In most languages reading from a variable reads variable with the narrowest scope. So if both global and local variables with the same name exist, local one will be accessed. This is called shadowing.

When text displayable is created, screen variable does not exist yet, so it searches outer scopes until it finds variable with the same name in global scope and uses it.

I believe, if you switch lines around, you will see different numbers.

To fix that you can access global store directly: store.health = store.health - 1

And now for an elephant in the room. After you apply this fix, you will notice that value of health is not 49, but lower. SOmetimes considerably. This is because you should not have side effects in your screens. Screen code is executed multiple times between interactions, as part of prediction, when it feels like it, after certain time is passed...
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]