How to use variables ?

Variables are extremely useful in a game: They can be used to remember lots of information, like the life of the player, the state of an object, the amount of ammo available…
In this tutorial, we are going to show how to uses variables in different context.

You can download the small project used for the tutorial here: http://www.compilgames.net/dl/VariablesTutorial.zip
and you can also try it online.

And of course, do not hesitate to experiment with the project and the events! :-)

Quick reminder about variables in GDevelop

Variables allow you to store data, for example a number or a text. 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: number of lives remaining, high-score, bullets left, killed enemies, etc… You are probably going to use numbers often.

Variables are attached to a scene ( We call them Scene variable ) or to the game ( Global variables ). The values of global variables are not changed when switching to a new scene, whereas scene variables are reset.
Every object of a scene can also have its own set of variables: In this case, the variables are called Object variables.

Using variables

Store the state of an object

Variables can be used to store the state of an object: In the example, the Player object contains a variable called Jumping.
When the variable is set to 0, we consider that the object is not jumping and when it is set to 1, we consider that the object is in the air. Here, the variable is used to avoid adding more than one impulsion to the player when Space is pressed:

The conditions are testing if Space is pressed and if the variable is set to 0. If the conditions are fulfilled, the actions are triggered: The object is moved, and the Jumping variable is set to 1. Thus, the actions won't be able to be triggered more than once.

We then need to set back the Jumping variable to 0 when the object has landed on the ground:

And that's all. Using this variable, we can now easily know if the object is in the air or on the ground, and prevent unwanted repetitions of actions.

Store an amount

Variables are also often used to store the life/ammo or any other amount related to the player or to objects.
In our example, the life is stored inside a scene variable called Life. By default, the variable is set to 100 ( In the project manager, make a right click on the scene and choose 'Initial variables' to show the initial values set. )

When the player click on the character, the character is sent in the air and a hurt animation is set. We also want to decrease the life by 20:

Here we've just used an action to decrease the variable, so whenever the player is clicked, the life will be decreased.
Note that the conditions ensures that the actions won't be repeated more than one time in a row ( Otherwise, it would decrease the life several times per second, and we do not want this! )

Progressively increase a variable

Sometimes, we want to increase a variable more slowly. It can be done easily using the TimeDelta function, which returns the time, in seconds, to compute the latest frame displayed. Here, we just increase the life of the player at the rate of 5 points per seconds:

You could also decrease a variable in the same manner: Just replace the + by a -.

Limit the values of a variable

The life should be always between 0 and 100 in our small game: To ensure that a variable is between two values, uses two events like this:

Note that these kind of events must be done at the end of the event sheet, or at least put after every events modifying the variable, otherwise these events could change again the value of the variable and make it go out of the range.

Displaying a variable on the screen

Showing the value of a variable to the player is easy: Put a Text object somewhere on the scene.
Then, just add an event setting the text of the object:

The value is get using the Variable(Life) part. Then, we convert it to a text using ToString, and finally, we concatenate it with a small text ( Always remember to use quotes when you want to display a text ). We've also added a call to the round function so that the variable is displayed as a whole number without decimal places: “Life: ”+ToString(round(Variable(Life))).

You can also display the variable of an object like this: ToString(Player.Variable(Ammo)),
or, if your variable contains a text, use VariableString to get its content: “Your name is ”+VariableString(PlayerName).

The last part of the tutorial is devoted to structures, an advanced type of variable. If you're a beginner, you probably do not need to read this part for now: Experiment first all the techniques shown above, and then come back if you want more information about structure ;-)

Advanced use of variables: Structures

GDevelop supports an advanced feature for variables called Structures. Variables in GDevelop can contains either a number, a text or be a structure. In this case, it has no value, but it contains instead other variables, called the child variables.
Of course, these child variables can contains numbers, texts or also be structures variables themselves.

If you want to use a variable as a structure, you just have to avoid changing its value or text ( As structures variables do not have any value ). Then, to modify the value of a child variable, just enter as variable name: the name of the variable followed by a period and finally the name of the child variable. For example: PlayersStats.NumberOfDeaths.

As normal variables, if the child does not exists, it will be created.
There is also a condition testing for the existence of a child, and even an action to remove a child from a structure variable.

Dynamic access to the children

Children of a structure can also be accessed using a text expression, using this syntax: Variable[expression].
For example, we can imagine that you're storing the stats of the player for each level, called Level1, Level2, Level3… If you want to show to the player the stats for a specific level, you may store the number of the level in a variable called CurrentLevel.
Then, you can access to the stats using this syntax: PlayersStats[“Level”+ToString(Variable(CurrentLevel))].

Other advanced uses

Structures are useful to store structured data. You can also uses them when you need array: Just name the children 0, 1, 2
Actions and expressions are also available to convert a structure from/to a JSON string, a notation currently used on the web to exchange data.