How are pawns controlled?

I’m trying to understand how pawns are controlled by controllers. I was following the tutorial to setup a c++ pawn (2. Configuring Input and Creating a Pawn Movement Component | Unreal Engine Documentation) and it implements the control logic in the pawn itself. However I would like to create a pawn that could be controlled by either an ai or a player. This probably means using a controller to do it. I’m having difficulty understanding how movement data is passed from one to the other. I’ve found examples for the character class, but I haven’t found anything on controlling the pawn class. Is the controller calling a function in the pawn, is the pawn requesting data from controller? Any information on how the two communicate is what I’m looking for, an example would especially be helpful.

Thx.

Control Logic In Pawn

In all kind of approaches, as far as I know, it is clear that the control logic of the Pawn must be implemented on Pawn.


void AMyPawn::SetThrottle(float Axis)
{
    // Control logic goes here
    AddActorWorldOffset(GetActorForwardVector() * MyPawnVelocity * Axis * DeltaTime);
}

Player Input Binding in Pawn

Pawn has a setup function for Player called SetupPlayerInputComponent. This function recieves Player Input by binding PlayerController’s InputComponent’s methods then you must connnect your Pawn’s control logic by using these Axis and Action mappings.


void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);
    InputComponent->BindAxis("Throttle", this, &AMyPawn::SetThrottle);
}

Player Input Binding in PlayerController

However, there is an other aproach for this by not implementing SetupPlayerInputComponent method on the Pawn which you mentioned I assumed. You can always bind Input from PlayerController.


void AMyPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	InputComponent->BindAxis("Throttle", this, &AMyPlayerController::SetThrottle);
}

void AMyPlayerController::SetThrottle(float Axis)
{
    auto MyPawn = Cast<AMyPawn>(this->GetPawn());
    if (MyPawn) 
    {
        MyPawn->SetThrottle(Axis);
    }
}

The main idea here is the Pawn has a function to control it from outside either PlayerController or AIController.