Cast to get my custom AiController fails every time

Hi. I’m new to unreal and currently trying to make a FP Dungeon Crawler game which uses the navmesh to make the player do that smooth transition throught positions.
The problem is that I’ve searched all the internet many ways to get the custom AI controller that I’ve set up in the in my character BP, but fails every time.

The prints show how my AIController is set up in the character BP and the spawn of that class in the game as also of my player.



AMyAIController* myAiController = Cast<AMyAIController>(GetController());

if (myAiController)
{
myAiController->MoveToLocation(nextPosition);
bCanGoToNextPosition = false;
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Green, TEXT("Cast Failed"));
}


how did you set your defines in your files for this line?




AMyAIController* myAiController = Cast<AMyAIController>(GetController());


If that code is called on BeginPlay that is too early.

^ Could be that. If you are calling that code in BeginPlay() you do not know the order things will spawn in. The AIController may not exist yet.

Looking at our code … we spawn an individual AIController in BeginPlay() for each AI controlled character. Having their own controller shouldn’t be an issue unless you have a ton of characters.