Ren'Py Renpy error: not defined [Resolved]

defm64

Newbie
Jan 28, 2025
29
54
Remove the following section:
Python:
python:
    love = 0
    corruption = 0
    ntr = 0
    rose_love = 0
and set variables inside start label:
Python:
label start:
    $ love = 0
    $ corruption = 0
    $ ntr = 0
    $ rose_love = 0

    scene black with fade
    jump day_1
 

Winterfire

Forum Fanatic
Respected User
Game Developer
Sep 27, 2018
5,608
8,201
Even better, just keep them loose in a rpy file. No point in keeping them inside a label.
 

defm64

Newbie
Jan 28, 2025
29
54
Even better, just keep them loose in a rpy file. No point in keeping them inside a label.
Defining these initial variables at the beginning of the start label will prevent unexpected behavior as the project grows in size, as well as making things clearer and more organized.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
11,337
17,192
Remove the following section:
Yes...

and set variables inside start label:
No!


Defining these initial variables at the beginning of the start label will prevent unexpected behavior as the project grows in size, as well as making things clearer and more organized.
Wrong.


It's not an issue for the first release of a game, but for it only.

For any following release, either the code is kept clear and organized, with all variables declaration being grouped in the "start" label, and therefore the game will not be save compatible. Or the game is kept save compatible, and therefore the variables declaration will be spread all over the code, what is the opposite of "clear and organized".

And, obviously, doing it for the very first release is also not an option, because it starts a bad habit.



Ren'Py have two statements specially dedicated to variables declarations, and every devs should use them. The first one being d as constants, and the second being for all the other variables.

This is the only way to have both a clear and organized code, and a save compatible game (at least when it come to variables). All declarations are located in the same place, by example a loose file like Winterfire said. This while ensuring that all variables will exist in a players play, whatever the moment they've been added to the game, whatever how old the save file is, and whatever content the player have seen during his own playthrough.
 

defm64

Newbie
Jan 28, 2025
29
54
Yes...



No!




Wrong.


It's not an issue for the first release of a game, but for it only.

For any following release, either the code is kept clear and organized, with all variables declaration being grouped in the "start" label, and therefore the game will not be save compatible. Or the game is kept save compatible, and therefore the variables declaration will be spread all over the code, what is the opposite of "clear and organized".

And, obviously, doing it for the very first release is also not an option, because it starts a bad habit.



Ren'Py have two statements specially dedicated to variables declarations, and every devs should use them. The first one being d as constants, and the second being for all the other variables.

This is the only way to have both a clear and organized code, and a save compatible game (at least when it come to variables). All declarations are located in the same place, by example a loose file like Winterfire said. This while ensuring that all variables will exist in a players play, whatever the moment they've been added to the game, whatever how old the save file is, and whatever content the player have seen during his own playthrough.
The OP did not provide any detailed information about the project, including whether there will be several updates at different times or whether these variables will actually be used later in the next chapters/days. If it is a project with a really short story, there may not even be a need for players to use saves.

Considering a scenario like this and that when a variable is not defined at game start, using the "default" statement is the same as defining the variable at the beginning of the start label, my solution is still applicable and is still a clear/organized way to deal with these initial variables in the limited context presented by the OP. I am not obligated to imagine/foresee all the different scenarios where it is necessary to give different instructions to someone who asks a question here with limited information.

At no point did I suggest that variable definitions and other declarations should be spread throughout the code. Increasing the number of rpy files will negatively impact performance and further increase the risk of unexpected behavior for players with older devices. This should only be done if it is absolutely necessary or if the developer is willing to reduce the number of rpy files before generating a build.