Click here to Skip to main content
Click here to Skip to main content
Go to top

Android Practical Programming: Implementing Google Play Achievement Features within Your Game

, 30 Aug 2014
Rate this:
Please Sign up or sign in to vote.
This article discusses the basic steps on how to implement Google Play achievement features within your game.

Introduction

An achievement can be a trophy, medal, badge, stamp or award in video games. With Google Play powered achievement features, users can easily track their high score, invite friends to play games in group, compare their accomplishments, and even engage in light-hearted competitions.
This article discusses the basic steps on how to implement Google Play achievement features within your game.

Adding achievements on Google Play

To integrate Google Play achievement features into your app, first make sure you already own a Google Play developer account, then follow the steps below to set up Google Play achievements for your app.
Sign in to your Google Play developer console, click the “Game Services” icon in the left sidebar, add the description for your app in the “Game Details” section as shown in figure 1.


 
Figure 1 Game details


Next, in the “Achievements” section, add new achievements for your app (figure 2).

 

 
Figure 2 New achievement

Google Play asks you to add at least 5 achievements for your app. As outlined in table 1, we added 6 achievements for our project X Shuttle (Android Games: Practical Programming by Example, Quickstart 2).

 

Table 1 List of achievements

Then, copy the Achievement IDs from the Achievements page (figure 3) to the Achievement ID section of string.xml.

<string name="achieve_id1">CgkI_97ls5wCEAIQAA</string>
<string name="achieve_id2">CgkI_97ls5wCEAIQAQ</string>
<string name="achieve_id3">CgkI_97ls5wCEAIQAg</string>
<string name="achieve_id4">CgkI_97ls5wCEAIQAw</string>
<string name="achieve_id5">CgkI_97ls5wCEAIQBA</string>
<string name="achieve_id6">CgkI_97ls5wCEAIQBQ</string>

 
Figure 3 Achievements page

Last, click the “Testing” section in the developer console to add testers for your app.

Designing table structure

To store the achievements record locally, we introduce a table named Achievements here.
This table contains 3 columns: Id (INTEGER), AchieveId (INTEGER), and Step (INTEGER).


Table 2 Achievements table

Integrating achievements APIs

Once the user has signed in and the GoogleApiClient is connected, your game can start using the achievements APIs. Otherwise, warn the user that he/she must sign in to use this feature. It will start the asynchronous sign in flow by calling the method below if the user clicks on the sign-in button.

beginUserInitiatedSignIn();

To unlock a single achievement, i.e. only one step is required to unlock it, call the unlock() method and pass in the achievement Id.

Games.Achievements.unlock(getApiClient(), getString(achievement_id));

In contrast, to unlock an incremental achievement, i.e. several steps are required to unlock it, call the increment() method below instead.

Games.Achievements.increment(getApiClient(), getString(incremental_achievement_id),1);

As Google Play services will automatically unlock the specified achievement once it reaches the predefined number of steps, you do not need to write additional snippet to unlock the achievement. Afterwards call startActivityForResult() to show a user's achievements.
The final integration code implemented in the project X Shuttle reads as follows:

//View archievements
protected void viewAchieve(){    
    try {            
        if(!isSignedIn()) {
            // alternative implementation (or warn user that they must sign in to use this feature)
            // start the asynchronous sign in flow
            beginUserInitiatedSignIn();            
        }
        else {
            if(level != lockLevel) {    
                int curid = 0;
                int curstep = 0;
                int lastid = readAchieveId();
                int laststep = readAchieveStep();
            }
                    
            //Invokes Google Play Games services API method
            if(lastid == 0) {
                Games.Achievements.unlock(getApiClient(), getString(ACHIEVEMENT_IDS[0]));
            }
            else if(!(lastid == 6 && laststep == 30)) {
                Games.Achievements.increment(getApiClient(), getString(ACHIEVEMENT_IDS[curid-1]),1);    
            }
            saveAchievement(curid, curstep);
            lockLevel = level;
        }
                
        //Displays achievements
        startActivityForResult(Games.Achievements.getAchievementsIntent(
                               getApiClient()), REQUEST_ACHIEVEMENTS);
    }        
    catch(Exception ex) {
        //ex.printStackTrace();           
    }         
}


Figure 4 and 5 are the screenshots of Google+ sign-in and unlock achievements captured when the player uploads high score onto Google Play.

 

 
Figure 4 Google+ sign-in

 
Figure 5 Unlock achievements

Summary

This article walked through the basic steps involved in implementing Google Play achievement features within your game.

Read more here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

esupersun
Team Leader MICBOTS
Canada Canada
Fred Yang is the lead developer at Micbots (micbots.com), a Vancouver based game studio that produces fun and attractive mobile games on market for all to enjoy. So far the released titles include Sokoban Deluxe, Lost Warrior, Crocodile Island, Ozman Quest, Temple of Arceus, and Nezeh Series.
As an active member of the game industry, he has contributed numerous brilliant articles to Apphex Forums (apphex.com), a top mobile game developers forum, covering comprehensive technical discussions and cutting-edge programming tutorials on Android/iOS game development.
Fred holds a Ph.D. in Applied Mathematics and is also an inspiring mentor for aspiring programmers and artists trying to tap into the game industry.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 30 Aug 2014
Article Copyright 2014 by esupersun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid