Variables

In GDevelop, you can use variables to store temporary data, such as numbers and text. For example, you might use variables to store the player's name, current health, and score.

To learn how to store data that isn't temporary, such as a player's saved progress, refer to Storage.

Data types

All variables have a data type. The data type of a variable determines what type of data the variable can contain.

In GDevelop, variables can contain the following types of data:

  • Number
  • Text
  • Structure

Number

A variable with the Number data type can contain numeric values, such as 0, 100, and -10. You can perform mathematical calculations on variables that have this data type, such as multiplication or division.

Text

A variable with the Text data type can contain text, such as the words Hello world. In programming languages, this data type is often referred to as a string. In this documentation, the terms text and string are used interchangeably.

Structure

A variable with the Structure data type can contain other variables. You can use this data type to organize related variables within a single variable. In programming languages, this data type is often referred to as an object, hash, or dictionary.

Accessing child variables

Variables that exist within another variable are known as child variables. To access the value of a child variable, use the following syntax in an expressions, replacing the values in angled brackets with variable names:

<parent_variable>.<child_variable>

If a child variable doesn't exist, GDevelop creates it.

Structure variables can contain other Structure variables. This makes it possible to store complex data in a single variable. Just be careful the data doesn't become too difficult to manage.

Accessing child variables dynamically

You can use expressions to dynamically access child variables.

For example, imagine storing the the player's score for each level, called Level1, Level2, Level3. If you want to show the player's score for a specific level, you may store the current level number in a variable called CurrentLevel. You could then use the following syntax to access the score:

PlayerScore[“Level”+ToString(Variable(CurrentLevel))]

Simulating the behavior of arrays

You can use the Structure data type to simulate the behavior of arrays by using an incrementing number as the name of each child variable (e.g. 0, 1, 2, etc).

Scopes

The scope of a variable determines:

  • where the variable can be accessed from
  • how long the variable is stored in memory
  • the steps required to create the variable

In GDevelop, there are three variable scopes:

Refer to the linked pages for more information about each variable scope.

Naming variables

Variable names should not contain dots (periods) or commas. We recommend using alphanumerical characters (A-Z, 0-9).

Using variables without declaring them

You don't have to create (declare) variables before using them.

For example, if you reference a variable that doesn't exist in an action or condition, GDevelop automatically initializes the variable with a default value. The default value is determined by the data type of the variable:

  • Numeric variables are initialized with a value of 0.
  • Text variables are initialized with a value of "" (an empty string).

But while it's possible create variables without first creating them, we recommend creating them anyway, as it allows GDevelop to generate optimized events and helps you keep track of the variables in your game.

Debugging variables

When developing a game, bugs can occur because the value of a variable isn't what you expect it to be. If something in your game isn't working and you think the problem might relate to a variable, use GDevelop's debugger to figure out what's wrong.

For more information, refer to Game Debugger and Profiler.