In this lesson, you will learn how the GameMode acts as the director of your game by triggering the Possess process, which bridges the gap between a player's input and a physical body in the world.
The Role of Possession
Think of a PlayerController as the "brain" or the user's soul, and the Pawn as the physical body currently residing in the game level. By themselves, they are disconnected. Possession is the vital moment where the GameMode takes these two separate objects and links them together so that when you press a key on your keyboard, the Pawn reacts.
How the GameMode Orchestrates the Flow
When a player joins a match, the GameMode is responsible for deciding which Pawn to spawn and which PlayerController will take control of it. This usually happens in the RestartPlayer function. The flow typically looks like this:
- Spawn: The
GameModespawns aPawnat aPlayerStartlocation. - Assign: The
GameModeidentifies thePlayerControllerthat is ready to play. - Possess: The
GameModecalls thePossess()function on thePlayerController, passing in the newly spawnedPawn.
You can see this connection happening in code:
// Inside your GameMode's RestartPlayer function
void AMyGameMode::RestartPlayer(AController* NewPlayer)
{
// 1. Spawn the Pawn class defined in the GameMode
APawn* MyPawn = GetWorld()->SpawnActor<APawn>(DefaultPawnClass, StartSpot);
// 2. Tell the Controller to take over the Pawn
NewPlayer->Possess(MyPawn);
}
Once Possess() is called, the PlayerController gains ownership of the Pawn. The controller then starts routing input—like moving the mouse or pressing "Jump"—directly into the logic contained within that specific Pawn. If the Pawn is destroyed, the GameMode can trigger the possession process again for a respawn, ensuring the gameplay flow remains uninterrupted.
Key Takeaway: Possession is the crucial handshake where the GameMode assigns a physical Pawn to a PlayerController, enabling the bridge between user input and in-game character movement.