CodingNub
CodingNub

Reputation: 303

Easiest way to integrate an offline Leaderboard [LibGDX]

Was just wondering what the easiest way is to integrate an offline leaderboard that keeps a score saves. For example lets say score++ gives an extra point to int score, would you just make another int highScore with an if (score > highScore){ highScore = score } or some weird stuff

Upvotes: 0

Views: 434

Answers (1)

noone
noone

Reputation: 19776

The easiest way with LibGDX to implement an offline Leaderboard would be to use the cross-platform Preferences.

Preferences prefs = Gdx.app.getPreferences("leaderboard");
Integer score = prefs.getInteger("highscore", 0); // if there is no highscore yet, the score will be 0

// gameplay logic...
Integer newScore = 1337;

if (newScore > score) {
    prefs.putInteger("highscore", score);
}

That's it. It should work on at least Desktop, Android and iOS and will be persisted so it can be retrieved also when the application is restarted.

Upvotes: 3

Related Questions