Can I query the Game Center APIs from C++, without Objective-C?

| | August 4, 2015

I am writing a game that needs Game Center leaderboards. These however, are not very extensive leaderboards. I just need the ability to submit a score automatically when the player completes a level. Then, when a player visits a specific in game screen, it queries his/her position on the leaderboard and returns the value and the position. I don’t need the UI, or any achievements. Just need those values.

Is this information possible to query using C++? I am using Cocos2d-x and would like to keep all of it C++ if possible.

One Response to “Can I query the Game Center APIs from C++, without Objective-C?”

  1. Is this information possible to query using C++?

    Not practically, no. Apple’s Game Center API is an Objective-C one. Technically speaking, you could make a bunch of bare calls to objc_msgSend if you were hell-bent on avoiding Objective-C code, but this is not practical. Instead, you can create a simple wrapper for the relevant Game Center calls by creating a C++ header file:

    // GameCenterAccess.h
    #pragma once
    void submitPlayerScore (int score);
    int getPlayerScore ();

    and implement those functions in an Objective-C++ file (one that ends with the .mm extension; you can use a regular .m file if you aren’t going to do anything C++-specific in the header, such as use namespaces):

    #include "GameCenterAccess.h"
    void submitPlayerScore (int score) {
      GKScore * scoreObject = [[GKScore alloc] initWithLeaderboardIdentifier:/* your leaderboard id*/];
      scoreObject.value = score;
      [GKScore reportScores:@[scoreObject] withCompletionHandler:^(NSError * error) {
        ... // scores reported
    int getPlayerScore () {
      GKLeaderboard * board = [[GKLeaderboard alloc] init];
      [board loadScoresWithCompletionHandler:^(NSArray * scores, NSError * error) {
        ... // do something with board.localPlayerScore

    and so on (the above code is not tested, but should be a reasonable-enough approximation of reality; you’ll want to do something smarter with the asynchronous nature of the queries).

    You can use the leaderboard API in the way you describe: simply store a simple score per player, and not bother with all the related UI and achievements (the act of supporting Game Center will make your game appear in the Game Center app and show all the “required” Game Center stuff that the OS forces on you, but you don’t need to do more than that). But you will need to use some portions of the API, and consequently to wrap them in some fashion in the above wrapper interface.

    For example, to transmit a score you’ll need the GKScore API and the GKPlayer API to represent the player obtaining the score, as well as the GKLeaderboard itself.

Leave a Reply