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?
Difference between 'define', $ and 'default' and when to use which?
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.
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.
- Ocelot
- Lemma-Class Veteran
- Posts: 2402
- 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?
define statement defines a variable in init time. define x = 0 is equivalent to
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:
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.
Code: Select all
init python:
x = 0
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
Generally, using $ x = 0 inside the label should work too. It is a simple assignment without any other features.
< < insert Rick Cook quote here > >
- m_from_space
- Miko-Class Veteran
- Posts: 975
- Joined: Sun Feb 21, 2021 3:36 am
- Contact:
Re: Difference between 'define', $ and 'default' and when to use which?
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.
is the same as
Code: Select all
$ <some python code here in one line>
Code: Select all
python:
<some python code here>
<and even more python code possible inside the block>
...
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.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.
Who is online
Users browsing this forum: Bing [Bot]