In this lesson, you will learn how to clean up your match state and reset player positions without disconnecting clients from the server.
When a round ends, you don't want to destroy the entire session and force players to reconnect. Instead, you need a "soft reset" that returns the world to its initial state while keeping the PlayerController objects alive. In the GameMode class, the ResetLevel() function is your primary tool for this.
The Reset Process
A successful reset requires three distinct steps handled by the server:
- State Reset: You must clear the score variables held in your
GameState. Since theGameStateis replicated, setting scores back to zero here will automatically update the UI for all connected clients. - Pawn Management: You should destroy the old Pawns that are currently in the level. Once the old Pawns are gone, calling
RestartPlayer()on yourPlayerControllerswill trigger the standard spawn logic, placing players back at their startingPlayerStartlocations. - Variable Cleanup: Any custom variables inside your
GameInstanceorPlayerStatethat tracked temporary round data (like "is player currently stunned?") must be set to their default values.
Implementation Snippet
You typically trigger this logic from your GameMode after a win condition is met. Here is a simplified approach to clearing the board:
void AMyGameMode::ResetMatch()
{
// 1. Reset Global State
MyGameState->CurrentScore = 0;
// 2. Iterate through controllers to respawn them
for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
{
APlayerController* PC = It->Get();
if (PC && PC->GetPawn())
{
PC->GetPawn()->Destroy(); // Remove the old pawn
}
RestartPlayer(PC); // Spawn fresh at PlayerStart
}
}
By keeping the PlayerController stable, the client never loses their connection to the server. You are simply clearing the "data" of the match and refreshing the physical actors in the world. This ensures a seamless transition into a new round while maintaining the existing session network stability.
Key Takeaway: To reset for a replay, reset your global data in GameState, destroy existing Pawns, and trigger RestartPlayer to refresh player locations while keeping the existing controller sessions intact.