I’m facing an issue where my AI Controller does not attach to a character when I spawn it in the world using the Spawn Actor from Class node.
Here are the details:
The character has the correct AI Controller Class specified in its Class Defaults.
Auto Possess AI is set to Placed in World or Spawned.
When the character is manually placed in the level, everything works fine, and the AI Controller attaches as expected.
However, when I spawn the character dynamically, the Get Controlled Pawn node in the AI Controller returns None.
I also tried manually calling Spawn Default Controller after spawning the character, but it doesn’t seem to fix the issue. I know what AI Controller should be only one in to one actor…
What could be causing the AI Controller to fail to attach to the spawned character, and how can I fix this?
Any help or insights would be greatly appreciated!
I’m just guessing here, but maybe the AI Controller lifecycle and the character’s initialization gets out of sync when dynamically spawning actors. This is why it works when you manually add the character. Like everything is already running correctly in order.
So maybe the AI Controller might not initialize correctly if the character isn’t fully ready at the moment it spawns and while “Placed in World or Spawned” usually works, in some cases, spawning dynamically requires additional steps to ensure the controller properly attaches.
Maybe try this in blueprint (i’m not really familiar with BP):
// Blueprint:
// After spawning the character
SpawnedCharacter->SpawnDefaultController();
You might need a slight delay to let the character finish its initialization. In Blueprint, use a Delay node (e.g., 0.1 seconds) before calling SpawnDefaultController.
Or make sure you override the OnPosses and call corresponding data from there, with a delegate or something that actually works for you.
void AYourAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
// Your custom initialization code
if (InPawn)
{
// Do something here
UE_LOG(LogTemp, Warning, TEXT("AI Controller Possessed: %s"), *InPawn->GetName());
}
}
So again, this is just a guessing, without seeing any actual code you made.