2

I have some incremental achievements on my game and i want to check if one of them is already unlocked. How can i do that?

Originaly i used this method:

getGamesClient().incrementAchievement(achievement, increment);

Then i tried to use this:

mHelper.getGamesClient().incrementAchievementImmediate(new OnAchievementUpdatedListener()
{
    @Override
    public void onAchievementUpdated(int statusCode, String achievementId)
    {
        // TODO: Check if the achievement got unlocked
    }
}, achievement, increment);

Is this the right way to do what i want? Is there a better way?

I only got two statusCode values on my tests.

Value: 5 = STATUS_NETWORK_ERROR_OPERATION_DEFERRED

Value: 0 = STATUS_OK

Can some one help me with this?

Thanks

1 Answers1

9

Declare this inner class:

class AchievementClass implements ResultCallback<Achievements.LoadAchievementsResult> {

    @Override
    public void onResult(LoadAchievementsResult arg0) {
        Achievement ach;
        AchievementBuffer aBuffer = arg0.getAchievements();
        Iterator<Achievement> aIterator = aBuffer.iterator();

        while (aIterator.hasNext()) {
            ach = aIterator.next();
            if ("The Achievement Id you are checking".equals(ach.getAchievementId())) {
                if (ach.getState() == Achievement.STATE_UNLOCKED) {
                    // it is unlocked
                } else {
                    //it is not unlocked
                }
                aBuffer.close();
                break;
            }
        }
    }       
}

And use it like so:

Games.Achievements.load(ApiClient, false).setResultCallback(new AchievementClass());

This will get achievement data for the currently signed in player, go through all the achievements, and when it reaches the achievement you're concerned with, it will check if it is unlocked.

Ogen
  • 6,499
  • 7
  • 58
  • 124