google-play-storeAt the Google I/O conference last May, Google announced Google Play game services and began offering online game support similar to Apple’s GameCenter.

Corona Labs, working with Google, demonstrated live apps at Google I/O using these game services. And now, following on the heels of this demo, we’ve rolled out the first version of Google Play game services support in Corona SDK. At this time, the plugin supports leaderboards and achievements on Android, but we’re actively adding more features and we’ll soon offer iOS support as well.


Google Setup

If you’ve already implemented Apple’s GameCenter using Corona’s gameNetwork API, you only need to make a few changes to implement the Google Play leaderboard and achievement services. There are just a couple things to be aware of:

  1. Set up the Google Play game services independent of the apps you’ve installed.
  2. Link your apps to the service following the six basic setup steps outlined below. Google provides a walkthrough on setting up your game here. IMPORTANT: make sure that you follow c. Specify client ID settings — and particularly steps 2-5 — before you click on the “Create client” button.

Once you click on the little game controller icon, you’ll see a popup form that asks for the name of your game and the category. There will be two tabs:

  • “I don’t use any Google APIs in my game yet.”
  • “I already use Google APIs in my game.”

Select the first option (default) and then specify the name of your game. It doesn’t need to match any existing or future game — you’re just naming the service at this time. Next, select the category and click “Continue”.

1. Configure Game Details

First and foremost, record the long number next to the name you entered. This is your game’s App ID which you’ll need later. You can also change the name/category and add the following:

  • a description
  • a 512 × 512 PNG icon
  • a 1024 × 500 JPEG feature graphic

2. Link the App

Here you can link up the name of the game that will support Google Play game services (GPGS for shorthand in this tutorial) and specify its Package name. Unless your game will reside solely on Google Play, you should set anti-piracy off.

NOTE: this step will take you through the process to create OAuth2 login credentials for the linked app. It’s very important that you follow these steps carefully, including the steps to get the SHA1 Keystore signature. If you don’t do this, you won’t be able to connect to the service (and you cannot edit it later to fix it).

3. Set Achievements

You will need to enter at least five achievements before you can publish, so plan accordingly. Like many other services, you have 1000 points to allocate among your various achievements. You’ll get an ID string for each achievement which you’ll reference later in your app. Two additional aspects are a 512×512 icon and an achievement description which you may not have if you’ve used only GameCenter. You can test the app without these two items, but you’ll need them before publishing to market.

4. Configure Leaderboards

For leaderboards, you just need to fill out the basic information, i.e. whether you want a “high” or “low” system. Because the GPGS SDK automatically creates daily, weekly, and all-time versions of every leaderboard, there’s no need to create separate leaderboards for each time frame.

5. Set Test Accounts

You may add test accounts on this screen. Note that your developer account is already included.

6. Publish!

Before you publish, you’ll be notified if anything needs to be fixed. If everything is correct and in place, you can publish your service. Please note that once published, you can not delete the service, nor can you edit any field with a “padlock” icon beside it.


Corona Setup

Now, let’s initiate Google Play game services in Corona…

1. Build Settings

Before you start coding the actual functionality, add the following code to your build.settings file and specify the App ID you received from the Google. Don’t forget to include the Corona plugin as well!

2. Login and Event Handlers

Somewhere convenient — perhaps within main.lua — require the gameNetwork module. Then, set up some event listeners to initialize Google Play game services. Unlike Apple’s GameCenter, which automatically logs the player in during the gameNetwork.init() call, you have to explicitly log in the player with GPGS.

This may appear confusing at a glance, so let’s follow through it step by step. Since this routine is structured using basic Lua scoping methods, the flow actually goes bottom-to-top.

  1. In the gameNetworkSetup() function, we initialize the gameNetwork library. Here, we check the system platform to determine the appropriate provider. As mentioned above, GameCenter does not require a separate login call, so the event listener can jump directly to the gameNetworkLoginCallback() function. In the case of GPGS, however, we need to run an “intermediate” function to handle the initiation, as illustrated with the gpgsInitCallback() function. This intermediate function uses gameNetwork.request() to initiate GPGS and, assuming it’s successful, it passes the flow onward to the gameNetworkLoginCallback() function.
  2. Once the proper service is initiated, we use gameNetwork.request() again to log in the player locally. Assuming this is successful, the loadLocalPlayerCallback() function is called. The event table of this function contains a data table which in turn contains information such as the player “alias” (event.data.alias) or the player’s network ID (event.data.playerID). At this stage, it may be useful to save this player data locally to a text/JSON file or a SQLite database.

Using Leaderboards

The following example shows how simple it is to display leaderboards. Staying true to the cross-platform design standard above, this code is compatible with both GPGS and GameCenter.

Note that while both platforms use the gameNetwork.show() API to display the leaderboards, the timeScope parameter is not yet supported for GPGS. This feature may be implemented in the near future, however.

Managing Leaderboards

Adding scores to a leaderboard is straightforward using Corona’s gameNetwork.request() API:


Showing Achievements

The following example shows how to display achievements. This time, there’s no code difference between GPGS and GameCenter — just call gameNetwork.show() with the sole parameter of “achievements”.

Managing Achievements

GPGS makes it easy to manage (unlock) achievements:

For Google Play game services, the “unlockAchievement” request doesn’t support percentComplete or showsCompletionBanner, both of which are required for GameCenter. Internally, GPGS supports a “counter” which can be incremented but not decremented; this feature is not yet supported in Corona, but it may be in the near future. At this time, it doesn’t hurt to pass the GameCenter-specific values to GPGS to keep your code base the same on both platforms.


That should provide you enough code to get going with Google Play game services. We are adding multiplayer support on Android right now and once that is done will start work on iOS support. Once these items are done, we’ll follow up with a tutorial that covers all those elements.

Link: 

Tutorial: Introducing Google Play Game Services