Achievements #

Read more about the Achievements system in Hiro here.

Initializing the achievements system #

The achievements system relies on the Nakama System and the Economy System, both must be passed in as dependencies via the constructor.

1
2
var achievementsSystem = new AchievementsSystem(nakamaSystem, economySystem)
systems.Add(achievementsSystem);

Subscribing to changes in the achievements system #

You can listen for changes in the achievements system so that you can respond appropriately, such as updating the UI, by implementing the appropriate interface.

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

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

Refreshing the achievements system #

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

1
await achievementsSystem.RefreshAsync();

Listing achievements #

You can list all achievements for the user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
foreach (var achievementKvp in achievementsSystem.Achievements)
{
    var id = achievementKvp.Key;
    var achievement = achievementKvp.Value;
    
    Debug.Log($"Achievement {achievement.Name} ({achievement.Category})");
    Debug.Log(achievement.Description);

    if (achievement.HasReward())
    {
        Debug.Log("Reward available on completion");
    }

    if (achievement.SubAchievements.Any())
    {
        Debug.Log($"{achievement.SubAchievements.Count} sub-achievements.");
    }
}

You can also list all repeatable achievements for the user.

1
2
3
4
foreach (var achievementKvp in achievementsSystem.RepeatAchievements)
{
  // ...
}

As well as list achievements by category for the user.

1
2
3
4
foreach (var achievement in achievementsSystem.GetCategory("<categoryName>"))
{
  // ...
}

Listing available achievements by category #

Similarly to above, you can list achievements by category for the user, including only achievements the user is currently able to complete.

1
2
3
4
foreach (var achievement in achievementsSystem.GetAvailableAchievements("<categoryName">))
{
  // ...
}

Updating achievement progress #

You can update the user’s progress for achievements.

1
2
var timesCompleted = 1;
await achievementsSystem.UpdateAchievementsAsync(new[] {"<achievementId1>", "<achievementId2>"}, timesCompleted);

Claiming achievement rewards #

You can claim rewards for an achievement. You can also specify whether you would like to claim any available total rewards (e.g. if the user has completed all sub-achievements) as well as whether or not to refresh the Economy System.

1
2
3
4
var claimTotalReward = true;
var refreshEconomy = true;

await achievementsSystem.ClaimAchievementsAsync(new[] {"<achievementId1>", "<achievementId2>}", claimTotalReward, refreshEconomy);