Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
            } 
        }];
    }
}
like image 557
pixeloverflow Avatar asked Jun 09 '11 14:06

pixeloverflow


People also ask

How do you authenticate Game Center?

Game Center also checks whether you configured your game for Game Center. To authenticate the user, set the authentication handler ( authenticateHandler ) on the shared instance of GKLocalPlayer that represents the player of your game as in: GKLocalPlayer. local.

How do you use Game Center on iPhone?

On your iPhone, iPad, or iPod touchOpen Settings. Scroll to Game Center, then tap it. Tap the Nickname field to enter a name that your friends will see when you play games together. If you can't think of a name, you can choose one of the randomly generated suggestions.


2 Answers

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];
        }
    }];
}
like image 124
Orkun Ozbek Avatar answered Oct 20 '22 17:10

Orkun Ozbek


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 ) ;
      }] ;
    }
  }] ;
}
like image 26
bobobobo Avatar answered Oct 20 '22 18:10

bobobobo