Differences

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
Last revision Both sides next revision
gdevelop:tutorials:howtousevariables [2018/08/25 23:04]
ndajiya [Quick reminder about variables in GDevelop]
gdevelop:tutorials:howtousevariables [2020/06/13 11:12]
rapperdinesh [Displaying a variable on the screen]
Line 2: Line 2:
  
 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...\\ 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.+In this tutorial, we are going to show how to uses variables in different ​contexts.
  
 You can download the small project used for the tutorial here: **[[http://​www.compilgames.net/​dl/​VariablesTutorial.zip]]**\\ You can download the small project used for the tutorial here: **[[http://​www.compilgames.net/​dl/​VariablesTutorial.zip]]**\\
Line 12: Line 12:
   *Global Variables   *Global Variables
   *Scene Variables   *Scene Variables
-    ​*Initial ​Variables (Default Variable Values+  ​*Object ​Variables (also called Instance Variables
-  ​*Object Variables + 
-    ​*Instance Variables+All of these variables can have default values specified in the editor. 
 ===== Quick reminder about variables in GDevelop ​ ===== ===== 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 allow you to store data, for examplea 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 ). Scene Variables can be viewed by right-clicking the scene editor workspace and selecting properties. Global Variables can be viewed under the Game Settings Tab of the Project Manager. The values of global variables are not changed when switching to a new scene, whereas scene variables are reset.\\ Variables are attached to a scene ( We call them **Scene** variable ) or to the game ( **Global** variables ). Scene Variables can be viewed by right-clicking the scene editor workspace and selecting properties. Global Variables can be viewed under the Game Settings Tab of the Project Manager. The values of global variables are not changed when switching to a new scene, whereas scene variables are reset.\\
Line 42: Line 43:
  
 Variables are also often used to store the life/ammo or any other amount related to the player or to objects.\\ 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. )+In our example, 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:+When the player ​clicks ​on the character, the character is sent in the air and a hurt animation is set. We also want to decrease life by 20:
  
 {{ gdevelop:​tutorials:​variablestutorial_2_.png?​nolink |}} {{ gdevelop:​tutorials:​variablestutorial_2_.png?​nolink |}}
  
 Here we've just used an action to decrease the variable, so whenever the player is clicked, the life will be decreased.\\ 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! )+Note that the conditions ​ensure ​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 ==== ==== Progressively increase a variable ====
  
-Sometimes, we want to increase ​variable more slowly. It can be done easily using the ''​TimeDelta''​ function, which returns the time, in seconds, to compute the latest frame displayed.+Sometimes, we want to increase ​the 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: Here, we just increase the life of the player at the rate of 5 points per seconds:
  
Line 66: Line 67:
 {{ gdevelop:​tutorials:​variablestutorial_5_.png?​nolink |}} {{ gdevelop:​tutorials:​variablestutorial_5_.png?​nolink |}}
  
-Note that these kind of events must be done at the end of the event sheetor 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.+Note that these kinds of events must be done at the end of the event sheet or at least put after every event 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 ==== ==== 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.\\ 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:+Then, just add an event set the text of the object:
  
 {{ gdevelop:​tutorials:​variablestutorial_6_.png?​nolink |}} {{ gdevelop:​tutorials:​variablestutorial_6_.png?​nolink |}}
  
-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 ).+The value is to 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)))''​. 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)))''​.