How to delete / remove un-possessed AI Controllers?

I’m only using blueprints for the following. I have 5 different characters, from a single base class (which inherits from CharacterClass). I’ve made a custom player controller that can posses the different characters. Also the characters follow (using navmesh and simple move to) the currently possessed character. The problem I’m having is that whenever I switch characters new AI Controllers are created but they aren’t being destroyed when I switch. These are the steps I’m trying to make sure that the controller are being destroyed but none of these actually work. The list of AI controllers keeps increasing in the scene outliner.

  • Whenever I want to switch the possessed character I get the controller of the character to be controlled, cast it to AI Controller, and unpossess and destroy it. If cast fails I unpossess and destroy the controller.
  • In LevelBP, using EventTick, I get all actors of class “AIController”, and using a for each loop get owner, check if owner is valid. If not valid I unpossess and destroy. ( I also tried casting the actor to an AI Controller, still didn’t work)

None of the above methods work and switching between characters keeps creating stray AIControllers.

Any help would be appreciated.

You should be able to reassign that original AI controller not create a new one. The way I do it is using the gamestate i pass in my controller and my Pawn and the next pawn and next AI controller that owns the next pawn.

Then Take My controller and possess they next pawn and have the next controller posses my pawn. This works with no leaks. Is there a reason to create a new AI?

Thanks for your reply. I don’t create new controllers, the engine automatically does it when I call SimpleMoveTo. I’ll try your method. Thanks.

Yep that worked. I just switched the AIController and PlayerController. Thanks again.

“Detach From Controller Pending Destroy” Node working perfectly to me also. Destroy all (spawned)AIControllers (eg.: 100), then i can spawn another 1.

https://forums.unrealengine.com/t/how-do-i-unassign-an-ai-controller-to-a-pawn/300470/7