Move APawn derived class with AIController

Hi there guys and gals,

I recently had to learn the hard way that you can not move anything else but ACharacter derived classes with an AIController out of the box.
So, my understanding of the ACharacter class is that it has a lot of overhead data in it which would not be used when you’re using it as an AI controlled pawn. Hence I wanted to use an APawn class.

Can anyone point me to some resources on how to get this working if possible? Do I need to implement my own MovementComponent?

Cheers Edregol

Hi Edregol - yes you’re right, if you to stick to the UE4 architecture, the AI Controller expects a UMovementComponent derived component attached to the actor, or move commands are ignored. Do note however that UMovementComponent is very basic and lacks some of the functionality supplied by the engine, such as pathfinding.

Of course it depends on what you’re doing, however if you want a pawn movement component with pathfinding, I’d recommend considering deriving from UPawnMovementComponent (UMovementComponent->UNavMovementComponent->UPawnMovementComponent) then as a loose guide:

  • override UPawnMovementComponent::TickComponent
  • interrogate the owner pawn’s AAIController for the UPathFollowingComponent
  • get the next waypoint from the path
  • adjust the outer actor’s location accordingly

Use CharacterMovementComponent as an example if you need it; it has some functionality that you can base your code off, like FindFloor is useful if you need it.

Getting the AI Controller’s UPathFollowingComponent

UPathFollowingComponent* UMyMovementComponent::GetPathFollowingComponent()
{
	if (!GetPawnOwner()) return nullptr;

	auto controller = Cast<AAIController>(GetPawnOwner()->GetController());
	if (!controller || !controller->GetPathFollowingComponent() || !controller->GetPathFollowingComponent()->HasValidPath())
	{
		return nullptr;
	}

	return controller->GetPathFollowingComponent();
}

Getting the next waypoint

void UMyMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
     UPathFollowingComponent* pathComponent = GetPathFollowingComponent();
     if (!pathComponent) return;

     const TArray<FNavPathPoint>& pathPoints = pathComponent->GetPath()->GetPathPoints();
     uint32 pathIndex = pathComponent->GetNextPathIndex();
     const FVector& nextWaypointLocation = pathPoints[pathIndex].Location;
    
     // ... Adjust actor position based on waypoint
}

NB. Apologies for the lack of URLs; I could never found any resources on this myself - this is all original!

Awesome! Thanks DerPurpelHelmut. Really appreciate that.
I will have a look into this asap.
I’m going to leave this question open for a bit. Maybe this becomes a comprehensive link and resource collection for this matter.