Ren'Py label with a list of variables

liberatorus

Member
Jun 12, 2022
134
186
I was going through a game's code and noticed a file called variables.rpy

Code:
label variables:

    #character relationship variables
    default lilyRS = 0
    $ persistent.lilyMet = False
    
    # And a lot more variables after
    # ......
   
    return
As far as i can see there is no `jump variables` anywhere. (searched using grep) How do these variables actually get defined? If control never reaches this label

The game in question is Jury if that helps.
 

GetOutOfMyLab

Conversation Conqueror
Modder
Aug 13, 2021
6,034
16,142
It's possible they are calling the label, given that it has a return statement at the end. Either way, I do not recommend defining variables inside labels like this.
 

liberatorus

Member
Jun 12, 2022
134
186
It's possible they are calling the label, given that it has a return statement at the end. Either way, I do not recommend defining variables inside labels like this.
Interesting, just checked it doesn't seem to be called using the call statement either. Is there something I can do like put a `print(stacktrace)` or something similar inside the label that lets me see where exactly it's getting called from?
 

GetOutOfMyLab

Conversation Conqueror
Modder
Aug 13, 2021
6,034
16,142
I did a search for "variables" and there are no references to it at all. However, when Ren'Py loads, even if inside a label, it will read the default keyword and set the variable. So the label doesn't actually have to be jumped to or called... it's defined at init.
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,576
2,203
... that lets me see where exactly it's getting called from?

It's not getting called from anywhere.

default is one of a list of commands that, in effect, are executed when the game starts.
The label has nothing at all to do with them and is likely just the author copy/pasting code from someone who didn't understand it either.

Things like default, define and image are NOT executed at runtime.

When you hit the [START] button on almost every game, those default values are loaded - regardless of where the statements are located within the code.
For much the same reason, those same statements are treated as comments while the script is running (since they've already been executed).

As with all programming languages, there are some caveats with all that. But that's the basics of it.

I wrote this example for a another thread which highlights the point...

Python:
label start:

    "*** START ***"

    $ myvar1 += 1
    "value is [myvar1]."

    default myvar1 = 5

    "new value is [myvar1]."

    "*** THE END ***"

Will display 6 and 6, not 6 and 5 (or even 1 and 5).

Most developers will put statements like these at the top of the scripts, or create separate scripts to hold them. It's purely for organizing things, RenPy doesn't care. But it makes sense to the programmer and will make their lives easier in the future, if they know where to find things that are initialized.

Edit: (for a bit of clarity)...

default should be used for variables where the value will be (or is likely to be in the future) changed by the script.
define should be used for variables that will remain unchanged while the script is running.
 
Last edited:
  • Like
Reactions: Tribe and Meushi

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,576
2,203
I was going through a game's code and noticed a file called variables.rpy
Python:
label variables:

    #character relationship variables
    default lilyRS = 0
    $ persistent.lilyMet = False
   
    # And a lot more variables after
    # ......
  
    return

I just went back and looked at this again and it works... but not for the reasons the original author probably thought.

label variables: - not needed.
default lilyRS = 0 - is fine. But doesn't need to be indented (or within a label).
$ persistent.lilyMet = False - will never be executed, unless a call variables is added to the script.
return - no more needed than the label.

They get away with the persistent variable, because they wanted the default value to be False.
But that line of code is never run and so the variable will never be set. But a peculiarity of persistent variables is that they don't cause runtime errors if they don't exist and will generally behave like an empty string and/or False - depending on how they are used. The end result is the same for this script, but by accident rather than design.

Personally, I would use default persistent.lilyMet = False - since this is clearly a variable that will be changed with the actions of the player and following that rule is safer when starting out. I could explain why it doesn't strictly need to be default like other variables - but that would just muddy the waters and honestly just go into a level of detail not needed for this type of question. Keep it simple - if it's going to change, it should be default.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,299
15,167
I just went back and looked at this again and it works... but not for the reasons the original author probably thought.
[Emphasis is mine]


Ren'Py biggest force, but also its biggest flaw...
 
  • Haha
Reactions: osanaiko