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.
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.
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...
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: