Destroy Actor on last element of array not working

image

The way BP Graphs work, every time an Exec node is evaluated, its input pins are re-evaluated. In case of ForEachLoop you can assume the Array input pin is re-evaluated before each LoopBody execution (and before Completed as well).

Since the input pin is a pure function call (Self->GetAttachedActors), it is also re-evaluated.
And since you are destroying actor in LoopBody, the result of GetAttachedActors has one less element with every loop check, resulting in skipping every other element. First iteration index=0 you remove first element, then index=1 but GetAttachedActors only returns 2 elements now, you remove the second element (which is the third actor), then index=2 and you are out of bounds so the loop completes. The second actor is never removed.

Promoting to variable is generally the right solution to avoid this.

Sometimes it is not needed, if you are using the result of an Exec node instead of a pure node (no exec pin). For example the result of “Get All Actors Of Class” node. Exec nodes are not re-evaluated unless you trigger their input exec flow. Their output pins behave like global graph variables.

2 Likes