Cast on BeginPlay() fails during runtime but otherwise succeeds

Hi guys,
I’m initializing a reference to the pawns AIController on BeginPlay() inside the pawns class. This works just fine as long as I place the pawns before loading the game. The cast fails however, once I start spawning in pawns dynamically during runtime. Using the Gameplay AI Debugger shows that the pawns seemingly spawn with the correct controller and I can confirm that their Behaviour Tree is running. Unfortunately, my AI code does not fully execute this way since some of it’s functions are gated behind the valid reference to my AIController.

void AIMPEnemyNPC::BeginPlay()
{
	//...

	IMPController = Cast<AIMPEnemyNPC_AI>(GetController());
	if (IMPController)
	{
		UE_LOG(LogTemp, Warning, TEXT("%s"), *IMPController->GetName());
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Failed!"));
	}

    //....
}

I’m using the “Spawn AI From Class” Blueprint node to spawn the pawns. I have no idea why this Cast fails during runtime.
Apparently I’m missing some background info about how the Engine works behind the scenes?

327192-ue-log-output.jpg

Hello! Take a look at this https://answers.unrealengine.com/questions/1004334/spawn-actor-and-aicontroller-problem.html

Thanks, Kehel18!
Problem resolved.