Cast on BeginPlay() fails during runtime but otherwise succeeds

Bakeneko
Thu, 14 Jan 2021 11:22:56 +0000
AI

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?

3 posts - 1 participant