9

I need to retrieve authenticated player's submited score from Game Center. I use this code to get the score, but it just gets the top score (best score of the leaderboard not the specified player's score). How can I retrieve the authenticated player's score?

- (void) retrievePlayersScore {
    GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init]; 
    if (leaderboardRequest != nil) {
        leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal; 
        leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime; 
        leaderboardRequest.range = NSMakeRange(1,1);
        [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
            if (error != nil) {
                // handle the error. if (scores != nil)
            }
            if (scores != nil){
                // process the score information.
                CCLOG(@"My Score: %d", ((GKScore*)[scores objectAtIndex:0]).value);
            } 
        }];
    }
}
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
pixeloverflow
  • 579
  • 1
  • 5
  • 23

4 Answers4

10

You can use the following code:

GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];

leaderboardRequest.identifier = _leaderboardIdentifier;

if (leaderboardRequest != nil) {
    [leaderboardRequest loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error){
        if (error != nil) {
            //Handle error
        }
        else{
            [delegate onLocalPlayerScoreReceived:leaderboardRequest.localPlayerScore];
        }
    }];
}
Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76
Orkun Ozbek
  • 116
  • 1
  • 2
6

You just have to hit loadScoresWithCompletionHandler for a given GKLeaderboard, then automatically board.localPlayerScore will be filled out for that board.

So for example,

- (void) getLoadLeaderboardPositions
{
  [GKLeaderboard loadLeaderboardsWithCompletionHandler:^(NSArray *leaderboards, NSError *nsError) {
    if( nsError != nil )
    {
      error( nsError, "get leaderboard score" ) ;
      return ;
    }

    for( GKLeaderboard* board in leaderboards )
    {
      // fetch score for minimum amt of data, b/c must call `loadScore..` to get MY score.
      board.playerScope = GKLeaderboardPlayerScopeFriendsOnly ;
      board.timeScope = GKLeaderboardTimeScopeAllTime ;

      NSRange range = {.location = 1, .length = 1};
      board.range = range ;

      [board loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
        printf( "YOUR SCORE ON BOARD %s WAS %lld\n", [board.title UTF8String], board.localPlayerScore.value ) ;
      }] ;
    }
  }] ;
}
bobobobo
  • 64,917
  • 62
  • 258
  • 363
5

You may also try to initiate the leader board by using an array of player id(s) in order to narrow the number of players:

GKLeaderboard *board = [[[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:myGCPlayerID]] autorelease];
brigosx
  • 96
  • 1
  • 2
3

Updated version using Swift

let localPlayer = GKLocalPlayer.localPlayer()

if localPlayer.isAuthenticated {
    let leaderboard = GKLeaderboard(players: [localPlayer])
    leaderboard.identifier = LEADERBOARD_ID
    leaderboard.timeScope = .allTime
    leaderboard.loadScores(completionHandler: {
        (scores, error) in

        let bestScore = scores?.first?.value

        if bestScore != nil {
            // Do something with bestScore
        }
    })
}
Ehtesham Hasan
  • 4,133
  • 2
  • 23
  • 31