PlayerController, Pawn, Character & HUD
Unreal separates the will from the body. A Controller decides; a Pawn is the thing in the world that it possesses. Players use an APlayerController; AI uses an AAIController. The same enemy pawn class can be driven by either, which is how "take control of any unit" features and AI-for-disconnected-players work.
| Class | Responsibility |
|---|---|
AController |
possesses one pawn at a time; has a control rotation (where it is looking) |
APlayerController |
the local player's input, camera manager, UI, mouse cursor; persists across pawn deaths |
AAIController |
runs Behavior Trees / StateTree, pathfinding and perception (AI section) |
APawn |
anything a controller can possess: a turret, a vehicle, a spectator camera |
ACharacter |
a pawn with a capsule, a skeletal mesh and UCharacterMovementComponent: walking, jumping, falling, crouching, swimming and networked movement prediction |
A third-person character
This is the core of what the Third Person template generates, trimmed. Input binding comes in the Enhanced Input lesson.
// ArenaCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "ArenaCharacter.generated.h"
class USpringArmComponent;
class UCameraComponent;
UCLASS()
class ARENA_API AArenaCharacter : public ACharacter
{
GENERATED_BODY()
public:
AArenaCharacter();
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera")
TObjectPtr<USpringArmComponent> CameraBoom;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera")
TObjectPtr<UCameraComponent> FollowCamera;
};// ArenaCharacter.cpp
#include "ArenaCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
AArenaCharacter::AArenaCharacter()
{
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.f);
// The controller's rotation steers the camera, not the body.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
UCharacterMovementComponent* Move = GetCharacterMovement();
Move->bOrientRotationToMovement = true; // face the direction we walk
Move->RotationRate = FRotator(0.f, 500.f, 0.f);
Move->MaxWalkSpeed = 500.f; // cm/s
Move->JumpZVelocity = 700.f;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.f;
CameraBoom->bUsePawnControlRotation = true; // the boom follows the mouse/stick
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
}The mesh and animation Blueprint are assigned in a Blueprint subclass (BP_ArenaCharacter), because they are assets. That is the usual split: C++ defines components and behaviour; a Blueprint child picks assets and tunes numbers.
Possession and respawning
// in the GameMode (server)
void AArenaGameMode::RespawnPlayer(AController* Controller)
{
if (APawn* Old = Controller->GetPawn())
{
Controller->UnPossess();
Old->Destroy();
}
RestartPlayer(Controller); // finds a PlayerStart, spawns DefaultPawnClass, possesses it
}APawn::PossessedBy(AController*) runs on the server when a controller takes over, and APawn::OnRep_Controller / NotifyControllerChanged() on clients. Use those, not BeginPlay, for anything that needs the controller: when BeginPlay runs, the pawn may not be possessed yet.
HUD and UMG
AHUD is the old canvas-drawing UI class. Its DrawHUD is still handy for debug text. Real UI is built with UMG widgets (UUserWidget, designed in the Widget Blueprint editor), created by the PlayerController on the owning client:
// ArenaPlayerController.h
UPROPERTY(EditDefaultsOnly, Category = "UI")
TSubclassOf<UUserWidget> HudWidgetClass;
UPROPERTY()
TObjectPtr<UUserWidget> HudWidget;
// ArenaPlayerController.cpp
#include "Blueprint/UserWidget.h"
void AArenaPlayerController::BeginPlay()
{
Super::BeginPlay();
if (IsLocalController() && HudWidgetClass)
{
HudWidget = CreateWidget<UUserWidget>(this, HudWidgetClass);
HudWidget->AddToViewport();
}
}The widget reads its data from the pawn, PlayerState and GameState, ideally by binding to delegates (health changed, score changed) rather than polling every frame. Add "UMG" to Build.cs. For menus that must work with a gamepad, look at CommonUI (Plugins).
A model: controllers outlive pawns
#include <iostream>
#include <memory>
#include <string>
struct Pawn {
std::string Name;
int Health = 100;
explicit Pawn(std::string n) : Name(std::move(n)) { std::cout << " spawn " << Name << "\n"; }
~Pawn() { std::cout << " destroy " << Name << "\n"; }
};
struct PlayerState { int Kills = 0, Deaths = 0; };
struct PlayerController {
std::unique_ptr<Pawn> Possessed;
PlayerState State; // survives pawn deaths
void Possess(std::unique_ptr<Pawn> p) {
Possessed = std::move(p);
std::cout << " PC possesses " << Possessed->Name << "\n";
}
};
int main() {
PlayerController pc;
int spawned = 0;
auto restart = [&] { pc.Possess(std::make_unique<Pawn>("BP_ArenaCharacter_" + std::to_string(spawned++))); };
std::cout << "match start\n";
restart();
pc.State.Kills += 2;
std::cout << "player dies\n";
pc.State.Deaths++;
pc.Possessed.reset(); // unpossess + destroy
restart(); // GameMode::RestartPlayer
std::cout << "kills " << pc.State.Kills << ", deaths " << pc.State.Deaths
<< ", now driving " << pc.Possessed->Name << "\n";
}match start
spawn BP_ArenaCharacter_0
PC possesses BP_ArenaCharacter_0
player dies
destroy BP_ArenaCharacter_0
spawn BP_ArenaCharacter_1
PC possesses BP_ArenaCharacter_1
kills 2, deaths 1, now driving BP_ArenaCharacter_1
destroy BP_ArenaCharacter_1
The kill count lived in the PlayerState, so the death didn't lose it. Had it been on the pawn, it would have been destroyed along with BP_ArenaCharacter_0.
Test yourself
Why might GetController() be null in a pawn's BeginPlay?
- Pawns can't have controllers
- Possession can happen after BeginPlay; use PossessedBy or NotifyControllerChanged
- BeginPlay runs on the CDO
- Only AI pawns have controllers
What does UCharacterMovementComponent add over a plain Pawn?
- Rendering
- Walking, jumping, falling and networked movement prediction
- AI
- Input bindings
Who should create the HUD widget in a multiplayer game?
- The GameMode
- The PlayerController on its owning client (IsLocalController)
- Every pawn
- The GameState