Get a variable child name by its position/index

Hi,
I have a question I haven’t find a solution on other topics or wiki, so I ask.

Let’s say I have this variable structure :

place.11 = “tower”
place.37 = “lake”

There’s only 2 children : “11” and “37”

Here I want to get the name of the 2nd child of my structure. In my case I cannot access it by 37, cause I don’t know if it’s 37 or something else, what I want is to get the name of a child, given its index position, in this example, the 2nd position.
So I’d like, given variable name “place” and given the index “2”, to get “37”, the second child of variable place.

So the generalisation of my example is :
I have the name of the parent variable, “variable name”.
I have the number i, the index of the child I want the name of.
How can I get j, knowing that variablename.j is the "i"th. child of parent variable “variablename”?

I hope my question is understandable and make sense.

In case it’s impossible to get the child’s name, is it possible to get the child’s value, given its parent variable and its index?

For example I want the value of 1st child of parent “place”, and I’d get “tower”

Through GDevelop’s expressions I don’t think it’s possible. However, I have done some javascript experimentation to yield a viable result:

Here is my structure variable for reference:

One quick note, this will only yield child names, and not their values. Haven’t figured quite how to get those, but lemme know if you need help with that or anything else!

OB

2 Likes

Another note, children[1] can be replaced with a variable if you like, something like this: let child_name = children[1]; and then .setString(child_name);

1 Like

Thanks a lot for your explanations!

I’ve never used Javascript before, but I’ll try that, it’s a good way to start learning!

1 Like