Stats
Your game can record stats for each player that plays it. Here are some examples of things stats can record.
Your game can record stats for each player that plays it. Here are some examples of things stats can record.
- Number of zombies killed
- Fastest time
- Total meters walker
- Coins collected
- Longest time
- Bullets fired
- Kills per weapon
You can query and display these stats, or use them in any other way you want. You can query stats from individual players, and you can get the compound stats globally.
Recording Stats
Depending on what you're doing, you either want to increment your stat..
publicย voidย OnZombieKilled()
{
ย ย ย ย Sandbox.Services.Stats.Increment(ย "zombies-killed",ย 1ย );
}
Or just set it directly..
publicย voidย OnGameFinished()
{
ย ย ย ย Sandbox.Services.Stats.Increment(ย "wins",ย 1ย );
ย ย ย ย Sandbox.Services.Stats.SetValue(ย "win-time",ย SecondsTakenย );
}
You can call these apis as often as you like. We batch the stats and send them when we're ready.
You can also add data when calling SetValue. This is in the format of a Dictionary<string, object> and this data is available when querying leaderboards.
Reading Stats
Stats are available in two forms. Either global, or player.
// get global zombie kill count
var zombies = Sandbox.Services.Stats.Global.Get( "zombies_killed" );
Log.Info( $"there have been {zombies.Sum} zombies killed by {zombies.Players} players!" );
// Get local player zombie kill count
var zombies = Sandbox.Services.Stats.LocalPlayer.Get( "zombies_killed" );
Log.Info( $"You have killed {zombies.Sum} zombies!" );
// Get stats for Garry in SS1
var stats = Sandbox.Services.Stats.GetPlayerStats( "facepunch.ss1", 76561197960279927 );
// wait for them to download
await stats.Refresh();
var zombies = stats.Get( "zombies_killed" );
Log.Info( $"Garry has killed {zombies.Sum} zombies!" );
Referenced API
Canonical API pages mentioned in this guide.
Get the global stats for the calling package
Get the global stats for the calling package
Allows access to stats for the current game. Stats are defined by the game's author and can be used to track anything from player actions to performance metrics. They are how you submit data to leaderboards.
No summary available.
No summary available.
No summary available.
No summary available.
No summary available.