AI not working in Shipping/Packaged Game

I’ve nearly got a basic prototype for a game and was going to share it with friends for a play test. However, when packaging the game, when the AI spots the player (after 10 seconds) the game crashes, but doesn’t crash in the editor.

In my ASeekerAIController.cpp::BeginPlay() I have the following. I’m using a cast so I am able to use m_observer->GetLocation() to get the distance between the AI and the player.

void ASeekerAIController::BeginPlay()
{
	Super::BeginPlay();
	
	m_fpsCharacter = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
	m_observer = Cast<AObserver>(GetPawn());
	
	GetWorldTimerManager().SetTimer(m_timerHandle, this, &ASeekerAIController::CountdownTimer, 10.0f, true, 10.0f);
}

In the header file, the AIs class is declared like so

AObserver* m_observer = nullptr;

The Pawn section in the BP_Seeker has the following selected

Auto Possess Player = false
Auto Possess AI = Placed In World
AI Controller Class = BP_SeekerAIController

However, when stepping through the code, in shipping, m_observer = Cast<AObserver>(GetPawn()); returns null, but doesn’t when I play in the editor. I have no clue what’s going on… any ideas?

Fixed the problem. It seemed like in shipping/packaged, the pawn wasn’t get initialized in time, whereas in development it was. I used the OnPossess() function within my AI Controller and did the cast in there instead. Works a treat!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Yup, what you’ve encountered is called a “race condition”. An error that occurs because you expected something to happen in the correct order but due to parallel processes (cause of multi threading) doesn’t always happen.

While it wouldn’t have exactly fixed this particular bug, it’s good practice to check if a pointer is null using IsValid() before using it.

Privacy & Terms