I’ve been following along with an online class teaching how to make a third person shooter, and the current topic is about enemy behaviors and behavior trees. However, despite following along exactly with the code in the recordings, I am getting an access violation error when I run the code. This seems to be caused by me defining APawn* PlayerPawn in BeginPlay, and immediately setting up a BlackboardComponent after it in BeginPlay, which runs while PlayerPawn is still null. Here is the code I’m working with:
void AShooterAIController::BeginPlay()
{
Super::BeginPlay();
if (AIBehavior != nullptr)
{
RunBehaviorTree(AIBehavior);
APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
GetBlackboardComponent()->SetValueAsVector(TEXT("PlayerLocation"), PlayerPawn->GetActorLocation());
}
}
And here is the exact info the error gives me, which specifically highlights the GetBlackboardComponent line when it comes up:
Exception has occurred: W32/0xC0000005
Unhandled exception at 0x00007FF9532A46DF (UnrealEditor-AIModule.dll) in UnrealEditor.exe: 0xC0000005: Access violation reading location 0x00000000000000E8.
Note: In case it’s important to see, AIBehavior is defines in the header file in the private section with the following code, nothing flashy:
UPROPERTY(EditAnywhere)
class UBehaviorTree* AIBehavior;
I have no other code beyond this for my Behavior Tree stuff so far. Is there a better way to define all of this in BeginPlay than what is done in this class’s lessons that avoids this null/access violation issue? The videos might be a little outdated for UE5 specifically (even though they say it should not be a problem).
I should mention I previously defined PlayerPawn in Tick with no issues, but I’d prefer it be in BeginPlay now so as to follow closely with the class.