blachniet
blachniet

Reputation: 4553

Is it possible to do a running hi-score leaderboard in GameKit?

I want a leaderboard in GameKit that shows my total accumulated score over multiple plays, not just a score from a single play. Is there any way to do this?

In other words, a single player would never have more than one entry to their name in the leaderboard. Think total experienced earned, or lifetime number of headshots. You're not going to have an entry in that leaderboard for when you had 4 headshots, and then when you had 20 headshots. You would just have the one entry for 20 lifetime headshots.

Upvotes: 0

Views: 638

Answers (2)

Scott K.
Scott K.

Reputation: 1771

You can do this but you have to manage it yourself. GameKit is oriented around a single-game high score, not cumulative scores. Note that a player won't appear more than once on the leader board because GKLeaderboard will only report back the highest score in the time range you specified.

Tracking a cumulative score isn't that hard. Here's some code I'm using to do this. Get the GKLeaderboard with the ID that is tracking the score, and then fetch the high score for the local user. Then, add the new total to the current high score and then report the new total.

- (void)updateLeaderboardWithID:(NSString *)identifier score:(int64_t)score {
    GKLeaderboard* leaderBoard= [[GKLeaderboard alloc] init];
    leaderBoard.category = identifier;
    leaderBoard.timeScope = GKLeaderboardTimeScopeAllTime;
    leaderBoard.range = NSMakeRange(1, 1);

    [leaderBoard loadScoresWithCompletionHandler:  ^(NSArray *scores, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^(void) {
                GKScore *localScore = leaderBoard.localPlayerScore;
                int64_t newValue = localScore.value + score;
                localScore = [[GKScore alloc] initWithCategory:identifier];
                localScore.value = newValue;
                [localScore reportScoreWithCompletionHandler:^(NSError *error){
                    // error handling goes here
                }];
            );
    }];
}

Upvotes: 3

Gajendrasinh Chauhan
Gajendrasinh Chauhan

Reputation: 3397

Yes,It may be possible. But you have to read apple documentary.This is all about GKLeaderBoardViewController class. You can use GKLeaderBoardViewController class for counting Highest Score.

http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKLeaderboardViewController_Ref/Reference/Reference.html

Upvotes: 0

Related Questions