Search code examples
androidandroid-studiogoogle-play-servicesachievements

Add XP to Android game app


I have made a gaming app and I now want to add google achievement xp to my app how should I do that I have added achievements using this guide but there is no guide on internet that I can find that should add player's earned xp.

Here is my code to unlock achievement but its not unlocking xp :

void giveAchievements(int counter) {
    if (counter == 10) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_first_10_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-  eXOwZsFEAIQBg", counter);
        }
    }
    if (counter == 50) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_50_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-eXOwZsFEAIQBg", counter);
        }
    }
    if (counter == 100) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_100_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 250) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_250_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-eXOwZsFEAIQBg", counter);
        }
    }
    if (counter == 500) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_500_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 1000) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_1000_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 1500) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_1500_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 3000) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_3000_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 5000) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_5000_clicks));
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
}

Solution

  • The XP isn't based on your game it's based on your Google+ Profile. Let's say you unlock an achievement in the game Orc Genocide and you get 500xp to your Google+ profile. After that you unlock an achievement in your game and earn 1000xp. So your Google+ profile has 1500xp in total.

    But you can easily calculate the XP by yourself and store it in SharedPreferences :)

    void giveAchievements(int counter) {
        if (counter == 10) {   
            int xp = getXp();
            xp += 500;
            saveXp(xp);
            if(getApiClient().isConnected()) {
                Games.Achievements.unlock(getApiClient(), getString(R.string.achievement_first_10_clicks));   
                Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-  eXOwZsFEAIQBg", counter);
            }
        }
    }
    
    private int getXp() {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        return sharedPref.getInt("xp", 0);
    }
    
    private void saveXp(int xp) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("xp", xp);
        editor.apply();
    }
    

    https://support.google.com/googleplay/answer/3129940?hl=en

    When you get in-game achievements, you can earn experience points (XP) and levels on your Play Games profile.

    When you're playing a game, you'll see a notification when you've earned XP or enough points to level up your Play Games profile. When you level up, you'll also see a notification in your mobile device's notification shade near the top of your screen and your Play Games Inbox menu.

    You can view your XP and individual achievements on games you use with Play Games on your Play Games profile page.

    For more information on how to make your Game Profile public or private, go to Play games on your device.