Jonny
Jonny

Reputation: 16338

Retrieve all scores in a GKLeaderboard

How can I retrieve all scores for a GKLeaderboard? (I'm making my own leaderboard graphics). I'd need to know the number of scores in a certain leaderboard, but there seems to be no way of polling GC for that kind of information?

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html#//apple_ref/doc/uid/TP40008304-CH6-SW14

Upvotes: 1

Views: 1834

Answers (4)

Benzy Neez
Benzy Neez

Reputation: 21785

I see that this question is over 10 years old now, so interfaces will have changed quite a lot in that time. I was interested in loading a set of scores from a leaderboard in iOS 14+ with the hope of being able to display the scores around the local player's own score, like how GKGameCenterViewController does it. There are two reasons for wanting to do this:

  1. As a workaround for the dark leaderboard issue reported in GKGameCenterViewController showing as black text on dark background on iPads running iPadOS 16
  2. To allow the context for a score to be used to enrich the information displayed.

After examining the (non-deprecated) functions of GKLeaderboard, I came to the following conclusions:

  • The function loadEntries(for:timeScope:range:completionHandler:) would seem to be the best function available.
  • The completion handler receives the totalPlayerCount as third parameter. This provides an answer to the original 10-year-old question of how to get the number of scores in a certain leaderboard!
  • But... the range only allows scores in the top 100 to be fetched (min value 1, max value 100). See comments

I tried supplying a range which broke the min/max rules and it caused a crash.

There is another loadEntries function which lets you fetch scores for other players that you can identify. This could be used to fetch the scores of friends, but otherwise it doesn't help much.

So for iOS14+, the bottom line is: although it is at least possible to determine how many scores are in a leaderboard, it seems you can only fetch the local player's own score and scores in the top 100. See comments

Upvotes: 0

Jellyjoey
Jellyjoey

Reputation: 188

In order to get all the scores, you need to request them in chunks using the range that Scar details. Typically in games you display a subset of say 100, and give the user the option to see a lower subset or a higher subset.

Upvotes: 0

Jonny
Jonny

Reputation: 16338

I'm getting the feeling the answer is the maxRange property of GKLeaderboard. The docs:

This property is invalid until a call to loadScoresWithCompletionHandler: is completed. Afterward, it contains the total number of entries available to return to your game given the filters you applied to the query.

http://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboard_Ref/Reference/Reference.html#//apple_ref/occ/instp/GKLeaderboard/maxRange

So this would be the number of rows in a leaderboard table view.

Upvotes: 1

Scar
Scar

Reputation: 3480

In the apple document, there is a section called Retrieving Leaderboard Scores, they use an example of how to retrive a top 10 score:

- (void) retrieveTopTenScores
{
    GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
    if (leaderboardRequest != nil)
    {
        leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
        leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
        leaderboardRequest.range = NSMakeRange(1,10);
        [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
            if (error != nil)
            {
                // handle the error.
            }
            if (scores != nil)
            {
                // process the score information.
            }
            }];
    }
}

We can edit the range of the leaderboardRequest, to retrive more scores, apple said in there document:

range

The numerical score rankings to return from the search.

@property(nonatomic, assign) NSRange range

Discussion The range

property is ignored if the leaderboard request was initialized using the initWithPlayerIDs: method. Otherwise, the range property is used to filter which scores are returned to your game. For example, if you specified a range of [1,10], after the search is complete, your game receives the top ten scores. The default range is [1,25].

The minimum index is 1. The maximum length is 100.

Upvotes: 1

Related Questions