Course contentsShow
← Unreal Engine Game Development
Lesson 88 of 8802. The Gameplay FrameworkFree lesson

Managing Player Identity via PlayerState

You will utilize the PlayerState class to track individual player statistics, scores, and persistent data that remains consistent across network connections.

In this lesson, you will learn how to use PlayerState to store and synchronize individual player data that persists even if a player disconnects and reconnects during a match.

The Role of PlayerState

While GameMode defines the rules of the match and GameState tracks the progress of the world, PlayerState is the dedicated container for information specific to a single human player. In a networked game, PlayerState is the most reliable place to store data like player names, current scores, or inventory counts.

Crucially, PlayerState objects are replicated to all clients. This means that if Player A increases their score, every other player in the game can see that update because the PlayerState keeps the data consistent across the network. If a player experiences a brief network hiccup or disconnects and rejoins, the PlayerState is the bridge that keeps their identity and stats intact.

Implementing Basic Data

To use PlayerState, you create a custom class inherited from APlayerState. You can add variables to it just like any other actor class. To make these variables visible to others in your game, you use the UPROPERTY(Replicated) macro.

Here is a simple example of how you might declare a score variable in your header file:

UCLASS()
class MYGAME_API AMyPlayerState : public APlayerState
{
    GENERATED_BODY()

public:
    UPROPERTY(Replicated, BlueprintReadOnly)
    int32 PlayerScore;

    // Helper to update the score safely on the server
    void AddScore(int32 Amount);
};

In your implementation, you would then ensure that the GetLifetimeReplicatedProps function is overridden to tell the engine that PlayerScore should be sent to every connected client.

Because PlayerState is owned by the player controller, it is perfectly suited for statistics that shouldn't reset when a character dies or changes pawns. Even if the player’s character (the Pawn) is destroyed and respawned, the PlayerState remains unchanged, preserving the player's personal progress throughout the entire session.

Key Takeaway: Use PlayerState to store persistent, per-player data that needs to be synchronized across the network and survive through character respawns.