Click here to Skip to main content
12,078,370 members (48,107 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.8K views
9 bookmarked
Posted

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

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

Introduction

Google Play provides cross-platform games services that let you easily integrate popular gaming features such as leaderboards, achievements, real-time multiplayer, and cloud save to your mobile games.
This tip discusses the basic steps on how to implement Google Play leaderboard features within your game.

Adding Leaderboards on Google Play

To integrate Google Play leaderboard 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 leaderboards for your app.
Sign in to your Google Play developer console, go to the “Leaderboards” section, add a new Leaderboard for your app as shown in figure 1.

<

Figure 1 New leaderboard

Then, copy the Leaderboard Id from the Leaderboards page to the Leaderboard Id section of string.xml.

<string name="leaderboard_id">CgkI_97ls5wCEAITRY</string>

Integrating Leaderboards APIs

Once the users has signed in and the GoogleApiClient is connected, your game can start using the leaderboards 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 submit the user’s score, call the submitScore() method and pass in the leaderboard Id.

Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_id), point);

To display a leaderboard, call the startActivityForResult() and pass in the intent to create the default leaderboard UI.

//Display leaderboard
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(),
                       getString(R.string.leaderboard_id)), REQUEST_LEADERBOARD);

The final integration code implemented in the project X Shuttle (Android Games: Practical Programming by Example, Quickstart 2) reads as follows:

//View leaderboard
protected void viewBoard(){    
    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 {
            //Invoke Google Play Games services API method
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_id), point);
                
            //Display leaderboard
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(),
                                   getString(R.string.leaderboard_id)), REQUEST_LEADERBOARD);
        }
    }
    catch(Exception ex) {
         //ex.printStackTrace();           
    }        
}

Figure 2 shows a screenshot of leaderboards captured when the player uploads his/her score onto Google Play.

Figure 2 Leaderboard screenshot

Summary

This tip walked through the basic steps involved in implementing Google Play leaderboard 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
Architect
Canada Canada
Fred Yang is a mentor for aspiring programmers and artists who are interested in breaking into the game industry.

You may also be interested in...

Comments and Discussions

 
Questionhow to integrate leaderboard Pin
Member 1106646128-Nov-14 1:49
memberMember 1106646128-Nov-14 1:49 
AnswerRe: how to integrate leaderboard Pin
esupersun28-Nov-14 13:52
memberesupersun28-Nov-14 13:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160212.1 | Last Updated 30 Aug 2014
Article Copyright 2014 by esupersun
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid