[SOLVED] Basic working with array variables still unclear

Hi,

I’d like to learn, what I still didn’t understand with the array variables.
I made this little program which should display in a textbox the text of variable c1.
If I set the variable counter to the value 2, the text of c2 should be displayed.

With the code as it is now, only the number “0” is displayed, no matter if I change counter or not.
Either I’m doing things wrong in the creation of the variable or the expression is wrong.
Please help me to identify and remove the mistake.

You can’t concatenate a variable name directly ( c[Variable(counter)] ), but you can access a child variable through the variable string name, the strings can be concatenated :slight_smile:
For example, you can create a structure like this:

Main c1 = "255;255;255" c2 = "127;127;127"
Then you can access c2 (with counter = 2) this way: VariableString(Main[“c”+VariableString(counter)])

Thank you very much Lizard-13,

I’m sorry, but things are still unclear to me.

I’d like to keep this example very close to the variables tutorial.
Instead of your “Main” I’d like to use my “c”.

So I’d have to enter something like your

CODE: SELECT ALL
c
1= “255;255;255”
2= “127;127;127”

but I don’t know how.

I mean, things worked fine when using these kind of structures as object variables and now I need a structure without an object, and I feel like I am lost.
I look at variables and there is an option for “structures” - but it will only let me delete a child.
Where is the option to create the structure? How do I do that?

4ian wrote in the variables tutorial:

That means c1 and c2 are variables without child variable.
Changing their name to c.1 and c.2 gives me variable c with two child variables.

Now to the “dynamic access”:
Do=VariableString(c[VariableString(counter)]) to the text of MyText

This puts a text into the MyText textobject.
It uses the variable c.
To access the child variable dynamically, there has to be an expression - that’s what the are for.
In it the value of variable counter is turned into a text.
Notice that the period between the c and the childnames doesn’t appear.

Below is the changed and working example, thank you very much, again, Lizard-13.

Your final example looks correct, so you could guess that structures are created dinamically, when you do something like: Do = "255;255;255" to the text of variable c.1
GDevelop will create all the structure to get the variable c.1, here just have to create the child “1” inside “c”.
Another way to create structures is in the “Variables window” (right click on the project ==> edit global variables, right click on the scene ==> edit scene variables, properties of an object ==> variables ==> edit). When you are in that window, right click on a variable and select “Add child variable” :slight_smile:

Perfect :smiley:
The wiki talks about this dynamic access, with the form Variable(Main[“child-name”]), where Main could be a child, so you could have complex structures, and mix access:

Player.Variable(Main["Level"+VariableString(level_selected)].Stats[VariableString(stat)]["value"])

Lizard-13 wrote:

Hidden… right click… that’s why I didn’t find it.

Thank you very, very much, again.