How to get authenticated player's high score form leaderboard ( Game Center )
Asked Answered
S

4

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);
            } 
        }];
    }
}
Sapheaded answered 9/6, 2011 at 14:15 Comment(1)
Through a lot of search, I think it's hasn't a way to do this..Sapheaded
F
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];
        }
    }];
}
Floozy answered 16/6, 2011 at 20:20 Comment(2)
What would be the interface of that delegate method being called?Stumper
Just a heads up: We have to set leaderboardRequest.identifier = @"myleaderboardidentifier"; before calling loadScores...Hus
A
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 ) ;
      }] ;
    }
  }] ;
}
Aldas answered 27/10, 2013 at 18:13 Comment(0)
C
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];
Chalmer answered 15/12, 2011 at 20:14 Comment(0)
B
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
        }
    })
}
Bolyard answered 20/11, 2017 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.