irakliy01
irakliy01

Reputation: 385

How to load scores in Unity?

I have a simple game published in Google Play. When player wins the game his score should be incremented in leaderboard. My problem is that when player wins the game, app stops. I think that problem is in PlayGamesPlatform.Instance.LoadScores because when I removed this part, no one had a problem. Also, I want to notice that not every player has such problem in game. This problem happens just to those, who haven't ever won the game (they have no score in leaderboard). So, PlayGamesPlatform.Instance.LoadScores works only to those who already have some score in leaderboard.

My script:

PlayGamesPlatform.Instance.LoadScores(GPGSIds.leaderboard_rating, GooglePlayGames.BasicApi.LeaderboardStart.PlayerCentered, 1, GooglePlayGames.BasicApi.LeaderboardCollection.Public, GooglePlayGames.BasicApi.LeaderboardTimeSpan.AllTime, (GooglePlayGames.BasicApi.LeaderboardScoreData data) =>
{
    long score;
    if (long.TryParse(data.PlayerScore.formattedValue, out score))
        Social.ReportScore(score + 50, GPGSIds.leaderboard_rating, (bool success) => { });
    else
        Social.ReportScore(50, GPGSIds.leaderboard_rating, (bool success) => { });
});

Upvotes: 2

Views: 2376

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

Try this google sample from github

Using PlayGamesPlatform.LoadScores()

This method uses the PlayGamesPlatform directly. This approach provides additional flexibility and information when accessing the leaderboard data.

 PlayGamesPlatform.Instance.LoadScores(
            GPGSIds.leaderboard_leaders_in_smoketesting,
            LeaderboardStart.PlayerCentered,
            100,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (data) =>
            {
                mStatus = "Leaderboard data valid: " + data.Valid;
                mStatus += "\n approx:" +data.ApproximateCount + " have " + data.Scores.Length;
            });

The parameters for LoadScores() are:

leaderboardId start position (top scores or player centered) row count leaderboard collection (social or public) time span (daily, weekly, all-time) callback accepting a LeaderboardScoreData object. The LeaderboardScoreData class is used to return information back to the caller when loading scores. The members are: 1. Id - the leaderboard id 2. Valid - true if the returned data is valid (the call was successful) 3. Status - the ResponseStatus of the call 4. ApproximateCount - the approximate number of scores in the leaderboard 5. Title - the title of the leaderboard 6. PlayerScore - the score of the current player 7. Scores - the list of scores 8. PrevPageToken - a token that can be used to call LoadMoreScores() to get the previous page of scores. 9. NextPageToken - a token that can be used to call LoadMoreScores() to get the next page of scores.

void GetNextPage(LeaderboardScoreData data)
    {
        PlayGamesPlatform.Instance.LoadMoreScores(data.NextPageToken, 10,
            (results) =>
            {
                mStatus = "Leaderboard data valid: " + data.Valid;
                mStatus += "\n approx:" +data.ApproximateCount + " have " + data.Scores.Length;
            });
    }

Additional reference How to get the user score value from Google Play Services Leaderboard.

P.S.:

Try setting the scores of all players to default 0 so players who havent played yet can make calls to loadScores.

Upvotes: 1

Related Questions