Page 1 of 1

Difference between 'define', $ and 'default' and when to use which?

Posted: Fri May 07, 2021 2:54 pm
by Yuvan raj
Hello, I'm new to renpy and I'm trying to define variables. But sometimes when I define using $ in the middle of a label, I get an error saying that the variable I'm using the $ for is not defined. But when I use 'default', it works.
And there is define too. Where should I use which? Is there any guide specifically for these 3?

Re: Difference between 'define', $ and 'default' and when to use which?

Posted: Fri May 07, 2021 3:21 pm
by Ocelot
define statement defines a variable in init time. define x = 0 is equivalent to

Code: Select all

init python:
    x = 0
It will not be part of the save state, unless you assign new value to the variable name itself (i.e. if you cange only a field of an object, changes will not be saved).
Generally you should use it only for variables which will not be changed in the process of the game or have to be accessible at init time.

default statement sets value to a variable after the game starts or loads if it does not exist. default x = 0 is equvalent to:

Code: Select all

label start:
if <variable x does not exist>:
    x = 0
#...
label after_load:
if <variable x does not exist>:
    x=0
You should use it for variables which are part of your game and will probably change during it. One of the useful features is that save made in older versions of the game might work in the new one after you add some variable.

Generally, using $ x = 0 inside the label should work too. It is a simple assignment without any other features.

Re: Difference between 'define', $ and 'default' and when to use which?

Posted: Sat May 08, 2021 8:54 am
by m_from_space
Maybe you are confused because you are familiar with PHP, but the symbol $ doesn't have to do anything with defining variables in renpy. It's just used for a single line of python code following.

Code: Select all

$ <some python code here in one line>
is the same as

Code: Select all

python:
    <some python code here>
    <and even more python code possible inside the block>
    ...
Yuvan raj wrote:But sometimes when I define using $ in the middle of a label, I get an error saying that the variable I'm using the $ for is not defined.
Maybe you were not trying to define the variable, but changing it before it was defined. You may also just post some code here so people can check it.