Announcement

Collapse
No announcement yet.

Integrate Google Play Services with Blueprint

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [TUTORIAL] Integrate Google Play Services with Blueprint

    Hello everyone

    I've had a frustrating three weeks trying to figure this out given the default blueprints. With some help from Unreal Engine User Xunil who looked at the Unreal Engine source code (and a lot of fumbling in the dark) we figured out together how to make this work. Before you start reading this tutorial make sure you meet the following requirements:
    • Unreal Engine 4.9.2 or Newer (Confirmed working in 4.10)
    • Got a Google Developer Account (One time payment of 25 dollars)
    • Got an APK ready to upload or already uploaded to the Google Developer Console
    • Got a working Alpha/Beta/Production build on the Google App Store. (In this case mine is in Closed Alpha)


    If you meet all of these requirements then read on!

    Lets have a look at the Google Developer Console First. You need to set up a couple of things first before it'll work at all. Otherwise you may run into your game crashing (like I experienced) or the service not working at all, with no error messages whatsoever.

    In the console you should see something like this:

    Click image for larger version

Name:	62dec119f0.png
Views:	1
Size:	26.4 KB
ID:	1165892

    Now click the app in the console and go to In-App Products. If you don't have any products in here and plan to have them, make one now for testing or those you already know you are going to have.

    Moving on, go to the Services and API section. It should look something like this (I greyed out my RSA Public Key):

    Click image for larger version

Name:	989d8301dd.png
Views:	1
Size:	45.7 KB
ID:	1165894

    The key in that box should already be in your project settings of your Unreal Engine Project like so:

    Click image for larger version

Name:	be8abb9c72.png
Views:	1
Size:	14.2 KB
ID:	1165901

    Now up to the left press the Game Services box. You should see something like this:

    Click image for larger version

