Proper way to manage AIController with switching Pawns?

I very new to Unreal and recently found out AIControllers can’t be destroyed :rolleyes:, but in my game, i spawned many enemies, all with an own AIController, so after some “waves” of enemies, i was flooded with AIControllers, and everything was becoming laggy after a while, since they update on tick.

I rewrote my code, and my first attempt was to unposses the AI-pawn, when he got destroyed and if there is an empty AiController, no new AIController would be created and the old one is reset and used for the next spawning pawn. (since all pawns use the same logic it seems like a great idea)

However, i ran into the next Problem: (please read)
https://answers.unrealengine.com/questions/573985/why-does-my-validation-check-not-work.html
I came to the conclusion, that the Actor is not destroyed instantly, so i used a boolean to check INSTANTLY if the AiController was marked “BOOL_FREE”, but that led to the same result, and i can’t figure out why…

The AiController seem to ignore the execution-flow, and just gives me random “missing reference” NONE errors in functions, the execution should never reach, because of simple bool checks/branches. It’s like the code is running parallel, and suddenly the pawn gets destroyed and everything breaks.

I just need to know how to make the AiController do NOTHING while waiting for a new pawn

I’m very depressed, my code now looks like spaghetti or spiderweb (i tried hard to solve this “simple” problem)
please god of war send help :frowning:

I think Event Unposses may fire in the AIController when its pawn is destroyed. I’m not 100% sure on this though, but it is worth a shot.

From there you should be able to get the ‘brain component’ from the AI Controller and call the function ‘Stop Logic’

Borrowing this image from another answerhub question to show what the nodes look like within the AI controller:

Also be sure to use the IsValid macro on any blue pins to avoid Reference None errors from occurring.

Thanks for your help.
My AI is very simple, i don’t even use behaviour trees, so StopLogic does nothing. I tried using a BTree at the beginning, and got frustrated, since values in the behaviour tree weren’t updated until the branch failed upwards. I will take a look at them later, if I’m more comfortable with the Engine in general, and understand how to fix this problem.

I solved my Problem by creating 8 AiControllers on Level-Begin-Play, and made them looking for and finally possessing Pawns with no AIController [optional new AiControllers will be spawned, if there are more Pawns than AiControllers]. A IsValid check on all reference pins inside the BP solved the missing-reference problem.
And… i don’t update the AIController BP on tick anymore, but with a custom event, that gives me more control.
It’s not the nicest way, but it works, and i can finally continue :slight_smile: