Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local player score from Game Center

How to get score of local player from Leaderboard Game Center? I tried this code, but it returns nothing. Anybody know how to solve it, or is there better way how to get score?

- (NSString*) getScore: (NSString*) leaderboardID
{
    __block NSString *score;
    GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
    if (leaderboardRequest != nil)
    {
        leaderboardRequest.identifier = leaderboardID;

        [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
            if (error != nil)
            {
                NSLog(@"%@", [error localizedDescription]);
            }
            if (scores != nil)
            {
                int64_t scoreInt = leaderboardRequest.localPlayerScore.value;
                score = [NSString stringWithFormat:@"%lld", scoreInt];
            }
        }];
    }
    return score;
}

I think, that method have to wait for completion of [leaderboardRequest loadScoresWithCompletionHandler: ...

Is it possible?

like image 945
user2374693 Avatar asked Feb 05 '14 23:02

user2374693


2 Answers

Your code appears to not have any bugs that I can see. I would recommend displaying the standard leaderboard interface to see if your code that reports the scores is actually working correctly. If so, you should see the scores in the leaderboard. The code below works in my game, and I know the score reporting is working properly because it shows in the default game center UI.

GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
leaderboardRequest.identifier = kLeaderboardCoinsEarnedID;
[leaderboardRequest loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else if (scores) {
    GKScore *localPlayerScore = leaderboardRequest.localPlayerScore;
    CCLOG(@"Local player's score: %lld", localPlayerScore.value);
    }
}];

If you aren't sure how, the code below should work to show the default leaderboard (iOS7):

 GKGameCenterViewController *gameCenterVC = [[GKGameCenterViewController alloc] init];
 gameCenterVC.viewState = GKGameCenterViewControllerStateLeaderboards;
 gameCenterVC.gameCenterDelegate = self;
 [self presentViewController:gameCenterVC animated:YES completion:^{
      // Code
 }];
like image 90
Corbin87 Avatar answered Oct 13 '22 00:10

Corbin87


You cannot return score outside the block. In this code first "return score" will be executed before the method "loadScoresWithCompletionHandler". Additionally you haven't set initial value for "score", this method will return completely random value. I suggest you to put your appropriate code inside the block, instead of:

int64_t scoreInt = leaderboardRequest.localPlayerScore.value;
score = [NSString stringWithFormat:@"%lld", scoreInt];
like image 38
pedro Avatar answered Oct 13 '22 00:10

pedro