At least in some programming languages, manipulating the array of objects you are looping through in the loop itself can cause foreach to not work correctly.
Maybe try marking them for being destroyed and then have a separate loop for destroying them that is based on a count or something.
Hey guys,
I’m having problems with an infinite loop being detected in my foreachloop, which I just can’t understand how it becomes infinite.
It’s a foreachloop, fed an array from all actors of class where the maximum number should be 2000, rather less.
In the foreachloop are only branches, nothing that could go on infinitely, by far not.
Does anyone see a problem within this script?
Thank you for every helpful hint!
Based on what I’m seeing, you could just use a ForLoop instead, and set the Last Index to 2000.
I doubt the Counter is the real problem though. Base on what I’m seeing, I think it’s being caused by the fact that you have one loop inside another loop, and the internal loop is being scaled inside the external loop. (Spawning object in first loop, then looping over the number of spawned objects inside that loop).
It’s difficult to see well enough what’s going on to be sure, but that’s generally a pretty messy and dangerous thing to do.
Would it be possible to just loop over all the objects after the first loop completes?