Just a quick note in case you want to do something similar again: If you want to remove items from an array while iterating the array, it’s a good idea to iterate the array in reverse. That way you don’t have to change i
every time you remove an element.
For example, this:
TArray<AActor*> ActorArray;
// initialize ActorArray, then:
for (int i = 0; i < ActorArray.Num(); i++)
{
AActor* Actor = ActorArray[i];
// compute things
if (sometimes true)
{
if (ActorArray.Remove(Actor) > 0)
i--;
}
}
can become this:
TArray<AActor*> ActorArray;
// initialize ActorArray, then:
for (int i = ActorArray.Num() - 1; i >= 0 ; i--)
{
AActor* Actor = ActorArray[i];
// compute things
if (sometimes true)
{
ActorArray.Remove(Actor);
}
}