Stats #

Read more about the Stats system in Hiro here.

Initializing the stats system #

The stats system relies on the Nakama System and an ILogger, both must be passed in as dependencies via the constructor.

1
2
var statsSystem = new StatsSystem(logger, nakamaSystem);
systems.Add(statsSystem);

Subscribing to changes in the stats system #

You can listen for changes in the stats system so that you can respond appropriately, such as updating the UI, by implementing the IObserver pattern, or use the SystemObserver<T> type which handles it for you.

1
2
3
4
5
var disposer = SystemObserver<StatsSystem>.Create(statsSystem, system => {
  Instance.Logger.Info($"System updated.");

  // Update UI elements etc as necessary here...
});

Refreshing the stats system #

To ensure the stats system has the latest information from Nakama you can refresh it.

1
await statsSystem.RefreshAsync();

Listing public stats #

You can list public stats for the user:

1
2
3
4
foreach (var (key, value) in statsSystem.PublicStats)
{
    Debug.Log($"{key}: {value.Value}");
}

Listing private stats #

You can list private stats for the user:

1
2
3
4
foreach (var (key, value) in statsSystem.PrivateStats)
{
    Debug.Log($"{key}: {value.Value}");
}

Updating public stats #

You can update public stats for the user.

1
var stat = await statsSystem.PublicUpdateAsync("strength", 1, StatUpdateOperator.Delta);
Operators

There are 4 operator values that you can use to update stats:

  • Set: Set the given value, overwriting any previous one.
  • Delta: Increment or decrement the existing value by the given amount. Equivalent to Set if no previous value existed.
  • Min: Use the new value if it’s lower than the existing one. Equivalent to Set if no previous value existed.
  • Max: Use the new value if it’s higher than the existing one. Equivalent to Set if no previous value existed.

Updating private stats #

You can update private stats for the user.

1
var stat = await statsSystem.PrivateUpdateAsync("strength", 1, StatUpdateOperator.Delta);