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/05/16 00:41]
piyushpalawat99 [See variables in the debugger]
gdevelop5:all-features:variables [2019/05/19 22:30] (current)
piyushpalawat99
Line 1: Line 1:
 # Variables # Variables
  
-Variables allow you to store data in the computer memory ​like numbers and text (string). You can imagine variables as boxes where you store notes that you want to remember later. Using variables we can store any type of data like player health, ammo, name, items count, high score, ​everything ​that you want to remember later in the game. +Variables allow you to store data like numbers and text (string) ​in computer memory. You can imagine variables as boxes where you store notes that you want to remember later. Using variables we can store any type of data like player health, ammo, name, items count, high score, ​and anything else that you want to remember later in the game. 
  
 ## Variable and Data Types ## Variable and Data Types
Line 14: Line 14:
 The scope of a variable determines where a variable can be accessed and how long it has to be stored in memory. In GDevelop, three variable scopes are available: The scope of a variable determines where a variable can be accessed and how long it has to be stored in memory. In GDevelop, three variable scopes are available:
  
-* **Global** variables are stored in memory as long 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. +* **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 as long 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 scene. A scene variable is deleted from memory when we change to a different scene in 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 scene. A 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 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 independent of 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 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 independent of each other. An object variable is deleted from memory when we delete the instance of the object from the scene.
  
 ## Defining variables ## Defining variables
Line 30: Line 30:
 ####Using events: ####Using events:
  
-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 to avoid any crashes and errors but it 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 ​afterwards.+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 to avoid any crashes and errors but it will be initialized with the value 0 or an empty text ""​. In case, it is an assignment operation, the value to the variable ​will be assigned later.
  
-It works in most cases and makes the use of the editorsoptional. However, in some rare cases, it is necessary to have the variables defined before we execute certain events. For example in the case of reading 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.+It works in most cases and makes use of the editors ​(optional). However, in some rare cases, it is necessary to have the variables defined before we execute certain events. For examplein the case of reading a value from storage into a scene variablewe need to make sure we have the scene variable defined before we execute the action. Otherwise, we find the action doesn'​t workbecauseby the time GDevelop defines the variable, the action to read a value from storage into the variable ​has 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 that using the editors is optional, it is still 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.+Soeven though GDevelop makes this very convenient for usand that using the editors is optional, it is still 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 ####Naming the variables
Line 57: Line 57:
 ## See variables in the debugger ## See variables in the debugger
  
-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.+You can see the global variables, scene variables and object variables at an instanceby 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. 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.
Line 63: Line 63:
 {{ :​gdevelop5:​all-features:​var-debugger.png?​nolink |}} {{ :​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. ​For example, In the above example, ​if 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 Y. But if you press the "​REFRESH"​ button, the values change because the X coordinate and the Y coordinate of the player Frank have changed. ​+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 Y. But 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. 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!]] [[gdevelop5:​interface:​debugger:​|Read more about using the debugger here!]]
-!+
 ## Structure variables ## Structure variables
  
Line 76: Line 76:
 If you want to use a variable as a structure, you just have to avoid changing its value or text (structure 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''​. If you want to use a variable as a structure, you just have to avoid changing its value or text (structure 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.+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. There is also a condition testing for the existence of a child, and even an action to remove a child from a structure variable.