Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
gdevelop:tutorials:howtousevariables [2015/05/18 12:28] 127.0.0.1 external edit |
gdevelop:tutorials:howtousevariables [2020/12/26 13:56] (current) |
||
---|---|---|---|
Line 2: | Line 2: | ||
Variables are extremely useful in a game: They can be used to remember lots of information, | Variables are extremely useful in a game: They can be used to remember lots of information, | ||
- | In this tutorial, we are going to show how to uses variables in different | + | In this tutorial, we are going to show how to uses variables in different |
You can download the small project used for the tutorial here: **[[http:// | You can download the small project used for the tutorial here: **[[http:// | ||
Line 8: | Line 8: | ||
And of course, do not hesitate to experiment with the project and the events! :-) | And of course, do not hesitate to experiment with the project and the events! :-) | ||
+ | |||
+ | **3 Kinds of Variables** | ||
+ | *Global Variables | ||
+ | *Scene Variables | ||
+ | *Object Variables (also called 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 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: |
- | 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.\\ | + | 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.\\ |
Every object of a scene can also have its own set of variables: In this case, the variables are called **Object** variables. | Every object of a scene can also have its own set of variables: In this case, the variables are called **Object** variables. | ||
Line 35: | 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, | + | In our example, life is stored inside a scene variable called '' |
- | When the player | + | When the player |
{{ gdevelop: | {{ gdevelop: | ||
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 | + | Note that the conditions |
==== Progressively increase a variable ==== | ==== Progressively increase a variable ==== | ||
- | Sometimes, we want to increase | + | Sometimes, we want to increase |
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 59: | Line 67: | ||
{{ gdevelop: | {{ gdevelop: | ||
- | Note that these kind of events must be done at the end of the event sheet, or at least put after every events | + | 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 '' | Showing the value of a variable to the player is easy: Put a '' | ||
- | Then, just add an event setting | + | Then, just add an event set the text of the object: |
{{ gdevelop: | {{ gdevelop: | ||
- | The value is get using the '' | + | The value is to get using the '' |
We've also added a call to the '' | We've also added a call to the '' | ||
Line 79: | Line 87: | ||
GDevelop supports an advanced feature for variables called '' | GDevelop supports an advanced feature for variables called '' | ||
- | Variables in GDevelop can contains | + | Variables in GDevelop can contain |
- | Of course, these child variables can contains | + | Of course, these child variables can contain |
- | If you want to use a variable as a structure, you just have to avoid changing its value or text ( As structures | + | If you want to use a variable as a structure, you just have to avoid changing its value or text ( As structure |
- | 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. | ||