How to handle persistent AI that can be spawned and unspawned?

So I’m creating an AI that will be used in a procedurally generated environment. Because the AI can be beyond the loading range of the player, I want to be able to spawn and unspawned it but still keep track of its variables. It also needs to continue “moving” at a regular interval, even when not spawned.

What I’m doing right now:

  • I’m spawning an AI controller with
    the goal of managing both far and
    near state.

  • This is using a single Behavior Tree that check if its position is in
    the loading range, with one branch
    for each option.

  • When the player is near, I would want to spawn the actual Pawn.

So my problem right now is that when I spawn the Pawn (with Spawn actor from class), there is a second AI controller created at the same time. Am I doing this the right way? Sould I remove the AI controller from the controller property in my Pawn since I’m doing it this way? How do I make sure that my pawn is possessed by the correct controller?

I would appreciate any feedback you might have.

First of, the player Pawn doesnt need an AI. It’s controlled by the player and uses a PlayerController (which is assigned from the GameMode).

If you “unspawn” (DestroyActor), it’s no longer persistant. You can use the Save Game to store information that you want to keep and simply read the Save Game variables once you spawn the AI again.

You can posses a pawn by Get Player Controller → Posses → set your Pawn mesh in the In Pawn (make sure the Player Controller is connected to the Target pin).

Thank you for your time Amsanity but unfortunately it does not answer my question. Maybe it was not asked clearly enough so here is more info:

I have 2 Pawns in my game, PawnA, which is controlled by a Player controller, and PawnB, which is controlled by an AI controller.

What I want to do is having the AI controller persistent and spawning/unspawning PawnB according to the loading range of PawnA. So this means that if the player (PawnA) is near PawnB, the Actor would be loaded and moving according to the navmesh as most normal AI would do. But if PawnA is too far from PawnB, I still want it to have a behavior but without having the actual Actor loaded.

I hope it’s now clearer :slight_smile:

Have you got a solution? I want to have such a AI, too