Declaring variables

petertavy

Newbie
Aug 21, 2020
82
18
In traditional languages, variables must be declared before use.
e.g. in Pascal:

var
year: integer;

(etc)

and yes, this makes more work for the programmer, but far less work than if you dont declare
them & then mis-spell. If, in Pascal you say

yaer:= 2000 ;

the compiler immediately flags the error & tells you the line number. If you do the equivalent
in renpy, the error is invisible. You then have the much harder problem of discovering

1. Program is going wrong
2. because year has a wrong value
3. where is the offending line

and I recently found a game with several bugs of this type. I only stumbled across these,
for all I know there may be others.

In Visual Basic there is an option:

option explicit

that says that variables must be declared. Every advice I have read recommends to use this.

Please is there a similar option in renpy? I have searched both this forum & the manual &
I cannot find this. Thank you
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,131
14,814
Please is there a similar option in renpy?
Yes but no.

There's way to "officially" declare a variable, but variables names aren't enforced, therefore it will not have the effect you expect.
 

eugeneloza

Member
Jan 2, 2022
205
91
I think you'd be better searching DuckDuckGoogle or Python/RenPy related communities; have a look at StackOverflow in the first place. I'm not very acquainted with Python but quick skimming the search results show that you need some sort of better IDE that can analyze the code for this sort of errors. In the thread I've looked at they recommended PyCharm and to my unaware look free version may be ok for your goals. It seems like a few years ago PyCharm got integrated with RenPy, though I can't say how well it works.

Or depending on your end-goals just use a stricter language like Pascal/Basic/C# if those feel more convenient for you ;)
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,131
14,814
I think you'd be better searching DuckDuckGoogle or Python/RenPy related communities;
And what are we, if we aren't a Ren'Py related community ?


It seems like a few years ago PyCharm got integrated with RenPy, though I can't say how well it works.
No, it's the opposite. It's PyCharm that was added support for Ren'Py files and language.
 

Deleted member 2282952

Developing I SCREAM
Game Developer
May 1, 2020
416
868
And what are we, if we aren't a Ren'Py related community ?
Lol, exactly, 95% Ren'Py, 4% Unity, 1% others probably.

Because Python is an easy language, and Ren'Py has extensive documentation (it is really well-written imo):



Any bugs that occur are simple to fix with tracebacks and logs.

For me, a good case practice is to define all variables in a script file as

# Variables
define stuff = None
define stuff2 = []
define stuff3 = ""

Now, the type isn't really strictly enforced, but you can keep track and find errors. It really isn't much effort.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,131
14,814
# Variables
define stuff = None
define stuff2 = []
define stuff3 = ""
It's better to use default, unless the variable explicitly don't have to be saved. It's late here, so I'll let someone else explain the difference ; what will leave me the time for what follow..


Now, the type isn't really strictly enforced, but you can keep track and find errors.
From what I understood, OP don't really care about the type, and more about the name.
Whatever define or default permit to assign a default value to a variable, but not to effectively "declare" it ; reason why I answer by "yes but no".

What OP search is a strict declaration of the variable. Therefore variables that can't be used is they haven't been previously declared.

But unlike many other languages, Python (and therefore Ren'Py) do not need prior declaration for its variables. They are created at use.
This mean that if an assignation happen, Python will create a variable at this name ; overwriting the previous variable if the name was already used (in the same scope).
This is due to one of Python specificity, the immutability of the variables.

You said that the type isn't strictly enforced, this is mostly due to the fact that a variable life span is limited to the life span of its value. The instant you make an assignation, the variable disappear and is replaced by a new variable of the same name.
This can be witnessed in Ren'Py console, with the help of id(), that give the ID of the variable:
immutable.jpg
I voluntarily used += in my example, to enforce the fact that it's really all kind of assignation.
The instant the value change, it's not anymore the same variable. It will be a variable with the same name, but not the same variable than before the assignation.

The exceptions are structures (list, set, dict), because they aren't strictly variables. Yet, at entity level, the same behavior happen:
structure.jpg
The dictionary structure stay the same from starts to stop (unless you assign directly a again), but the entity having for index "b" change the instant it's assigned a new value.


In such condition, variable declarations are totally meaningless.
Why verify if the variable have been declared, since strictly speaking the variable will disappear every time its value change ?

This being said, Ren'Py itself could have such behavior. But it would need to add few statements for the assignation (what would then complicate the code), and to update all the statements accepting a variable as input (probably more than 75% of them).
So, I doubt that it will happen one day.
 

Deleted member 2282952

Developing I SCREAM
Game Developer
May 1, 2020
416
868
It's better to use default, unless the variable explicitly don't have to be saved. It's late here, so I'll let someone else explain the difference ; what will leave me the time for what follow..
I understand where the person is coming from, but I'm not on board with the approach (personally). I think utilizing if statements and savepoints of variables pre-change is an approach less prone to errors because the game can always revert back to pre-change_state in case there is something game-breaking going on, which occurs either by inappropriately changed variables / or some other issues. (explaining code in words isn't my strong suit).

From what I understood, OP don't really care about the type, and more about the name.
You are right, I misunderstood OP's question, the way you broke it down makes perfect sense.

To keep things clean I always define variables and keep them at the same state so that the future choices can revert back to check for flags and develop the game in the direction depending on what flags there are. So, for the sake of keeping track, it's easier for me to have a huge list in some data file with all variables defined and unchanged for any future in the game.

I know you can turn the flags into lists/dictionaries/etc, and remove the variable, but I feel like structurally it saves time, at least for me.

I know I'm going off-track, felt like writing it :D
 

petertavy

Newbie
Aug 21, 2020
82
18
Thank you who replied.

I strongly disagree with you who said that debugging programs
is ever easy.

My point about strict declarations is that it makes
some types of error **much** easier to correct because the
compiler or interpreter can detect them.