An error when casting

I was following this tutorial to help create some AI pathfinding using C++, and despite the tutorial being made in UE4 and my project using UE5.4, it has been the only tutorial I have found helpful for what I want to do so I decided to go with it. The tutorial was quite helpful until I reached a part of the video where I assume the person used a feature that is no longer available in UE5 and I am having trouble making it work.

void AAIEnemyController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result)
{
	AAICharacter* EnemyAI = Cast<AAICharacter>(GetControlledPawn());
	//Checks if cast was successful
	if (EnemyAI)
	{
		//Calls the function to pathfind
		EnemyAI->MoveToWaypoints();
	}
}

The error comes from the line GetControlledPawn, which seems not to exist in UE5 anymore.

I am very new at UE and C++ so any help would be appreciated.

Hey,

GetPawn() should be what you are looking for.

1 Like

Thank you, that seems to have worked

I forgot to mention it, but posting it here for reference: there is a templated version of GetPawn, which lets you cast to your type directly. This is just for convenience, slightly faster to type:

Instead of:

AAICharacter* EnemyAI = Cast<AAICharacter>(GetPawn());

You can type:

AAICharacter* EnemyAI = GetPawn<AAICharacter>();

This doesn’t change anything, you still need to check if EnemyAI is valid, in case the cast failed.