Reputation: 189
I'm trying to fetch the top score from a leader board using Google Play Games, however it doesn't return anything. Could someone point me in the right direction?
public int WorldRecord()
{
int topScore = 0;
PlayGamesPlatform.Instance.LoadScores(
GPGSIds.leaderboard_quick_fire_scores,
LeaderboardStart.TopScores, 1,
LeaderboardCollection.Public,
LeaderboardTimeSpan.AllTime,
(LeaderboardScoreData data) =>
{
topScore = (int)data.Scores[0].value;
});
return topScore;
}
Thanks
Upvotes: 1
Views: 158
Reputation: 90833
PlayGamesPlatform.Instance.LoadScores
runs asynchronous and executes the passed action after having finished.
Your method, however, doesn't wait until that request actually finished and thereby simply returns the default value 0
already before the LoadScores
actually finishes and provides a valid result.
You should probably rather use some kind of callback like e.g.
public void WorldRecord(Action<int> onResult)
{
PlayGamesPlatform.Instance.LoadScores(
GPGSIds.leaderboard_quick_fire_scores,
LeaderboardStart.TopScores, 1,
LeaderboardCollection.Public,
LeaderboardTimeSpan.AllTime,
(LeaderboardScoreData data) =>
{
onResult?.Invoke((int)data.Scores[0].value);
});
}
And then pass a callback what should happen when the result is ready like e.g.
WorldRecord(topScore =>
{
Debug.Log($"Top Score: {topScore}");
});
Upvotes: 2