Question

I am new to Android App development. I want to retrieve user's score from Google Play Game service and i am using following code to get the high score but i do not have any knowledge how it returns the value and how to save it.

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.highscore), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC);

Saving it to int or string does not worked.

Was it helpful?

Solution 2

The method:

loadCurrentPlayerLeaderboardScore (GoogleApiClient apiClient, String leaderboardId, int span, int leaderboardCollection)

returns

PendingResult<Leaderboards.LoadPlayerScoreResult>

Then you must use the getScore() method of the Leaderboards.LoadPlayerScoreResult class to get it.

Please see these links...

The loadCurrentPlayerLeaderboardScore method

The LoadPlayerScoreResult in the PendingResult

EDIT: Here's how you can use it.

Games.Leaderboards.loadCurrentPlayerLeaderboardScore().setResultCallback(new ResultCallback<LoadPlayerScoreResult>() {

            @Override
            public void onResult(LoadPlayerScoreResult arg0) {
                LeaderboardScore c = arg0.getScore();
            }

        });

This is how you get the score.

OTHER TIPS

The complete parameters for loadCurrentPlayerLeaderboardScore is like below

     Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(),
            getString(R.string.word_attack_leaderboard),
            LeaderboardVariant.TIME_SPAN_ALL_TIME,
            LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
            new ResultCallback<LoadPlayerScoreResult>() {

                @Override
                public void onResult(LoadPlayerScoreResult arg0) {
                    LeaderboardScore c = arg0.getScore();
                    long score = c.getRawScore();
                }
             }

R.string.word_attack_leaderboard is leaderboards id which get from google play game service

4 years later I was having a similar problem with this situation. Some stuff has been deprecated and stuff no longer works. So for those of you who want to know how to do it now, in 2018... check this answer-

First you have to get the LeaderBoardClient with

mLeaderboardsClient = Games.getLeaderboardsClient(MainActivity.this, googleSignInAccount);

Next you can the score

mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(getString(R.string.leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
          .addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
              @Override
              public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
                  long score = 0L;
                  if (leaderboardScoreAnnotatedData != null) {
                      if (leaderboardScoreAnnotatedData.get() != null) {
                          score = leaderboardScoreAnnotatedData.get().getRawScore();
                          Toast.makeText(MainActivity.this, Long.toString(score), Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                      } else {
                          Toast.makeText(MainActivity.this, "no data at .get()", Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: .get() is null");
                      }
                  } else {
                      Toast.makeText(MainActivity.this, "no data...", Toast.LENGTH_SHORT).show();
                      Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                  }
              }
          })
          .addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception e) {
                  Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
                  Log.d(TAG, "LeaderBoard: FAILURE");
              }
  });

The 3 parameters to .loadCurrentPlayerLeaderboardScore are as follows

ID of the leaderboard to load the score from.

Time span to retrieve data for. Valid values are TIME_SPAN_DAILY, TIME_SPAN_WEEKLY, or TIME_SPAN_ALL_TIME.

The leaderboard collection to retrieve scores for. Valid values are either COLLECTION_PUBLIC or COLLECTION_SOCIAL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top