Download

How can I check if a pointer to actor was freed

Lets say that I have a class of fireball which holds the fireball target (actor type).
In the tick function I am updating the fireball velocity direction to the target so if the target moves the fireball will keep track at it.
The problem is - what if the actor was already destroyed and freed by the engine?
lets say that the fireball was shot, and then the target died by another entity (whatever it is), so the destroy function has been called and the engine freed the actor. In this situation the fireball hold pointer (target) to a freed memory, and if it will try to call any of the actor functions the game will crash.
How can I check if actor was freed?

I have thought about sending an event when the actor is dead before it gets destroyed, is it the best option?

You can use Weak pointers to track that.



TWeakObjPtr<MyFireball> Fireball = SpawnActor<MyFireball>(...);

if (Fireball.IsValid()) // Our pointer is still valid, do stuff - otherwise it's been cleaned up.