Documentation

Did you find this helpful?

Using the Profile for Advanced Leaderboards

With PlayFab, you usually construct Leaderboards using the following API methods:

The result is a list of PlayerLeaderboardEntry that contains only basic information about the Player and his relation to the current leaderboard. PlayFab allows you to use PlayerProfileViewConstraints to gain additional information about each player.

Note: This example assumes you already have some leaderboard data to play with. Please refer to our Archived Tournaments guide for a method to generate some test data.

Configuring Player Profile View Constraints

By default, client API may only fetch DisplayName from another player's profile. In this example we will allow all players to access additional information about other players in the leaderboard.

Navigate to your title game manager. Navigate to "Settings" left-menu (1), then to "Client Profile Options" tab (2). Make sure "DisplayName" (3) and "Avatar URL" (4) properties are selected. Submit by clicking "Save Client Profile Options" (5):

Testing

The previous step allows client code to use "DisplayName" and "AvatarUrl" profile constraints. The next sample shows how to fetch and print a Leaderboard using any mentioned profile data. Please refer to the comments for further information:

private static async Task DoReadLeaderboard()
{
    // Get Leaderboard Request
    var result = await PlayFabClientAPI.GetLeaderboardAsync(new GetLeaderboardRequest()
    {
        // Specify your statistic name here
        StatisticName = "TestScore",
        // Override Player Profile View Constraints and fetch player DisplayName and AvatarUrl
        ProfileConstraints = new PlayerProfileViewConstraints()
        {
            ShowDisplayName = true,
            ShowAvatarUrl = true
        }
    });

    // Start printing the leaderboard
    Console.WriteLine("=== LEADERBOARD ===");

    if (result.Error != null)
    {
        // Handle error if any
        Console.WriteLine(result.Error.GenerateErrorReport());
    }
    else
    {
        // Traverse the leaderboard list
        foreach (var entry in result.Result.Leaderboard)
        {
            // Print regular leaderboard entry information
            Console.WriteLine($"{entry.Position + 1} {entry.PlayFabId} {entry.StatValue}");

            // Additionally print display name and avatar url that comes from player profile
            Console.WriteLine($"    {entry.Profile.DisplayName} | {entry.Profile.AvatarUrl}");
        }
    }
}

Note that the individual profile fields are only available if the client explicitly asks for them using Profile Constraints. Note also that if certain profile constraint are not allowed in the Game Manager and your client requests them, the API call will fail with a corresponding error. The result will look similar to this:

Using this technique, you can fetch a handful of profile information including other statistic values.



Did you find this helpful?