I have this code that is taking a list of scene components (box collisions) and trying to detect if they have hit another box collision, if they do it spawns a wall and removes every colliding box, if they dont it does nothing.
However it seems to only work ‘most’ of the time.
The more iterations the more likely it is to spawn a duplicate box. It doesn’t seem to fully remove a box before another iteration. What’s the proper way to stop this, AI has been suggesting perhaps multiple iterations are running at once or not completing before the next runs, Ive messed around with some delays but doesn’t seem to help.
One fix was perhaps gating it with a isRunning tag, is the correct way to solve an issue like this? I feel like I am misunderstanding how the code / for loops / or something is executing so I’d like to learn to not just cause this issue again later.
Hi,
You are removing elements from Exit List while iterating over it.
That needs to be done in multiple passes (top level loops), typically you identify things in first pass, remove in second. That doesn’t seem to fit your use case, so you might need a more radical restructure:
While list not empty:
Pop first thing off it.
Loop over the remainder finding overlaps and recording results (do not act on them in this loop).
Loop over your result list/map/whatever and act on everything identified before, and remove things from the exit list.
Whatever you do, do not remove from an array while you are iterating over it! There be dragons.
Actually that’s very helpful I had not considered this I could see how it might cause a problem.
I will see if I can cook up a solution but I feel like logically I could do as you suggested.
Do you think I could
Add the first item selected from the original array to a new array you suggested
Remove the items it collides with (because this is not the item(s) iterated correct?)
After iterating through array, “completed node” → remove array of items that caused collisions from original array
Should not cause duplicates because after first overlap is detected overlapped objects removed but original stayed(?)