How do I loop through all the childs of an object variable array

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I…

I am trying to cycle through all the iterations of an object array with structure children of pair string/value (basically a dictionnary in other language), and see if one of this children string match another string.

What is the expected result

I know there is the for each child in array, which unfortunately is limited to global variable array, but it is not possible with object variable array. So I have creaed a while event with LoopIndex, and keep the while active as long as it did not reach the limit of the array size, and for each iteration, I am trying to check the array at index of the LoopIndex if it is matching my test criteria. However, it seems I am doing it wrong.

What is the actual result

Nothing, simply does not goes through the first iteration

Related screenshots

Here I have commentd the relevant code fr debugging purpose, but it is still same issue.

Please include a screenshot of the full related events including both conditions and actions.
And screenshots to help showing us the issue.

Thank you for your support

It sounds like your array contains a bunch of structures like this:

1
   string    " "
   value     " "
2
   string    " "
   value     " "

If so, you could instead use a structure directly:

dictionary
   string      value
   string      value

Anyway, I’m not sure if Loopcounter is used in a While event. You might need to use a local variable instead and add 1 each time.

I think a more convenient method might be to copy the data from Metrics_Text into a scene variable, then use the “for each child” on that variable instead

Your logic seems sound. I think the issue is your events are creating an infinite loop because LoopIndex starts at 0 and your events are checking if the size of the array is >= LoopIndex, when it goes beyond the number of indexes in the array then a new index gets created. The indexes start at 0. So, the index counter goes to 3 but there’s no index 3 so it creates one. This causes an infinite loop.

Say the array had 3 children.

While Array size >= LoopIndex

array size, LoopIndex

3, 0
3, 1
3, 2
3, 3 the condition is still true. A new index is created, the array now has 4 children
4,4 the condition is still true. A new index is created, the array now has 5 children

If you changed your sign from >= to just > then it should work. When the LoopIndex and array size are both 3 then it would no longer be greater than.

I used 3 copies of the same text object for my test and set it’s value to the value of it’s array. It looks like you used 2 different objects. I’m guessing you had pairs of the objects.

My object variables.

My scene events. I reversed the variables. It’s just how I prefer to use it. Changing >= to > should also work.

My result

If the arrays were large, you could also add a condition to check for a variable like foundMatch is false. And then set the boolean to true if there’s a match.

The while event is notorious for creating infinite loops. I usually use the LoopIndex with a larger fail safe value of like 10 or 100. So, if something goes wrong it abort after that many loops. I wish GD had a built-in way to identify and automatically abort infinite loops before they caused crashes and lockups.

Hi, just managed to make the things works, and my code was not so far away too, but my definition of object variable was broken, because the instance of the object with the array was broken. The snippet you gave work right. I also discover the Array extension, which seems confortable to search for string and index.

Thank you very much!

I usually do this with a repeat and variablechildcount, should work perfectly fine