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

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
Yuvan raj
Regular
Posts: 38
Joined: Wed Feb 17, 2021 11:24 am
Contact:

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

#1 Post 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?

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

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

#2 Post 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.
< < insert Rick Cook quote here > >

User avatar
m_from_space
Miko-Class Veteran
Posts: 941
Joined: Sun Feb 21, 2021 3:36 am
Contact:

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

#3 Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users