Variables: Difference between revisions
(Clarified that saved variables can not be used in lists to prevent misunderstandings.) Tags: Mobile edit Mobile web edit |
(Added that saved variables used in lists will make them function the same as global lists and fixed some mistakes I added in the prior edition) Tags: Mobile edit Mobile web edit |
||
Line 7: | Line 7: | ||
Variables can have different modes: <b>Normal</b>, <b>Global</b> and <b>Saved</b>. | Variables can have different modes: <b>Normal</b>, <b>Global</b> and <b>Saved</b>. | ||
Any variable | Any variable can also be a [[List]], although saved ones won't work as expected, instead having the same functionality as global [[List]]s. | ||
The mode of the variable can be changed by pressing the button at the bottom left while editing it's name. | The mode of the variable can be changed by pressing the button at the bottom left while editing it's name. | ||
Line 40: | Line 40: | ||
{{Image|Set_Variable_button_saved.png|Keypad shown with mode being changed to Saved.}} | {{Image|Set_Variable_button_saved.png|Keypad shown with mode being changed to Saved.}} | ||
Saved variables currently only have number type | Saved variables currently only have a number type and there can only be up to 64 saved variables per game. | ||
=== Notes === | === Notes === | ||
Line 47: | Line 47: | ||
* During box art generation (the game is run for a single frame every time a level is selected), saved variables can both be read from and written to; take note of that if you have any scripts writing to them in the first frame. | * During box art generation (the game is run for a single frame every time a level is selected), saved variables can both be read from and written to; take note of that if you have any scripts writing to them in the first frame. | ||
* Saved variables will not save values such as <code>NaN</code>, <code>Infinity</code> and <code>-Infinity</code>, however, they will be stored until the game ends. | * Saved variables will not save values such as <code>NaN</code>, <code>Infinity</code> and <code>-Infinity</code>, however, they will be stored until the game ends. | ||
* Using a saved variable as a [[List]] will have the same functionality as when using a global variable. This means they will contrary to the expected behavior, not save the [[List]]'s values when losing, wining or leaving the game. | |||
* If there are not enough saved variables or they are cumbersome to use, it is possible to encode multiple values into one variable, such as with [https://play.fancade.com/60A0BAEF67EF6DD9 Float Encoder] by [[The Questers#sounak9434|Sounak9434]]. | * If there are not enough saved variables or they are cumbersome to use, it is possible to encode multiple values into one variable, such as with [https://play.fancade.com/60A0BAEF67EF6DD9 Float Encoder] by [[The Questers#sounak9434|Sounak9434]]. | ||
. | |||
[[Category:Scripting]] | [[Category:Scripting]] |
Latest revision as of 22:54, 1 August 2025
Think of a variables as a container with a name that can only store one value at a time. The value in a variable can be changed by using Set Variable, and the value stored in a variable can be read by using Get Variable.

If you were to set another value to a variable, it will overwrite the previous value of that variable.
Variables can have different modes: Normal, Global and Saved. Any variable can also be a List, although saved ones won't work as expected, instead having the same functionality as global Lists.
The mode of the variable can be changed by pressing the button at the bottom left while editing it's name.

Variables of different types (Number, Vector, Object, etc.) and different modes can have the same name and are considered different variables.
For example, Number
and $Number
are different variables.
Global variables
Global variables refer to the same value anywhere in a level.
Let's say you have a script inside a block that sets a variable to 1, and you have a script outside of the block to inspect that variable.

As you can see, we've set the variable to 1, but the Get Variable block outputs 0 as if we never set the variable in the first place.
This is because those two variables are local, meaning they can't transfer variable information from one place to another (inside a block to outside the block, or into another block and vice versa).
If we want a variable to share information to another variable in different places, then we would have to make them global.
Global variables gain a $
at the start of their name.
Saved variables
Saved variables are similar to global variables, but their value persists between levels and game sessions. They can be used to keep track of purchases from the Menu Item block.
Saved variables gain a !
at the start of their name.

Saved variables currently only have a number type and there can only be up to 64 saved variables per game.
Notes
- Saved variables are saved whenever the player wins, loses, or leaves a game, but not when Fancade is force-closed from the task menu.
- If the game is stored in the Edit section, opening the editor resets all saved variables, and they won't be saved when played from inside it. However, updating the game won't cause its players to lose their saved variables, as long as their names don't change.
- During box art generation (the game is run for a single frame every time a level is selected), saved variables can both be read from and written to; take note of that if you have any scripts writing to them in the first frame.
- Saved variables will not save values such as
NaN
,Infinity
and-Infinity
, however, they will be stored until the game ends. - Using a saved variable as a List will have the same functionality as when using a global variable. This means they will contrary to the expected behavior, not save the List's values when losing, wining or leaving the game.
- If there are not enough saved variables or they are cumbersome to use, it is possible to encode multiple values into one variable, such as with Float Encoder by Sounak9434.
.