Getting achievements

Get all the achievements in the game, or get achievements acquired by a player.

Since there can be hundreds of achievements in your game, it is not feasible to retrieve all achievements at the same time because of processing and network constraints. Therefore, is used so that achievements are retrieved in parts.

Getting all achievements

To get all achievements, whether acquired by the player or not, use the GetAchievements method in CoreServices.

There are a few key parameters to take note of:

  • string? query: A full-text search query.

  • int pageSize: The number of achievements to retrieve at a time. Defaults to 30, maximum 30.

  • string? groupName: The group ID to get achievements from. Either this or ungrouped can be specified, not both.

  • bool? ungrouped: Whether to get ungrouped achievements. Either this or groupName can be specified, not both.

  • AchievementsSortField sortBy: The field to sort the achievements by. Defaults to display name.

  • SortDirection sortDirection: The direction to sort the achievements by. Defaults to ascending.

A cursor (instance of AchievementsCursor) is returned, and you can use the HasNext attribute of the cursor to determine if the pagination has reached the last page, and use the Next() method to retrieve the next page. Each page is a list of Achievement instances, containing information such as the achievement ID, display name, cover URL and short description.

Example - Get achievements with pagination

var cursor = CoreServices.GetAchievements(
    query: "Gold",
    groupName: "group1",
    sortBy: AchievementsSortField.GroupOrder,
    sortDirection: SortDirection.Ascending
);

while (cursor.HasNext) {
    var achievements = await cursor.Next();
    foreach (var achievement in achievements)
    {
        Debug.Log($"Achievement ${achievement.DisplayName} retrieved.");
        /* ... */
    }
}

However, if you would like to retrieve every achievement with one method call, you can list the brief information of all achievements instead, using the ListAllAchievements method in CoreServices. The parameters are the same as GetAchievements; however, querying achievements is unavailable.

This will return a list of ListedAchievement instances, containing only the achievement ID, display name, group ID and group relative order.

Example - List all core achievement information

var listedAchievements = await CoreServices.ListAllAchievements(
    sortBy: AchievementsSortField.PercentageAchieved,
    sortDirection: SortDirection.Descending
);

foreach (var listedAchievement in listedAchievements)
{
    Debug.Log($"Achievement {listedAchievement.DisplayName} retrieved.");
    /* ... */
}

Getting achievements acquired by a player

To get achievement acquired by a player, use the GetPlayerAchievements method in Player.

There are a few key parameters to take note of:

  • string? query: A full-text search query, either by the display name, short description or detailed description.

  • int pageSize: The number of achievements to retrieve at a time. Defaults to 30, maximum 30.

  • string? groupName: The group ID to get achievements from.

A cursor (instance of PlayerAchievementsCursor) is returned, and you can use the HasNext attribute of the cursor to determine if the pagination has reached the last page, and use the Next() method to retrieve the next page. Each page is a list of PlayerAchievement instances, containing information such as the achievement ID, achieved date and time, display name, cover URL and short description.

Example - Get player achievements with pagination

var cursor = CoreServices.CurrentPlayer.GetPlayerAchievements(
    "Gold",
    groupName: "group1"
);

while (cursor.HasNext) {
    var achievements = await cursor.Next();
    foreach (var achievement in achievements)
    {
        Debug.Log($"Achievement ${achievement.DisplayName} retrieved.");
        /* ... */
    }
}

However, if you would like to retrieve every player achievement with one method call, you can list the brief information of all achievements instead, using the ListAllPlayerAchievements method in Player. The parameters are the same as GetPlayerAchievements; however, querying achievements is unavailable.

This will return a list of ListedPlayerAchievement instances, containing only the achievement ID, and achieved date and time.

Example - List all core player achievement information

var listedAchievements = await CoreServices.CurrentPlayer.ListAllPlayerAchievements(
    groupName: "group1"
);

foreach (var listedAchievement in listedAchievements)
{
    Debug.Log($"Achieved date: {listedAchievement.AchievedDate}");
}

Getting detailed information about an achievement

You can call the GetDetailedAchievement method in any achievement class, including Achievement and ListedAchievement, to get the detailed information, such as the detailed description.

var detailedSampleAchievement = await sampleAchievement.GetDetailedAchievement();
Debug.Log($"Achievement: {detailedSampleAchievement.DisplayName}, Detailed Description: {detailedSampleAchievement.DetailedDescription}");

Last updated