Hi, I have a simple question.
In my game I am deleting actors/characters based on their position.
First I tried to do it using Objects Iterator:
for (TObjectIterator<class AMyCustomCharacter> Itr; Itr; ++Itr)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, Itr->GetName());
if(condition){Itr->Destroy();}
}
I noticed that even when the Character(s) get(s) deleted in the game, it doesn’t entirely delete it’s reference ,
so that makes problems when trying to keep track of them as it always lists all of them like they wouldn’t be deleted at all.
So instead I simply iterate on the Array like this:
for (AMyCustomCharacter* EachChar : CharactersArray)
{
if (condition)
{
EachChar->Destroy();
CharactersArray.Remove(EachChar);
break;
}
This way I can keep track of the characters in game and all works fine.
So why is this happening? Is it cause when a Character is spawned, a pointer variable is auto-created to use with iterate functions? If someone could explain this a little more it would be nice.
Thanks,
Jurij
PS: After testing I can say this, unlike Object Iterator that obviously keeps the pointers even after you Destroy() an actor, Actor Iterator does track the actors in game properly, as said by with a few ticks of delay when calling Destroy(). However in my case I will rather use Iterating an Array, it doesn’t have the delay since you can manually remove the pointer variable from the Array, after Destroying the Actor/Character.
Here’s an example of Actor Iterator:
for (TActorIterator<class AMyCustomCharacter> Itr(()); Itr; ++Itr)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, Itr->GetName());
}