For example. I have a Structure and and Array.
I want to use the value of an array at an index to lookup the value of a structure
eg: Heroes.TurnArray[0].CurrentHealth
Structure looks like - Heroes.Barbarian.CurrentHealth=20
Array looks like → TurnArray[“Barbarian”, “Monk”, “Ranger”, “Wizard”]
Basically I want to lookup the current health of whichever Hero is currently in the array[0] spot.
Debug.log → Heroes.TurnArray[0].CurrentHealth returns a 0 though unfortunately for some reason
You can access values in a structure dynamically by replacing the first period with brackets with the child name.
Fruits.Apple.Quantity
Becomes
Fruits[“Apple”].Quantity
Or
Fruits[StringVariable].Quantity
In your case
Heroes[TurnArray[0]].CurrentHealth
You can replace everything except the variable name itself meaning in this example Fruits
Fruits[StringVariable] [StringVariable]
As always, be careful with variables. If you try to read a child or index that doesn’t exist then GDevelop will create it. If the number of children of an array or structure can change, it’s always best to use either the child exists condition for structures and/or check the number of children for arrays. Yes, sometimes you need both.
Also, indexes for arrays start at zero. So, the count will always be one number higher than the last index. Meaning an array with 5 items will have indexes from 0 to 4. So, the last index is the count minus one.
2 Likes
Awesome thanks
I guess i was just missing those extra brackets. good to know!
1 Like