ForEachLoop doesn't iterate all items

I’m using a ForEachLoop to turn on Simulate Physics for all the children of the blueprint I’m working on. Problem is the loop always stops short of the last item. The loop will cover all items if I break the last connection (from Cast To BoxComponent to Set Simulate Physics). This makes me think the Set Simulate Physics is somehow altering the array… but I don’t get why that would happen.

Any ideas or suggestions on how I could approach this differently?

Ran into this and here is what I made out from my experience.

Answer:
Here is how I resolved the issue. I made a variable of Object type, Array checked. I then set this from the “Get Children Components” and Pass that variable to the “ForEachLoop” this way the array is unaffected by the “Simulate Physics”

I had an array of 8 children when the loop starts. Printing the index out it goes to 3 (0-3 4 runs), half of the array. So it appears that the “Set Simulate Physics” must remove the object as a child. Which makes sense because a parent constraint connection would cause any transformation to the parent to alter the children. This explains the half-ish run of the loop (rounded up: 9 children = 5 loops). At index 0 it removes the item at 0 and the original index 1 moves to index 0. So when it goes to process the object at index 1 this is now the original index 2 object.

Example:

–Children Of Component–

0 = “Dog”

1 = “Cat”

2 = “Donkey”

3 = “Monkey”

4 = “Ape”

5 = “Turtle”

As it runs it would look like

– Start Index 0 –

0 = “Dog”

1 = “Cat”

2 = “Donkey”

3 = “Monkey”

4 = “Ape”

5 = “Turtle”

– After Loop 0 –

0 = “Cat”

1 = “Donkey”

2 = “Monkey”

3 = “Ape”

4 = “Turtle”

– Start Index 1 –

0 = “Cat”

1 = “Donkey”

2 = “Monkey”

3 = “Ape”

4 = “Turtle”

– After Loop 1 –

0 = “Cat”

1 = “Monkey”

2 = “Ape”

3 = “Turtle”

– Start Index 2 –

0 = “Cat”

1 = “Monkey”

2 = “Ape”

3 = “Turtle”

– After Loop 2 –

0 = “Cat”

1 = “Monkey”

2 = “Turtle”

Notice Cat, Monkey and Turtle are never processed by the loop, because they took the place of just processed index locations.

2 Likes

“Notice Cat, Monkey and Turtle are never processed by the loop, because they took the place of just processed index locations.”
Many thanks, it saved me in 2022. That was my problem in a similar situation, now I understand what is happening

2023 here, this was exactly what was happing to me, great post and explanation.