Variables

Variables allow you to store data, for example a number or a text (String). We might compare them to drawers or boxes where we can file notes. Any data can be stored, as long as it is in text or number format. A variable can hold the number of lives remaining, a high-score, the number of bullets left, the number of killed enemies, etc… You are probably going to use variables often in order to store numbers in your game.

Where are variables stored?

The scope of a variable determines where a variable can be accessed. In GDevelop, three variable scopes are available:

  • Global variables are accessible from all the game's scenes. For instance, they can be used to store the player's score across different levels.
  • Scene variables are only accessible from a scene. They can be used for data that only concern one scene and not the entire game. As an example, the time remaining before an explosion.
  • Object variables only concern one object. In this case, a hero can have a “Health” or “Ammo” variable that stores a number that monitors a hero's health or the number of bullets available to that character.

How to declare and use variables

Global, scene and object variables can be defined using the variable editors. For global variables, the Project Manager has a link to global variables. For scene variables, right click on a scene. Then, click on “properties”. For objects, right click on an object in the list and choose “edit object variables”.

Variable editors declare variables along with their initial values.

Note: that these editors are entirely optional. You can also directly start using actions and conditions to modify and compare variables. If you enter the name of a variable that does not exist in an action or a condition, it will be created automatically in memory during the game.

Any name can be used for a variable. By default, variables contain the initial number 0. If you use an action to change the text stored by the variable, the variable will switch to contain the text that you have defined.

Use variables in expressions

Variables can be accessed from conditions, modified from actions and can also be read in expressions.

In an expression computing a number, use:

  • GlobalVariable(name) to read the value of a global variable
  • Variable(name) to read the value of a scene variable
  • ObjectName.Variable(name) to read the variable of the object called “ObjectName”.

In an expression returning a text, use instead:

  • GlobalVariableString(name) to read the text of a global variable
  • VariableString(name) to read the text of a scene variable
  • ObjectName.VariableString(name) to read the variable of the object called “ObjectName”.

See variables in the debugger

FIXME This section is in construction. You can write it yourself by creating an account on the wiki!

Structure variables

FIXME This section is in construction. You can write it yourself by creating an account on the wiki!