How to get user names in google play game services leaderboard
I need help in getting the player nickname of the players in the leaderboards. When I use loadScores, I only get the playerID (which is a string numbers), with this code:
PlayGamesPlatform.Instance.LoadScores (
<your_leaderboard_ID_here>,
LeaderboardStart.PlayerCentered,
1,
LeaderboardCollection.Public,
LeaderboardTimeSpan.AllTime,
(LeaderboardScoreData data) => {
Debug.Log (data.Valid);
Debug.Log (data.Id);
Debug.Log (data.PlayerScore);
Debug.Log (data.PlayerScore.userID);
Debug.Log (data.PlayerScore.formattedValue);
//1°
FirstNickName.text = data.Scores[0].userID;
//2°
SecondNickName.text = data.Scores[1].userID;
//3°
ThirdNickName.text = data.Scores[2].userID;
});
}
Work fine for all the player id's, all ranks and scores... and for the logged user name, but not for all player names in the leaderboard. I tried to use loadPlayers and use alias, it doesn't give me anything. I can successfully logged in in google play game services.
I need this like:
//1
FirstNickName.text = data.Scores[0].userName;
//2
SecondNickName.text = data.Scores[1].userName;
//3
ThirdNickName.text = data.Scores[2].userName;
etc...
Answer by Dawdlebird · Feb 24, 2020 at 03:27 PM
In case anyone is looking for this: it seems you'll need to do this in a separate step. Loadscores gives you a list of userID's, which you cast into "string[] userIDs " for example. Then use Social.LoadUsers(userIDS, =>...) to have something done with this info.
I've used this as follows (more or less:
// get scores
IScore[] scores = data.Scores;
// get user ids
string[] userIds = new string[scores.Length];
for (int i = 0; i < scores.Length; i++)
{
userIds[i] = scores[i].userID;
}
// forward scores with loaded profiles
Social.LoadUsers(userIds, profiles => DisplayLeaderboardEntries(scores, profiles));
The "DisplayLeaderboardEntries" will be called after users have been loaded, so you will have both scores(and ranks) and profile info (name, icon) available to display in whatever way you like.
Answer by hypercry · Dec 01, 2018 at 11:55 AM
I wonder how it was done. We wish you to respond as soon as possible ...
Answer by WILEz1975 · Sep 21, 2019 at 02:28 PM
It has been done. But many years have passed by now and I don't remember how I did it. No answer had here :(
Answer by thedetective456 · Oct 02, 2020 at 05:58 AM
For anyone wondering how to get user data from leaderboard. First authenticate the player and then load scores. Use
var lb = PlayGamesPlatform.Instance.CreateLeaderboard(); // Create leaderboard
lb.id = Leaderboards.PerfectLevels.ID; // assign ID to it
PlayGamesPlatform.Instance.LoadScores(
lb.id, // Leaderboard ID
LeaderboardStart.PlayerCentered, // Get from top scores or player centered
5, // number of rows of scores you want
LeaderboardCollection.Public, // public scores or social
LeaderboardTimeSpan.AllTime, // Time span
(LeaderboardScoreData data) => // callback with scores
{
for(int i = 0; i < data.Scores.Length; i++)
{
Debug.Log("Player User name : " + data.Scores[i].userID);
Debug.Log("Player score : " + data.Scores[i].formattedValue);
Debug.Log("Player Rank : " + data.Scores[i].rank.ToString());
}
}
);
@WILEz1975 did a mistake, in number of rows to return, he had passed 1 -> only player score was returning.
Also if you specifically need Player data, just use "data.PlayerScore" instead of a for loop and "data.Scores"
Hope this helps :)
Your answer
Follow this Question
Related Questions
Weekly Leader boards using google play services 0 Answers
Google Play Services leaderboard not updating after high score is beaten? 1 Answer
How to Open a Spesific LeaderBoard using Google Play Services 0 Answers
Google Play and Facebook leaderboards 0 Answers
google play get score from leaderboard 3 Answers