Proper way to destroy actor without getting intermittent pending kill errors?

I have a Behavior tree controlled pawn that I want to destroy when the player kills it. However after unpossessing the pawn and calling destroy actor, sometimes I get an error from my movement behavior tree task’s receive tick event saying that it is accessing a property of the pawn which is now pending kill.

How do I the destroy/remove the pawn cleanly if it is in the middle of executing a task? If I only call unpossess and don’t call destroy, the pawn is still left in the world.

Thanks

I tried adding an IsValid node in the begining of my tick event. I still intermittently get the pending kill error. Also tried copying my entire tick graph to a function, put the IsValid node at the beginning of the function, and still got the pending kill error.

How do you destroy actors that stil have events running that use the actor(not just AI tasks)? Using isValid will only decrease the chance that your actor is invalid before you try to use it, but the blueprints are still vulnerable to race condition. From my testing, I have determined that the actor can become invalid right after you checked that it was valid in both event graphs and function graphs.

use is valid function right before destroying actor.
and also you must check whether an actor is valid before calling any function of it.

I had this problem before. take a look at what i did

Thanks for the link, but I already tried putting an IsValid execution node at the beginning of my function which has a a branch, set float, set actor location, and then set actor rotation raises an error that the actor is pending kill. I could check IsValid in more places throughout the function but I don’t think that will 100% fix the problem if this is a race condition.

The Actor is becoming invalid after I have checked that it was valid already in the same function. I tried to search around for this issue, and one of the staff said that you should never need to call DestroyActor, so I still don’t know how to solve this problem. DestroyActor is the only way I know to remove the actor from the World.

he did not say never…

I have destroyed billions of actors in different test projects till now and have used is valid node in just two cases. once for battery collector game and once for that I sent its link.
i think those functions which are inside the actor class are safe not using is valid but functions which must be called from other classes on behalf of that actor are dangerous and need validation.

Mieszko said:
“You should not be calling Destroy on actors, ever (unless you really, really know what your doing).”
and also:
“Calling destroy directly is not a good practice”

I know that IsValid is what you’re supposed to use in cases like this, but as I’ve stated already it doesn’t work for me. The Actor becomes invalid, after that I have checked it is valid already, in the same function / execution graph. Unless this is some Behavior Tree task specific bug like what was reported in my link, I can make a simple project that demonstrates where IsValid will not work.

Alternatively, Mieszko said that we don’t need to call destroy actor, but didn’t go about explaining how to remove something added to the world. As far as I know, In Blueprints, DestroyActor is the only way to remove something added to the level.

I figured out the problem. I was checking that the actor was valid before moving the actor, but not a second time after having moved it. I forgot that moving the actor would immediately trigger an overlap event + destroy in another blueprint event chain. Since I was moving the actor in only very small increments, the error didn’t happen every time.

@saeedc Thank you for the help. I just wanted to figure out why it wasn’t working rather than just putting IsValid everywhere I used the actor reference.