Differences

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

Link to this comparison view

Both sides previous revision Previous revision
gdevelop5:all-features:variables [2019/01/05 15:21]
ddabrahim Added a little more explanation to scope of variables
gdevelop5:all-features:variables [2019/01/06 13:01] (current)
ddabrahim added a little more detail to how to define variables
Line 18: Line 18:
 * **Object** variables are defined for each individual instances of an object and stored in memory for each instance as long 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 object variable to store the health for each instance of an enemy object so we can reduce the health of each enemy instance independently from each other. An object variable is deleted from memory when we delete the instance of the object from the scene. * **Object** variables are defined for each individual instances of an object and stored in memory for each instance as long 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 object variable to store the health for each instance of an enemy object so we can reduce the health of each enemy instance independently from each other. An object variable is deleted from memory when we delete the instance of the object from the scene.
  
-## How to declare and use variables+## Defining ​variables
  
-Global, scene and object variables can be defined using the variable editors. For global variables, the [[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"​.+####​Using ​the editor:
  
-Variable editors declare variables along with their initial values. ​+Global, scene and object variables can be defined using the variable editors.  
 +For global variables, the [[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 object variables, right click on an object in the list and choose "edit object variables"​. 
 +Variable editors declare variables along with their initial values.
  
-Notethat 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.//+####Using events:
  
-Any name can be used for a variable. ​ By defaultvariables 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.+Variables ​can also be defined by directly start 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 to avoid any crashes and errors and will be initialized with the value 0 or an empty text ""​In case it is an assignment operation, the value will be assigned to the variable afterward. 
 + 
 +It works in most cases and makes the use of the editors optional. However, in some rare cases, it is necessary ​to have the variables defined before we execute certain events. For example in the case we read a value from storage into a scene variable we need to make sure we have the scene variable defined before we execute the action. Otherwise, we find the action doesn'​t work because ​by the time GDevelop defines ​the variable, ​the action to read a value from storage into the variable ​been already executed and failed because the variable was undefined at the time. GDevelop defines the variable too late in this case. 
 + 
 +So even though GDevelop makes this very convenient for us and using the editors is optional, it is recommended ​to use the editors to make sure we have all the variables ​defined ​and initialized before executing any events to avoid errors in certain cases and to also keep our variables organized. 
 + 
 +####Naming the variables 
 +When naming our variables, there are a few rules we need to follow. Variable names can not begin with a dot (.) or comma (,) but other than that, we can use any characters in our variable names.
  
 ## Use variables in expressions ## Use variables in expressions