Get player nicknames for leaderboard - PlayFab

495 views Asked by At

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:

enter image description here

Are there any solutions for fast loading leaderboards?

2

There are 2 answers

0
RedStone On BEST ANSWER

I don't know if this is the best way. The reason that the necessary data is not coming out seems to be because the leaderboard was not properly received.

GetLeaderboardResult _leaderBoard;

IEnumerator GetLeaderBoardData()
{
    int getLeaderBoard = 1;
    PlayFabClientAPI.GetLeaderboard(new GetLeaderboardRequest()
        {
            StartPosition = /*Some Value Setting*/,
            StatisticName = /*Some Value Setting*/,
            MaxResultsCount = /*Some Value Setting*/,
            ProfileConstraints =
            new PlayerProfileViewConstraints()
            {
                ShowLocations = true,
                ShowDisplayName = true
            }
        }, (result)=>
        {
            _leaderBoard = result;
            getLeaderBoard -= 1;
        }, (error)=>{});
    yield return new WaitWhile(()=>!(getLeaderBoard <= 0));
}

If you do as above, the appropriate leaderboard data will be put in a variable called '_leaderBoard'. If there is another way to asynchronously wait for a value to come in, you can use that method.

Hope this helps you. :)

1
Ilya On

@RedStone's solution gave me the idea to move this line from GetPlayerProfile() to my GetLeaderboard() function

ProfileConstraints = new PlayerProfileViewConstraints() {
    ShowDisplayName = true
}

The GetPlayerProfile() function is no longer needed, it can be removed

And in the end I got this:

public void GetLeaderboard()
{
    Leaders = "";
    var request = new GetLeaderboardRequest
    {
        StatisticName = "BestScore",
        StartPosition = 0,
        MaxResultsCount = 10,
        ProfileConstraints = new PlayerProfileViewConstraints() {
            ShowDisplayName = true
        }
    };
    PlayFabClientAPI.GetLeaderboard(request, OnLeaderboardGet, OnError);
}


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

    LeaderboardText.text = Leaders.ToString();
}

So far, only the second player has a nickname, so this is a working solution

enter image description here