In this article we will take you through setting up the Google Play Services extension in your Android game. This article covers setting up the Google Play store as well as getting your game to log in when run on an Android device, which is a requirement for all other Google Services functionality to work.
IMPORTANT! In order for Google Play Services login to work the initial login request from the device must come from a build that was published on the Google Play Console and downloaded from the Store. You cannot tether your device via USB, hit play in GMS, and login as the login will fail. For the first initial login on your device you need to upload a build to Google Play, get the testing link from the release, and download the test build from the Google Play Store onto your device. Launching that downloaded build will login as expected and work. After that initial login subsequent logins from local tethered debug builds should work correctly.
Set Up Google Play
Before you can add any further Google Services functionality, you first have to set up an app listing on your Google Play Developer Console for the game and you will also have had to upload an APK to one of the available channels for testing - either Alpha or Beta is fine (the Internal Testing channel may not work). Once that has been done, you will also need to set up the Game Services for the app.
From the Developer dashboard, click the Game Services button on the left, then click the Add New Game button:
This will then show you a screen where you have to give some details about the game, including a name and a category:
After filling in the information and pressing Continue, you will now need to go to the Linked Apps section and get the App ID and set the app as being for Android:
The App ID is shown at the top and you will need to take a note of it as we'll be using it in GameMaker Studio 2 later. When you click the Android button, you will then be prompted to give some information about the app you want to link the services too, and you should link it to the app that you have previously uploaded to the store.
IMPORTANT! Any changes made to the Game Services console for your game should be published before testing.
When you create your app listing on the Google Play console, you can add tester accounts to each of the different releases (Internal, Alpha or Beta). However, this only adds testers for the game but not for the services. To fix this you will need to go to the Testing section of the Game Services page and then add in the emails individually, or select the testers that you have defined for the game in the app listing:
Set Up GameMaker Studio 2
You need to open your project in GameMaker Studio 2 now and go to the Android Game Options. If you then click on the Social menu item, you will see a section labelled Google Services ID and you should paste the APP ID that you got when you linked the app services on the Google Play developer console (see the section above for more details). You should also check the box marked Enable Google Services:
Checking the Enable Google Services checkbox will prompt you to get the Google Services extension, and selecting "Yes" will open the Game Options Add Ons window where you can download and install the extension in your project:
Once the extension has been installed, it will be added to your resource tree. It's important to note that the Services extension also includes the ability to show advertisements in your game. If you do not want that functionality then you can remove it by opening the extension (double click on it in the resource tree), and then right clicking on the GoogleMobileAds.ext and selecting Remove Selected:
If you do want to use ads, then you can just leave the extension as-is, and if you need further information on setting up ads for Android then see the following article:
When using the Google Services extension, we recommend that you create a unique and persistent controller object for it. By flagging the object as persistent, you will only need to have it created once at the very start of the game and it will be "persisted" across all other rooms. In this way you will always have access to it and it can maintain data across the whole game.
Once you have your persistent object created, you will need to add the following into the Create Event:
global.PlayerName = "Anon";
global.PlayerID = -1;
The achievement_login function will attempt to log the user into the Google Play services, using the Google account set up for the device being used, and the rest of the code will set up a couple of global variables for future use (this is not strictly necessary and they will be used simply to illustrate possible ways you can work with Google Services in this article).
NOTE: If the service is not available, then the user will be logged into a "pretend" game centre and all achievements and scores, etc... are stored on the device so that when the actual service is available, these details can be uploaded.
Calling this function will trigger a callback to the Social Asynchronous Event where you can access the built-in variable async_load. This variable holds a DS map populated with various key/value pairs which (in this case) will be related to the user logging in.
Once you have added this event, you can to add the following code:
if ds_map_exists(async_load, "id")
global.PlayerName = async_load[? "name"];
global.PlayerID = async_load[? "playerid"];
show_debug_message("GOOGLE SERVICES: User " + global.PlayerName + " with ID " + string(global.PlayerID) + " has logged in");
Here, the "id" key of the ds_map is used to identify the correct callback event (there can be more than one trigger function for any given asynchronous event), and will be paired with the constant achievement_our_info. We check this in the code above and then use the rest of the returned map contents to set some global variables and show a console log with the details.
Note that you don't have to log in on game start, and can delay the process until the player performs an act, like clicking on a login button. In these cases you can omit the login function in the persistent object (but keep the asynchronous system event), and simply call the login function from the object responding to the user input, something like this:
Note how we check the login status first using the function achievement_login_status. This function is useful and can be used to check that there is a user logged in before performing any actions that use the Google Services extension, as well as for setting buttons to be visible or change colour depending on the status, etc...
IMPORTANT! If you have enabled Google cloud saving in Android Game Options without first enabling it the Google Play Game Services console, then sign in will not work due to the additional permissions being requested
Using Google Play Services With Licencing
If you are also using the Google Play Licencing extension in your game as well as the Services extension, then you may experience issues while testing your project and trying to log into Google Play - specifically that you will be unable to log in. This happens because you need an OAuth client key for your test signing key, but Google Game Services will only create an OAuth client key for the store signing key, which may be different from the key located in the GameMaker Studio 2 keystore. To resolve this you need to follow these steps:
To start with you need to open your project in GameMaker Studio 2 and then in the Android Preferences click the Show Key Hash button:
We'll be using this SHA1 checksum string later, but for now you can leave GameMaker and go to the Google Cloud Services console for your game, and then in the API's section select Credentials (this link should take you to the appropriate section):
Ensure that you have selected the correct Game Services project at the top, and then go through the keys in the Oauth 2.0 client IDs section, clicking the "Edit" button to open each one and checking the Signing-certificate fingerprint SHA1 checksum string against the one generated for the keystore being used in your project:
If none of them match the SHA1 code in your GameMaker Studio 2 keystore settings, select Create credentials from the top left of the Credentials page and choose OAuth client ID from the drop-down menu:
Under Application type select "Android", then give the key a name, and paste in the SHA-1 signature of your keystore from GMS2. Finally, fill in the Package name field with the Android package name of your app and create the key.
Your app should now be allowed to sign in to Google Play when deployed directly to the test device from GameMaker Studio 2 for testing.
With that, you know have a persistent object or a button that will log the user into the Google Play Services API and deal with the callback from that API, storing the player details in a couple of global variables. This is the most basic integration of the Services extension that you can have, but once this is set up you can then go on to add more features. To find out more, read the following articles: