Skip to content

Commit fe89252

Browse files
committed
Move array and structure sections into a dedicated page
1 parent cbf50e6 commit fe89252

File tree

12 files changed

+93
-81
lines changed

12 files changed

+93
-81
lines changed

docs/gdevelop5/all-features/variables/index.md

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,9 @@ the terms *text* and *string* are used interchangeably.
4141

4242
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.
4343

44-
### Structure
44+
### Structures and arrays
4545

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.
6547

6648
## Using a variable in expressions
6749

@@ -84,66 +66,6 @@ For a variable of an object, you write the object name first followed by a dot a
8466
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.
8567
In an extension, the parameter with the given name will be used first, other a property if it exists.
8668

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-
![](structure-variable.png)
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:
136-
137-
```
138-
PlayerScore["Level" + CurrentLevel].enemies.killbonus
139-
```
140-
141-
Note that this is equivalent to writing:
142-
143-
```
144-
PlayerScore["Level" + CurrentLevel]["enemies"]["killbonus"]
145-
```
146-
14769
## Scopes
14870

14971
The _scope_ of a variable determines:
File renamed without changes.
-144 KB
Binary file not shown.
37.2 KB
Loading
28.8 KB
Loading
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: Structure and array variables
3+
---
4+
# Structure and array variables
5+
6+
!!! warning
7+
8+
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+
![](structure-variable.png)
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+
![](array-variable.png)
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+
![](structure-of-arrays.png)
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.
89+
90+
![](array-of-structures.png)
35.1 KB
Loading

0 commit comments

Comments
 (0)