GDevelop: Accessing structure key with global variable (dynamic key not working)

I’m using a global variable structure in GDevelop like this:

inGame.map[“island1”]
inGame.map[“island2”]

Previously, I used a scene variable:
inGame.map[“island” + ToString(currentIsland)]
and it worked.

Now I moved currentIsland into a global variable:
inGame.currentIsland

But this doesn’t work:
inGame.map[“island”]inGame.currentIsland

How do I correctly access:
inGame.map[“island1”], [“island2”], etc.
using inGame.currentIsland?

Is this possible?

If inGame.CurrentIsland is a number.

inGame.map[“island”+ToString(inGame.currentIsland)] 

You need to build the child name. For a structure it’s always a string for an array, it would be a number.

If inGame.currentIsland was a string then you wouldn’t need the ToString().

inGame.map[“island”+inGame.currentIsland]

If there was another child like ID then it would need a period after the bracket.

inGame.map[“island”+inGame.currentIsland].ID

If there were multiple dynamic children then you wouldn’t need a period between the brackets.

inGame.map["Island1"]["ID"] 

inGame.map[Variable1][Variable2] 

Knowing when to use and not use a period can be confusing. The shorter the variable structure, the better.

1 Like

I want top add more on top of Keith tip

Its not like if its text then you don’t need ToString()
Nah
You will break it with it

For example you set text var to AbCd
Now you have change text set to TextVar
And text object will display AbCd

BUT if you will change text set to ToString(TextVar)
You will get 0 instead or was it null
I don’t remember
So if your variable is number then you convert it
BUT NOT when its already text

Now imagine you have Map.Fireland
Map.Waterland

But this will check if you have them
THIS on other hand
Map[“Fireland”]
Map[“Waterland”]

Will not check if they exist
In fact will create them if they don’t exist

So you could have structure variable Map WITH NO CHILD VARAIABLES
And now you can Map[“SOMETHING”] to create/use such variable on the fly dynamically

In fact i am doing it to NOT litter my variable window
I have there ZVar structure which is empty
Look it is empty

But i can totally create them on the fly

I just need to pay ULTRA close attention to names

But i could also do
Map[ToString(NumberVariable)]
Or
Map[TextVariable]

Anyway farm a little

1 Like