In this article we will take you through setting up the Google Play Services extension in your project and getting it to log-in in your Android game, which is a requirement for all other Google Services functionality to work.
IMPORTANT! In order for Google Play Services login to work, your initial login request from your 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 GMS2, and login, as this will fail. You do need to upload a build to Google Play, get the testing link from your app's dev panel and then download the test build from the Google Play Store onto your device.
After that initial login, subsequent logins from local builds should work correctly, but we cannot guarantee Google's policy here will remain unchanged - any issues, always upload your builds and download from the store as a tester..
Set Up Your Google Play Developer Panel For The App
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 GMS2 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 to, 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 you do any testing.
Adding Testers to Your Google Play Panel
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:
Obtain the Google Play Services asset from the GameMaker Marketplace
The Google Play Services asset is a free add-on by YoYo Games and not only contains the Google SDKs you require, but also a full demo project for much of this Google Play Services functionality (and everything in this guide), so you do have the option of importing our asset as a new project, swapping out our Game Options for yours, and testing using our demo before adapting your own project later.
To get the Marketplace asset click the following link: https://marketplace.yoyogames.com/assets/2008/google-play-services and then click the green "Add to Account" button at the top of the page. Read and accept the EULA, and then you will be able to download the asset later on and import it via My Library inside GMS2 (more on this below).
Note that if you have limited/unreliable internet access, or you have multiple machines you want to develop on but only want to download the asset once, you can alternatively download the asset direct from the website to get a .yymps file. This .yymps file can then be dragged onto GMS2 whenever you want to import the asset into your projects.
Set Up Your Project's Game Options
You now need to open your project in GameMaker Studio 2 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. Paste the APP ID that you got when you linked the app services in your Google Play developer console:
Note that you do not need to click the "Enable cloud saving" checkbox here (unless you know you want to use that), as cloud saving is separate Google functionality beyond the scope of this guide.
Import The Google Play Services Asset Into Your Project
You can now import the Google Play Services asset from your Marketplace library:
In the Import dialogue which appears, select which extensions and other assets you want to import into your project, then confirm this dialog to add the items into your asset browser.
It's important to clarify that the Google Play Services asset contains multiple extensions for various Google products/services and not all of them are required to complete this guide. If you imported everything in the asset, but do not want the extra functionality, then you can simply remove any unwanted extensions from the asset browser.
Note also that the asset contains a full demo project for much of this Google Play Services functionality (and everything in this guide), so you do have the option of importing our asset as a new project, swapping out our Game Options for yours, and testing using our demo before adapting your own project later.
Code Your Login Functionality
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 a suitable object created and set to be persistent, you will need to add the following into its 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 enabled Google cloud saving in Android Game Options earlier without first also enabling it your Google Play Game Services console, then this sign-in will not work due to the additional permissions being requested.
Now Add More Functionality
With that, you now 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:
Troubleshooting: Ensure Your Google Oauth 2.0 Client IDs Allow You To Login (Including Store Packages!)
Please be aware that if your Google Developer panel settings are not correct, then you may experience additional issues while testing your project and trying 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 one you use within GMS2 (especially if you're in a team and only one of you does the uploads, but you each have your own development keystore).
There is also a specific App Signing Certificate value which must appear in this list, otherwise store packages will not be able to log in.
To check and fix this, 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. Click the "Edit" button to open each one and check the Signing-certificate fingerprint SHA1 checksum string against the one you were expecting to see.
(See the two subheadings at the bottom of this guide as to what values you should be seeing.)
If none of them match the SHA1 code you need, 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 you want to add. 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 using that signing certificate.
When testing store builds
If you have enabled Google's optional extra app-signing when doing your builds (whihc is the default nowadays), then you do need to make sure that your project's apk/abb App Signing Certificate value is added to this OAuth list, otherwise whilst you will be able to test using dev builds, store packages will not be allowed to log in.
You can get this value from the app's own App Signing page.
(The Upload Certificate value is likely already in the list and is the value used when dev-testing, as shown below, so ignore that if your issue is specific to store downloads not working.)
When trying to test Google Play Licencing also / you have multiple keystores in your team
If it isn't already, open GameMaker Studio 2 and open Preferences > Platform Settings > Android. See if your Key Hash (SHA1) field is already populated, and if it is not then click the Show Key Hash button. After a few seconds the hash fields will be populated, like so (your value will be different, so don't copy ours here):