question

369ilya369369 avatar image
369ilya369369 asked

Get player nicknames for leaderboard

I have a script that gets the username by PlayFab id and writes it to the variable TestName

It was taken (and slightly modified) from the original documentation: https://learn.microsoft.com/en-us/gaming/playfab/features/data/playerdata/getting-player-profiles

// Initially TestName not set

void GetPlayerProfile(string playFabId) {
    PlayFabClientAPI.GetPlayerProfile( new GetPlayerProfileRequest() {
        PlayFabId = playFabId,
        ProfileConstraints = new PlayerProfileViewConstraints() {
            ShowDisplayName = true
        }
    },
    result => TestName = result.PlayerProfile.DisplayName,
    error => Debug.LogError(error.GenerateErrorReport()));
}

Next, I have a script that takes an array of leaderboard data and writes it to the Leaders variable in a user-friendly format

void OnLeaderboardGet(GetLeaderboardResult result)
{
    foreach (var item in result.Leaderboard)
    {
        GetPlayerProfile(item.PlayFabId);
        Debug.Log(TestName);
        Leaders += ((item.Position + 1) + ") " + TestName + ": " + item.StatValue + "\n");
    }

    LeaderboardText.text = Leaders.ToString();
    Debug.Log(Leaders);
}

The problem occurs in the first function GetPlayerProfile(item.PlayFabId);. The function receives a username with a delay and does not have time to overwrite the variable TestName

As a result I get this:

Are there solutions to download leaderboards with nicknames?

,

I have a script that gets the username by PlayFab id and writes it to the variable TestName

It was taken (and slightly modified) from the original documentation: https://learn.microsoft.com/en-us/gaming/playfab/features/data/playerdata/getting-player-profiles

// Initially TestName not set

void GetPlayerProfile(string playFabId) {
    PlayFabClientAPI.GetPlayerProfile( new GetPlayerProfileRequest() {
        PlayFabId = playFabId,
        ProfileConstraints = new PlayerProfileViewConstraints() {
            ShowDisplayName = true
        }
    },
    result => TestName = result.PlayerProfile.DisplayName,
    error => Debug.LogError(error.GenerateErrorReport()));
}

Next, I have a script that takes an array of leaderboard data and writes it to the Leaders variable in a user-friendly format

void OnLeaderboardGet(GetLeaderboardResult result)
{
    foreach (var item in result.Leaderboard)
    {
        GetPlayerProfile(item.PlayFabId);
        Debug.Log(TestName);
        Leaders += ((item.Position + 1) + ") " + TestName + ": " + item.StatValue + "\n");
    }

    LeaderboardText.text = Leaders.ToString();
    Debug.Log(Leaders);
}

The problem occurs in the first function GetPlayerProfile(item.PlayFabId);. The function receives a username with a delay and does not have time to overwrite the variable TestName

As a result I get this:

Are there solutions to download leaderboards with nicknames?

Player DataLeaderboards and Statistics
screenshot-24.png (50.1 KiB)
screenshot-24.png (50.1 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

You can use Display Name as an alternative, since Player Data Management - Get Leaderboard - REST API (PlayFab Client) | Microsoft Learn can return the player’s Display Name, you don’t need to call additional APIs.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.