Name:	d976e4e945.png
Views:	1
Size:	6.1 KB
ID:	1165904

    Pressing your Game Service here should bring up a slew of other options such as Achievements and Leaderboards. If you haven't set these up yet, you can do so now. The Leaderboard and Achievements will have ID's associated with them (like the In-app purchases) and you will need those later in the editor when we go to set that up so keep the information on hand until then.

    Now we should be done with that part of the Console. Now go here: https://console.developers.google.com/apis/
    It should take you to your projects API Console. If it doesn't then make sure you are logged in to the console in the browser you are currently using.

    You need to go and Enable 7 different APIs to make this work. These are:
    • Google Cloud Messaging for Android
    • Google Cloud Pub/Sub
    • Google Cloud User Accounts
    • Google Play Android Developer API
    • Google Play Game Management
    • Google Play Game Services
    • Google+ API


    You should be able to find them in the API Overview. When all of these are enabled (may not enable right away, just be aware of that), we have to check that you have Authentication Keys available for public use.
    Go to the Credentials part of the Console and make sure you at least have an OAuth 2.0 Client ID for your game. If you do (I can't help you with those) press the Add Credentials button and choose API Key. You should see something like this:

    Click image for larger version

Name:	ae8dc55da8.png
Views:	1
Size:	8.2 KB
ID:	1165905

    Press the "Server Key" button. Give it the name "Public API Key" and leave the IP Address list blank. Press Create. You should now have 2 keys. One of type Server and one of type Android.
    Everything on the console should now be set up and ready to go.

    Next Post will detail how to set up the project and Blueprints.
    Attached Files
    Last edited by OmniOwl; 11-29-2015, 05:23 PM.

    #2
    Lets have a look at the Blueprint and Project Settings part of this.

    Open up your Blueprint project and go to the Project Settings. Go down into the Android settings.
    You should already have "Enable Google Play Support" Checked and the Games App ID in the right field as well as the Google Play License Key in the right field (like mentioned earlier).

    Now go up to the "Advanced APKPackaging" part of the settings. Add two extra tags to the "<manifest> node" and add the following lines:

    Click image for larger version

Name:	c6c78c5f49.png
Views:	1
Size:	9.7 KB
ID:	1092126
    • com.google.android.gms.games.APP_ID="@string/app_id"
    • com.google.android.gms.appstate.APP_ID="@string/app_id"


    Make sure you don't have any weird spaces or hidden characters. It needs to be written exactly like the above.

    When you've done that go to the "Extra Permissions" part and add four elements:

    Click image for larger version

Name:	14d7fb5622.png
Views:	1
Size:	11.9 KB
ID:	1092127
    • com.android.vending.BILLING
    • android.permission.INTERNET
    • android.permission.GET_ACCOUNTS
    • android.permission.USE_CREDENTIALS


    Make sure you don't have any weird spaces or hidden characters. It needs to be written exactly like the above.
    With that added we need to add one more thing before we start mapping the Achievements and Leaderboards. Go to the top of the settings and press the "Open Build Folder" button. It will open the build folder of your project.

    In there you should be able to find the "GooglePlayAppID.xml" file which is located at <PROJECT NAME>\Build\Android\res\values.

    Open the GooglePlayAppID.xml file with any notepade editor. When you open it, usually all it will contain is this:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    	<string name="app_id"></string>
    </resources>
    In this line you add the Applications ID which can be found in your Project Settings in the "Games App ID" box that you have hopefully already filled in. Otherwise the ID is available in the Google Developer Console.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    	<string name="app_id">YOUR GAMES APP ID HERE</string>
    </resources>
    Now save and close the file. Go back into the Project and lets take a look at Achievements and Leaderboards.
    For this to work you need the IDs like I mentioned earlier found in the Developer Console. So if you look at one of your achievements In the console you may find one called something like "Play a game!" with an ID that consists of a lot of characters and how many points it's worth.
    In the project settings go add a node to the Achievement Map. It will add a new child with two fields. In the first field put a name that makes sense to you. It's not seen by the player but it's an alias connected to the ID you put in the second field called "Achievement ID". Do this for every achievement you have.

    Now go to the Leaderboards section. It's the same deal. You have a leaderboard and an ID to the leaderboard (both found in the Google Console). Add a node to the Leaderboard map and then add a name that you can remember (like HighScore) and the Leaderboards ID in the Leaderboard ID field.

    You should now be all set to try and get this done in Blueprint now. So lets get to it!

    At first we need to synchronize the user with the leaderboards. This is done fairly easy.
    In my case I need to check if the leaderboard have a higher score than my local copy does and if it does change it. If it doesn't however then I need to change the leaderboard value. It's done like so:

    Click image for larger version

Name:	224c0d391a.png
Views:	1
Size:	119.8 KB
ID:	1092128

    The "Show External Login UI" should only be called if you know that the player isn't online already. Otherwise it's a wasted call for the end-user and will have to sit through the Google Play Services trying to login again even though the player is already logged in. Otherwise you can just skip that action and then go straight for the Leaderboard nodes. You can check if a player is online with this node:

    Click image for larger version

Name:	bfd80194be.png
Views:	1
Size:	11.3 KB
ID:	1092129

    If this returns True then you can skip the "Show External Login UI" node. If it returns False then you call it. This makes sure you only call for a login when it's needed instead of every time you need to do this.

    Moving on lets read some leaderboards. You may have noticed that in my first picture I have "Get Platform" and then go into a switch statement that differentiates between IOS and Android. This is specific to me but it's relevant for you in case you want to make the game for both Android and IOS as it's unlikely you are going to call your leaderboards the same across the "Game Center" service and the "Google Play" service. Just keep that in mind.

    As I only use one GameMode I choose to store general functions I need to be able to call from different locations in my Game Mode as I can always get a reference to it. So I made the following blueprint nodes:

    Click image for larger version

Name:	6f15ee61f8.png
Views:	1
Size:	91.7 KB
ID:	1092130

    In the "Read Leaderboard Integer" you need to include a player controller. Usually just Player Index 0. Then in the State Name type the name of your leaderboard that you put in the Project Settings. In my case I called it HighScore. If your leaderboard is a Numeric Integer value like mine you will have the "Leaderboard Value" as output. If we assume you succeed then here is where the magic happens. My "Get Highest Score" node is a custom node that I made which looks in my local save file for the highest score I have recorded so far. If the score of the leaderboard is higher than the last recorded highest score, I set the score of my locally saved copy to the one I polled from the Leaderboard. That's what I do in my custom node "Set Highest Score".

    However if the score I found on the Leaderboard is NOT higher than what I locally recorded I go to write the value to the leaderboard instead. Just like before in the "Read Leaderboard Integer" node you have to do the same in the "Write" but with one extra input being the new score to write to the leaderboard. Since this will be synchronized across devices as they are all tied to the same Google Account, don't worry about overlaps or things like that.

    Now our scores are synchronized so lets show the table as well.

    Whatever way you want to trigger this, showing the actual board is very simple. You simply do this:

    Click image for larger version

Name:	af82c05036.jpg
Views:	1
Size:	14.1 KB
ID:	1092131

    As you can see I included the name of my one Scoreboard "HighScore" as the Category Name. Now when your player triggers this it will open up the Google Play Services and then show you a leaderboard. You can read all about how Leaderboards work on the Google API Documentation and Help Sites. You CANNOT embed the leaderboard and you cannot control it's layout nor how it shows. Google decides all of this. I know it's a shame but in order to circumvent this, someone would have to do some serious work with a plugin and a JSON or REST plugin.

    With this you should now have a working Leaderboard. Remember to check if the user is Online before you call the above node and if they aren't, log them in. Also note that this may not work if you simply deploy from the editor to your phone or tablet. You may have to package an APK file and install on your device before it works. Also please be aware that the first time you use this Service it will ask you for permission before doing anything.

    In terms of achievements however it's a bit more tricky. What you do there is essentially pull a copy of the achievements down with this node:

    Click image for larger version

Name:	02b418f82d.png
Views:	1
Size:	17.2 KB
ID:	1092132

    So you have a local copy to work with. Then you call the following node:

    Click image for larger version

Name:	f454e58097.png
Views:	1
Size:	18.9 KB
ID:	1092237

    And this is where things get tricky. What you would need to do is make an array of Achievement Names (that matches exactly with the ones you put in the Project Settings) and then iterate over the achievements you have as you see fit. Sorry I can't say more on this but it's still new to me how to work this part of Achievements.

    Also if you just want to show achievements you just call this node:

    Click image for larger version

Name:	41f0dbbe2f.jpg
Views:	1
Size:	15.8 KB
ID:	1092133

    No category needed.

    Lets move on to In-App Purchases (IAP). In order to do these you need to set them up in the Google Developer Console first. Assuming you've done this take note of the name you put next to the item in the console itself. You can see it here:

    Click image for larger version

Name:	091f4fcff6.png
Views:	1
Size:	18.5 KB
ID:	1092382

    Now go into your blueprint that is supposed to trigger the IAP dialog and put the nodes like this:

    Click image for larger version

Name:	c7cef32190.png
Views:	1
Size:	208.9 KB
ID:	1094206

    As you can see I put in the name that I showed above in the "Make InAppPurchaseProductRequest". If your product is consumable (can be bought multiple times) you need to put a check in the "Is Consumable" box. But this function won't work until 4.10 so just be aware of that.

    Lastly lets look at AdMob.

    Setting up AdMob is very simple. You go to your AdMob account and make a new Ad. It needs to be of type "Banner" and you can give it any name you want and allow any types in the Banner Category. When you've set that up you can see the banner in your overview screen and next to the banner there is an ID. You simply copy and paste that ID into the AdMob field found in the Android Project Settings and there you go.

    Now you can call this node here:

    Click image for larger version

Name:	bf0f61ff33.jpg
Views:	1
Size:	10.8 KB
ID:	1092385

    And you can't really control where it appears other than in the middle of the top of the screen or in the middle of the bottom of the screen.
    Attached Files
    Last edited by OmniOwl; 11-29-2015, 05:22 PM.

    Comment


      #3
      Hi Vipar nice tutorial this helped me a lot

      But i have a problem...my game keeps crashing on my S4 when i try to add the "show external login UI" in any blueprint. I tried in the game mode, player controller, in the menus and in the map bp but each time i call this node it crashes.
      Any idea why?
      I need this to work to make in app purchases leaderboards and achievements.

      Edit: i found the problem...i forgot to enter the license key...it is working now thanks again for the nice tutorial!
      Last edited by S0ul Assass1n; 11-01-2015, 09:06 AM.

      Comment


        #4
        Originally posted by Kryss View Post
        Hi Vipar nice tutorial this helped me a lot

        But i have a problem...my game keeps crashing on my S4 when i try to add the "show external login UI" in any blueprint. I tried in the game mode, player controller, in the menus and in the map bp but each time i call this node it crashes.
        Any idea why?
        I need this to work to make in app purchases leaderboards and achievements.
        It is most likely because you didn't do this step:

        Go to the top of the settings and press the "Open Build Folder" button. It will open the build folder of your project.

        In there you should be able to find the "GooglePlayAppID.xml" file which is located at <PROJECT NAME>\Build\Android\res\values.

        Open the GooglePlayAppID.xml file with any notepade editor. When you open it, usually all it will contain is this:

        Code:
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
        	<string name="app_id"></string>
        </resources>
        In this line you add the Applications ID which can be found in your Project Settings in the "Games App ID" box that you have hopefully already filled in. Otherwise the ID is available in the Google Developer Console.

        Code:
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
        	<string name="app_id">YOUR GAMES APP ID HERE</string>
        </resources>
        I have often run into instant crash when using that node, if that step was not done. It happened yesterday too when I had to clean out some files so make sure you did that.

        Comment


          #5
          Added In-App Purchases and AdMob section to the Tutorial.

          Comment


            #6
            Awesome tutorial!!. It really helped me, and my google play services are running perfect. Thanks for taking to time to do it!!


            Best,
            Alo.

            Now on Google Play!

            Comment


              #7
              Mine is working perfectly without have to upload an alpha o Beta or any kind of apk for that matter ,,, here is what I have done:
              I started the game service from the developer console,
              Add the package adress (com.yourcompany.yourappname) .
              Add game detail (only required ones).
              Add achievement (at least five - with required detail before publishing).
              Now I think the trik is here:
              I copied "get resource" completely to res/ values XML file
              I also add each achievement to project setting>android
              And then now I call UI login without any problem and for achievement the tappy chicken example seems fine for me so far....

              I hope my experience could help some one.

              I haven't tampered with leader board yet so I don't know anything about it yet

              Comment


                #8
                I've been having so many issues with this! I just found this today. I'm going to double check my work -- thank you so much for your contribution.

                Unpopular Minion on the Play Store (Released: January 31, 2016)

                Comment


                  #9
                  I'm having some difficulties with getting logged in.

                  After loading the game to my phone using the Launch option I perform a "Is Logged In" check, which returns FALSE. I then prompt the "Show External Login UI" node -- which does show both the account selection and Sign In page, but returns ON FAILURE. The App on occasion will seemingly restart after the ON FAILURE response.

                  I added the credentials as described and enabled all APIs as well added the permisions. (see images)

                  For my APP ID I only used the 12 digits, rather than the full string of alpha+numeric characters.

                  Click image for larger version

Name:	credentials.jpg
Views:	1
Size:	221.5 KB
ID:	1092448

                  Unpopular Minion on the Play Store (Released: January 31, 2016)

                  Comment


                    #10
                    Originally posted by Distul View Post
                    I'm having some difficulties with getting logged in.

                    After loading the game to my phone using the Launch option I perform a "Is Logged In" check, which returns FALSE. I then prompt the "Show External Login UI" node -- which does show both the account selection and Sign In page, but returns ON FAILURE. The App on occasion will seemingly restart after the ON FAILURE response.

                    I added the credentials as described and enabled all APIs as well added the permisions. (see images)

                    For my APP ID I only used the 12 digits, rather than the full string of alpha+numeric characters.

                    [ATTACH=CONFIG]64244[/ATTACH]
                    Did you remember to add the App's ID to the string value file?

                    Comment


                      #11
                      Originally posted by Vipar View Post
                      Did you remember to add the App's ID to the string value file?
                      It was already present.

                      What's the purpose of generating the API keys if they're never used in the project?
                      Last edited by Distul; 11-02-2015, 02:48 PM.

                      Unpopular Minion on the Play Store (Released: January 31, 2016)

                      Comment


                        #12
                        Originally posted by Distul View Post
                        It was already present.

                        What's the purpose of generating the API keys if they're never used in the project?
                        I am not sure. It didn't work when I didn't have it so I assumed I had to have it. I came to this conclusion by looking at other Android Projects that had the same errors that I did (nothing to do with the Unreal Engine projects).
                        Let me see your blueprint please.

                        Comment


                          #13
                          Originally posted by Vipar View Post
                          I am not sure. It didn't work when I didn't have it so I assumed I had to have it. I came to this conclusion by looking at other Android Projects that had the same errors that I did (nothing to do with the Unreal Engine projects).
                          Let me see your blueprint please.
                          The UI to logon actually does show but I don't ever get the ON SUCCESS response.

                          I'm going to tear down my entire Developer Console and start over since I set it up before your guide. - Nevermind, apparently you cannot remove apps from your console once published.

                          Click image for larger version

Name:	blueprint.png
Views:	1
Size:	147.2 KB
ID:	1092451
                          Last edited by Distul; 11-02-2015, 03:15 PM.

                          Unpopular Minion on the Play Store (Released: January 31, 2016)

                          Comment


                            #14
                            Originally posted by Distul View Post
                            The UI to logon actually does show but I don't ever get the ON SUCCESS response.

                            I'm going to tear down my entire Developer Console and start over since I set it up before your guide. - Nevermind, apparently you cannot remove apps from your console once published.

                            [ATTACH=CONFIG]64248[/ATTACH]
                            It's weird. I tried my app today and I have been unable to connect to the Google Play Services as well and I didn't change a thing. Maybe it's an issue on Google's side this time.

                            Comment


                              #15
                              Originally posted by Vipar View Post
                              It's weird. I tried my app today and I have been unable to connect to the Google Play Services as well and I didn't change a thing. Maybe it's an issue on Google's side this time.
                              I'm going to be so mad if I tore apart my project for a google bug. You may want to note in your tutorial that you only include the digits in the APP-ID as well. Also, maybe a brief discussion of the application setup as well (first step)

                              Do you even have to upload an APK to get this to work?

                              Wonder if this has anything to do with it:
                              MainFrameActions: Packaging (Android (All)): UE4Editor-Cmd: [2015.11.02-22.44.40:228][ 0]LogModuleManager:Warning: ModuleManager: Unable to load module 'OnlineSubsystemGooglePlay' because the file 'C:/Program Files/Epic Games/4.10/Engine/Binaries/Win64/' was not found.
                              Last edited by Distul; 11-02-2015, 06:45 PM.

                              Unpopular Minion on the Play Store (Released: January 31, 2016)

                              Comment

                              Working...
                              X