- Home /
The question is answered, right answer was accepted
Google Play Services leaderboard - player rank always 0
Hello,
I have an issue with the Google Play Services Leaderboard and I am stuck for days already.
I am using the Play Games Plugin for Unity https://github.com/playgameservices/play-games-plugin-for-unity.
I am trying to get the Userrank for a specific level. Therefore I am loading the Scores via the LoadScores Function and this seems to work fine.
The issue seems to be, that I get a correct rank in the "LoadScores" Function but the overall GetUserRank Function is still returning rank = 0, no matter what the actual Userrank is.
My guess is that this is a timing issue after all and that the rank is returned before the LoadScores Function has calculated the correct rank to return.
public int GetUserRank(string levelID)
{
int rank = 0;
string user = PlayGamesPlatform.Instance.localUser.id;
string leaderboard = ReturnLeaderboard(levelID);
Social.LoadScores(leaderboard, scores =>
{
if (scores.Length > 0)
{
Debug.Log("Retrieved " + scores.Length + " scores");
//Filter the score with the user name
for (int i = 0; i < scores.Length; i++)
{
if (user == scores[i].userID)
{
rank = scores[i].rank;
// This prints out the actual rank of the user and seems to work
print("Rank in LoadScoresFunction: " + rank);
break;
}
}
}
else
Debug.Log("Failed to retrieved score");
});
//Here the rank is always 0, no matter the outcome of the LoadScores Function above
print("Rank in PlayGamesController: " + rank);
return rank;
}
I reference this function from other scripts via:
int rank = PlayGamesController.Instance.GetUserRank(levelID);
Any ideas/hints greatly appreciated.
Thanks!
If someone stumbles upon this question: The problem seems to be that my function is executed async. I had to work with a callback and a lambda expression to solve this, as someone on stack overflow suggested: https://stackoverflow.com/questions/67657693/unity-problem-with-google-play-services-leaderboard-get-user-rank