You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/gdevelop5/all-features/variables/index.md
+2-80Lines changed: 2 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,27 +41,9 @@ the terms *text* and *string* are used interchangeably.
41
41
42
42
A variable with the *Boolean* data type contains the simplest form of information: either yes or no, 1 or 0, true or false. They are useful as they can be easily toggled.
43
43
44
-
### Structure
44
+
### Structures and arrays
45
45
46
-
A Structure variable maps names to other variables (called "child variables").
47
-
48
-
For example, a simple structure can map the name "Hello" to one sub-variable and the name "World" to another sub-variable.
49
-
You can use this data type to organize related variables within a single variable.
50
-
51
-
!!! tip
52
-
53
-
In programming languages, this data type is often referred to as an _object_, _map_, _hash_, or *dictionary*.
54
-
55
-
Structures can be created in the Variables Editor, using events or by using an extension like [JSON resource loader](../../extensions/jsonresource-loader/).
56
-
57
-
### Array
58
-
59
-
An Array variable, also sometimes called _list_ in programming languages, is like a list of variables.
60
-
61
-
Each variable in an Array has an index, which defines their position in the array.
62
-
The indices begin at 0 and go up to however long the array is.
63
-
64
-
Arrays can be created in the Variables Editor, using events or by using an extension like [JSON resource loader](../../extensions/jsonresource-loader/).
46
+
[Structure and array variables](structures-and-arrays) allow to organize values and access them dynamically.
65
47
66
48
## Using a variable in expressions
67
49
@@ -84,66 +66,6 @@ For a variable of an object, you write the object name first followed by a dot a
84
66
This means if there are conflicting names, the object will always be used in priority. Otherwise, in the events of a scene, it will be a scene variable if it exists, otherwise a global variable if it exists.
85
67
In an extension, the parameter with the given name will be used first, other a property if it exists.
86
68
87
-
## Accessing child variables in structures or arrays
88
-
89
-
Variables that exist within a collection variable (i.e: an array or a structure) are known as _child variables_.
90
-
To access the value of a child variable, use the following syntax in an [expressions](/gdevelop5/all-features/expressions), replacing the values in angled brackets with variable names:
91
-
92
-
```
93
-
<parent_variable>.<child_variable>
94
-
```
95
-
Assume we have this structure:
96
-
97
-

98
-
99
-
To get the value `123` we can write the following expression
100
-
101
-
```
102
-
players.player1.level1score
103
-
```
104
-
105
-
Or, using brackets:
106
-
107
-
```
108
-
players["player1"]["level1score"]
109
-
```
110
-
111
-
!!! tip
112
-
113
-
On structures, `<child_variable>` is the name of the child variable. On arrays it is the index of the child variable. Only **numbers work as indices** for arrays.
114
-
115
-
Parent variables need to be declared, but it's not the case for children variables. The benefit of declaring children is to get autocompletion in the events.
116
-
117
-
!!! note
118
-
119
-
Collection variables (structures and arrays) can contain other collection variables. This makes it possible to store complex data in a single variable. This is helpful when dealing with structured data coming from various sources, including data served from web services or third parties.
120
-
121
-
Just be careful the data doesn't become too difficult to manage if you create structures with a lot of variables.
122
-
123
-
### Accessing child variables dynamically
124
-
125
-
You can use expressions to dynamically access child variables.
126
-
127
-
For example, imagine storing the player's score for each level, called `Level1`, `Level2`, `Level3`. If you want to show the player's score for a specific level, you may store the current level number in a variable called `CurrentLevel`. You could then use the following syntax to access the score:
128
-
129
-
```
130
-
PlayerScore["Level" + CurrentLevel]
131
-
```
132
-
133
-
Whatever is inside the square brackets will be interpreted as the name of the child.
134
-
135
-
If you need to use a variable to define part of the child path, all the subsequent children in the path will need to be in square brackets as well. In the above example if you wanted to address a child called `PlayerScore.Level1.enemies.killbonus` but still define the level dynamically, it would look like this:
Reading the [variable](..) page first will help understanding this page.
9
+
10
+
### Structure
11
+
12
+
A Structure variable maps names to other variables (called "child variables").
13
+
14
+
For example, a simple structure can map the name "Hello" to one sub-variable and the name "World" to another sub-variable.
15
+
You can use this data type to organize related variables within a single variable.
16
+
17
+
!!! tip
18
+
19
+
In programming languages, this data type is often referred to as an _object_, _map_, _hash_, or *dictionary*.
20
+
21
+
Structures can be created in the Variables Editor, using events or by using an extension like [JSON resource loader](../../extensions/jsonresource-loader/).
22
+
23
+

24
+
25
+
### Array
26
+
27
+
An Array variable, also sometimes called _list_ in programming languages, is like a list of variables.
28
+
29
+
Each variable in an Array has an index, which defines their position in the array.
30
+
The indices begin at 0 and go up to however long the array is.
31
+
32
+
Arrays can be created in the Variables Editor, using events or by using an extension like [JSON resource loader](../../extensions/jsonresource-loader/).
33
+
34
+

35
+
36
+
!!! note
37
+
38
+
Array variables should only contain children of the same type, here numbers. When you need a mix of numbers and strings (or even only several numbers), you can make each child a structure as explained in the [Array of structures](#array-of-structures) section.
39
+
40
+
## Accessing child variables in structures or arrays
41
+
42
+
Variables that exist within a collection variable (i.e: an array or a structure) are known as _child variables_.
43
+
To access the value of a child variable, use the following syntax in an [expressions](/gdevelop5/all-features/expressions), replacing the values in angled brackets with variable names:
44
+
45
+
```
46
+
<parent_variable>.<child_variable>
47
+
```
48
+
Assume we have this structure:
49
+
50
+

51
+
52
+
To get the value `123` we can write the following expression
53
+
54
+
```
55
+
Scores.Alice.Levels[0]
56
+
```
57
+
58
+
Or, using brackets:
59
+
60
+
```
61
+
Scores["Alice"]["Levels"][0]
62
+
```
63
+
64
+
!!! tip
65
+
66
+
On structures, `<child_variable>` is the name of the child variable. On arrays it is the index of the child variable. Only **numbers work as indices** for arrays.
67
+
68
+
Parent variables need to be declared, but it's not the case for children variables. The benefit of declaring children is to get autocompletion in the events.
69
+
70
+
!!! note
71
+
72
+
Collection variables (structures and arrays) can contain other collection variables. This makes it possible to store complex data in a single variable. This is helpful when dealing with structured data coming from various sources, including data served from web services or third parties.
73
+
74
+
Just be careful the data doesn't become too difficult to manage if you create structures with a lot of variables.
75
+
76
+
### Accessing child variables dynamically
77
+
78
+
You can use expressions to dynamically access child variables.
79
+
80
+
For example, imagine storing the player's score for each level. If you want to show the player's score for a specific level, you may store the current level number in a variable called `CurrentLevel`. You can then use the following syntax to access the score:
81
+
82
+
```
83
+
Scores.Alice.Levels[CurrentLevel]
84
+
```
85
+
86
+
### Array of structures
87
+
88
+
Array variables can contain structure variables. It allows to have several values for one index.
0 commit comments