This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:all-features:variables [2018/10/21 20:56]
4ian old revision restored (2018/10/21 19:57)
gdevelop5:all-features:variables [2019/07/01 11:38] (current)
4ian [Other advanced uses]
Line 1: Line 1:
 # Variables # 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 storedas long as it is in text or number format. A variable can hold the number of lives remaininghigh-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.+Variables allow you to store data like numbers and text (stringin computer memoryYou can imagine variables as boxes where you store notes that you want to remember laterUsing variables we can store any type of data like player healthammo, name, items count, high score, ​and anything else that you want to remember later in the game. 
-## Where are variables stored?+## Variable and Data Types
-The scope of a variable determines where a variable can be accessed. In GDevelop, ​three variable scopes are available:+Different types of value need to be stored in different types of variables. In GDevelop, ​variables can have 3 types:
-**Global** variables are accessible from all the game's scenes. For instancethey can be used to store the player'​s score across different levels. +Number
-**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 examplethe time remaining before an explosion. +Text, also called "​string"​ in most programming languages
-**Object** variables only concern one object. In this case, 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.+Structure (which is a variable ​containing other variables)
-## How to declare and use variables+When you store a value, you need to consider how you are going to use this value later. If you would like to do mathematical operations with the value like reducing the player health or increase the score for example, then you must store the player health and score as a number in a number variable. ​
-Globalscene and object variables can be defined using the variable editorsFor global variablesthe [[fr:​gdevelop5:​interface:​project-manager|Project Manager]] has a link to global ​variables. ​For scene variables, ​ [[gdevelop5:​interface:scene-editor|right click on a scene]]. Then, click on "​properties"​. For objects, right click on an object in the list and choose "edit object variables"​.+Likewiseif you would like to store the name of the player, it must be stored as text in a text variable. Number ​and text values cannot ​be mixed without converting ​the value firstIn practiceyou need to make sure that you assign numeric values ​to numeric variables and text values to text variables. ​GDevelop can convert number ​variables ​to text and vice versa if needed - but you should avoid relying on this for performance reasons. 
 +You can convert between number and text using expressions if necessary. To learn more about expressionsvisit [[gdevelop5:​all-features:expressions|this page about expressions]].
-Variable editors declare ​variables ​along with their initial values. ​+## Scope of variables
-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 condition, ​it will be created automatically ​in memory ​during the game.//+The scope of a variable ​determines where variable can be accessed and how long it has to be stored ​in memory. ​In GDevelop, three variable scopes are available:
-Any name can be used for variable. ​ By default, ​variables ​contain ​the initial ​number ​0If you use an action ​to change ​the text stored ​by the variable, the variable will switch to contain ​the text that you have defined.+* **Global** variables are stored in memory for as long as the game is running and can be used to store data that we want to share between all scenes in the entire game. For instance, it can be used to store the player'​s overall score for the game that we can access from every scene. A global ​variable ​is deleted from memory only when we quit the game. 
 +* **Scene** ​variables ​are stored in memory for as long as the scene is active and can be used to store data that we don't want to share between different scenes. For instance, it can be used to store the number ​of enemies in the sceneA scene variable is deleted from memory when we change to a different scene in the game. 
 +* **Object** variables are defined for each individual instance of an object and stored in memory for each instance for as long as the instance is present in the scene. We can use object variables to store data that we don't want to share between instances of an object but we want to store the data for each instance individually. For example, we can use an object variable to store the health for each instance of an enemy object so we can reduce the health of each enemy instance, independently of each other. 
 +  * An object variable is deleted from memory when we delete the instance of the object from the scene. 
 +  * To be very specific, variables ​stored ​in instances of objects living on the scene should be called **instance variables**. You will still see most of the time the terms object variables and instance variables used interchangeably.  
 +## Defining variables 
 +#### Declare variables using the editors 
 +Global, scene and object variables can be defined using the variable ​editors.  
 +* For global variables, the [[gdevelop5:​interface:​project-manager|Project Manager]] has a link to global variables.  
 +* For scene variables, ​ [[gdevelop5:​interface:​scene-editor|right click on a scene]]. Then, click on "​properties"​.  
 +* For object variables, right click on an object in the list and choose "Edit Object Variables"​. 
 +  * You can also change the initial variables of a specific instance of an object that is on the scene. Click on this instance and modify the instance variables in the properties panel. 
 +Variable editors can be used to declare variables as well as their initial values. 
 +#### Use events to create and modify variables. 
 +Variables can also be defined by directly referencing them in actions and conditions. If a variable ​doesn'​t exist it will be created automatically in memory during ​the execution of the event. It will be initialized with the value 0 or an empty text ""​. 
 +<​note>​Even though GDevelop make it possible to use variables without declaring them in the editors, it is still recommended to use the editors to make sure we have all the variables are defined ​and initialized. This allow generation of optimized events and help to keep track of variables in your game.</​note>​ 
 +#### About variable names 
 +Variable names should not contain a dot/period (.) or comma (,). It is recommended to stick with alphanumerical characters - even though you can use any character in practice.
 ## Use variables in expressions ## Use variables in expressions
Line 39: Line 68:
 ## See variables in the debugger ## See variables in the debugger
-FIXME This section ​is in constructionYou can write it yourself ​by creating an account on the wiki!+You can see the global variables, scene variables and object variables at an instance by watching the preview of your game, pausing the game and opening the debugger. 
 +If you click on the variable or the instance mentioned in the left side panel, it would show a list containing variable name and values. But sometimes a variable might hold many children variables, in such a case you will see a list containing all the children variables and their values. 
 +{{ :​gdevelop5:​all-features:​var-debugger.png?​nolink |}} 
 +In the above example, we can see the children variable for the first instance of the variable Frank (a movable character in the game). The "​REFRESH"​ button ​is used to refresh the values of the variable if the variable'​s properties have changed. Let's say that in the above example you go back to the game and move Frank by some amount, you'd notice that the debugger shows the same values for X and YBut if you press the "​REFRESH"​ button, the values change because the X coordinate and the Y coordinate of the player Frank have changed.  
 +Similarly, you can change the variable values in the list so that it will be reflected in your preview. For example, if we change the value of the coordinates of Frank as per my choice, my character would reflect it in the preview ​by changing its position from the old coordinates to the new coordinates. 
 +[[gdevelop5:​interface:​debugger:​|Read more about using the debugger here!]]
 ## Structure variables ## Structure variables
-FIXME This section is in constructionYou can write it yourself ​by creating ​an account on the wiki!+GDevelop supports an advanced feature for variables called ''​Structures''​. Variables ​in GDevelop can be a number, text or a structureIn this case, it has no value, but it contains instead other variables, called **child variables**. These child variables ​can be numbers, text or also structure variables themselves. 
 +If you want to use a variable as a structure, make sure not to use any action or condition that is assigning ​it a number or a text (structure variables do not have any value). Instead, 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 exist, 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 and you can also use them when you need arrays: name the children ''​0'',​ ''​1'',​ ''​2''​… 
 +* Actions and expressions are also available to convert a structure from/to a [[https://​en.wikipedia.org/​wiki/​JSON|JSON]] string, a notation currently used on the web to exchange data.