How to get local player score from Game Center
Asked Answered
D

3

7

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?

Dunno answered 5/2, 2014 at 23:46 Comment(1)
Use a Protocol to pass you local player score. :-)Exum
C
6

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
 }];
Cushing answered 2/3, 2014 at 20:46 Comment(0)
G
3

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];
Gdynia answered 23/9, 2014 at 13:42 Comment(0)
L
1

The leaderboard request completes after the return of your method. This means that you are returning a null string.

The method you put the leaderboard request in should be purely for sending the request. The method will be finished executing before the leaderboard request is complete thus your "score = [NSString stringWithFormat:@"%lld", scoreInt];" line is being executed AFTER the return of the "score" which is null until that line is executed.

The solution is to not return the outcome of the completion handler using the method that sends the request. The score is definitely being retrieved correctly, so just do whatever you need to with the score inside of the completion handler. You have no way to know when the completion handler is going to be executed. This, in fact, is the reason why Apple allows you to store the code to be executed in a block! Although, it can be confusing to understand how to work with blocks that will definitely be executed later, or in your situation, sometime after the method is returning.

The best way to handle your situation is to not return anything in this method and just use the "score" variable as you intend to after the block has set score to a non-null value!

Last answered 19/3, 2016 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.