Runtime Error using AI Perception's output in a custom c++ function

In the blueprint of my NPC I have added the AI Perception component, configured the Sight sense in there and added an On Target Perception Updated event to the Event Graph.

I pass the Actor sensed to a custom function. Everything compiles fine but when the Actor (my playable character) is actually sensed during runtime the game crashes with an EXCEPTION_ACCESS_VIOLATION. It happens whenever I try to do anything with the Actor passed to my function.

void AEnemy::Chase(AActor* targetPawn)
{
	auto animInst = GetMesh()->GetAnimInstance();
	auto enemyAnimInst = Cast<UEnemyAnimInstance>(animInst);
	if (targetPawn != nullptr && enemyAnimInst->State == EEnemyState::Locomotion)
	{
		auto enemyController = Cast<AEnemyController>(GetController());
		enemyController->MoveToActor(targetPawn, 90.0f);
	}
	_chasedTarget = targetPawn;
}

As is, it will crash when calling MoveToActor.

Btw, I have tried the same with the PawnSensingComponent first and get the same error. I’m following the tutorial from this book:
Amazon.com: Unreal Engine 5 Game Development with C++ Scripting: Become a professional game developer and create fully functional, high-quality games: 9781804613931: ZHENYU GEORGE LI: Books

if it crashes at MoveActor then enemyController is null. which is possible if GetController is null or it’s not an enemycontroller.

it’s quite possible that the controller is not the right type.

2 Likes

You’re right. The controller class for the NPC was set to the AIController base class instead of my child class.
Thanks.

1 Like

:partying_face: well done turner! i’m glad it helped.

1 Like