How do I spawn a non-default AI Controller?

I have implemented my own extension of AI Controller. At some point during the game, I want to create an instance of it, and let it possess a pawn. However, the only relevant function I can find is SpawnDefaultController. Is there a similar one, but for an arbitrary controller?

Simply set “AI Controller Class” variable of your pawn/character to your custom controller and then call SpawnDefaultController. It will always uses the “AI Controller Class” alone while spawning the controller.

You can do this before the game begins or you can do it at run-time assuming your pawn needs a different controller for whatever reason.

Finally, if you look at how SpawnDefaultController works it uses SpawnActor under the hood! All controllers are Actors underneath so it calls SpawnActor with a special flag RF_Transient (because controllers are not persisted). It passes “AI Controller Class” as the class to spawn so you can always set this variable to do what you want.

Hope this helps.

5 Likes

I’ve re-written the answer with a much simpler solution. Check it out in case you were trying out the previous answer I’d written.

Thanks, SpawnDefaultController() works in BP and CPP