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

Tracking Individual Player Statistics

You will use PlayerState to encapsulate unique user data such as kill counts and inventory values that persist during gameplay.

In this lesson, you will learn how to use the PlayerState class to reliably track unique user data that must persist even when a player’s Pawn is destroyed.

Why PlayerState?

In Unreal Engine, a Pawn represents the physical body the player controls. When a player dies or switches to a new vehicle, the Pawn is often destroyed and a new one is spawned. If you store variables like "Kill Count" or "Gold Collected" inside your Character blueprint, that information disappears the moment the character is destroyed.

PlayerState is the solution. It is a class specifically designed to live on the server and exist for the entire duration of a player’s connection to the match. Even if the player is unpossessed or their character is deleted, the PlayerState remains, keeping that precious data safe.

Implementing Statistics

To track stats, you create a custom PlayerState class and add your variables there. Because PlayerState is replicated by default, it is the perfect place to store information that needs to be synchronized across a network.

For example, to track kills, you would add an integer variable KillCount to your PlayerState class. Whenever a player earns a kill, you access their PlayerState and increment that value:

// Inside your logic for handling a kill
APlayerState* CurrentPS = GetPlayerState();
if (CurrentPS)
{
    // Cast to your custom class to access your specific variables
    AMyPlayerState* MyPS = Cast<AMyPlayerState>(CurrentPS);
    if (MyPS)
    {
        MyPS->KillCount++;
    }
}

Accessing the Data

Since every Controller has a built-in reference to a PlayerState, you can easily fetch this data from anywhere—even inside your UI widgets or the GameMode. By keeping these statistics separated from the Character class, you ensure that your gameplay logic remains modular and your player's hard-earned progress is never lost during respawns or state changes.

Key Takeaway: Use the PlayerState class to store individual player statistics, as it persists independently of the Pawn and is designed to survive character destruction and respawning.