In this lesson, you will learn how to bridge the gap between a dying character and a respawned one so that your player doesn't lose their identity or data.
The Problem: The "Floating" Controller
When a Pawn is destroyed (for example, when its health reaches zero), the PlayerController that was "possessing" it suddenly loses its physical body. By default, the PlayerController remains active, but it is now "unpossessed." If you simply spawn a new Pawn, your PlayerController will still be sitting in empty space, unable to send movement commands or input to the new character.
To create a seamless respawn experience, we must explicitly re-link the existing PlayerController to the newly spawned Pawn.
Orchestrating the Hand-off
Since the PlayerController survives the destruction of the Pawn, it acts as the perfect "anchor" for your player's data. To handle this flow, we usually use the GameMode. Here is the logic flow:
- Detection: The
Pawndies and calls a "Restart" request to theGameMode. - Cleanup: The old
Pawnis destroyed, but thePlayerControllerremains alive in the world. - Instantiation: The
GameModespawns a newPawnat aPlayerStartlocation. - Repossession: The
GameModeinstructs the existingPlayerControllerto possess the newPawn.
You can implement this in your GameMode override like so:
// Logic inside your GameMode class
void AMyGameMode::RestartPlayer(AController* NewPlayer)
{
// 1. Spawn the new pawn
APawn* NewPawn = GetWorld()->SpawnActor<APawn>(DefaultPawnClass, SpawnLocation, SpawnRotation);
// 2. Re-link the existing Controller to the new Pawn
NewPlayer->Possess(NewPawn);
}
By using the Possess function, the PlayerController automatically routes its internal input buffers to the new Pawn’s movement components. Because the PlayerController never stopped existing, any local variables you stored on it (like current score or team ID) remain perfectly intact, effectively "transferring" the player's soul into their new body.
Key Takeaway: Because the PlayerController persists independently of the Pawn, you must explicitly call Possess() on the existing controller after spawning a replacement Pawn to restore input functionality.