libgdxrobovmgame-center-leaderboard

Submitting score to GameCenter using RoboVM and libgdx


I am using libgdx and robovm.

I get an error when I try to submit the score to the leaderboard on GameCenter on iOS. I am able to show the leaderboard. This is the error I get:

*** Terminating app due to uncaught exception 'GKInvalidArgumentException', reason: 'A GKScore must specify a leaderboard.' libc++abi.dylib: terminating with uncaught exception of type NSException

It is similar to this SO-post, but it is objective-c so I dont understand the answer.

This is my code for showing the leaderboard (this works)

public void getLeaderboardGPGS() {

    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
        return;
    } 
    GKGameCenterViewController gameCenterView = new GKGameCenterViewController();

    gameCenterView.setGameCenterDelegate(new GKGameCenterControllerDelegateAdapter() {
        @Override
        public void didFinish (GKGameCenterViewController gameCenterViewController) {
            dismissViewControllerAndNotifyListener(gameCenterViewController, GKGameCenterViewControllerState.Leaderboards);
        }
    });
    gameCenterView.setViewState(GKGameCenterViewControllerState.Leaderboards);

    gameCenterView.setLeaderboardIdentifier(identifier);


    keyWindow.getRootViewController().presentViewController(gameCenterView, true, null);
}

This is my code for submiting the score (this does not work)

public void submitScoreGPGS(int score, MyLeaderBoardCallback callback) {

    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
        //listener.scoreReportFailed(buildUnauthenticatedPlayerError());
        Gdx.app.log("Gamecenter","Notlogedin");
        return;
    }
    GKScore scoreReporter = new GKScore(identifier);

    scoreReporter.setValue(score);
    scoreReporter.setLeaderboardIdentifier(identifier);
    //scoreReporter.setShouldSetDefaultLeaderboard(true);
    //scoreReporter.setContext(0);

    NSArray<GKScore> scores = new NSArray<GKScore>(scoreReporter);

    Gdx.app.log("Gamecenter","Report socre");
    GKScore.reportScores(scores, new VoidBlock1<NSError>() {
        @Override
        public void invoke (NSError error) {
            if (error != null) {
                Gdx.app.log("Gamecenter","scorereportfailed");
            } else {
                Gdx.app.log("Gamecenter","scorereportcompleted");
            }
        }
    });

}

Anyone knows what the problem might be? I have tried googleing but there is little information about "robovm and gamekit/gamecenter".


Solution

  • The variable that contained the leaderboard id was garbage collected for some reason. Had to hard-code the string at scoreReporter.setLeaderboardIdentifier("my hard coded id"); . Weird....