📜 ⬆️ ⬇️

Google Play Game Services in Libgdx

image

Introduction


Many game developers want to use Google Play Game Services in their games. I was not an exception, but I didn’t have knowledge and skills on how to quickly add GPGS support to my libgdx game. In this article I will describe the process of connecting the table of records and achievements. Initial data: Eclipse , customized developer console for working with gaming services, android project, root project.

Customization


I will not tell you how to set up a developer console to work with GPGS , I will just say that from there we will need resources like:
games-ids.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Google Play game services IDs. Save this file as res/values/games-ids.xml in your project. --> <resources> <string name="app_id">310266082735</string> <string name="achievement_take_10_levels">HgkIr62KmoQJEAIQBg</string> <string name="achievement_take_30_levels">HgkIr62KmoQJEAIQBw</string> <string name="achievement_take_50_levels">HgkIr62KmoQJEAIQCA</string> <string name="achievement_take_70_levels">HgkIr62KmoQJEAIQCg</string> <string name="achievement_take_100_levels">HgkIr62KmoQJEAIQCQ</string> <string name="achievement_beginner_cutter">HgkIr62KmoQJEAIQDw</string> <string name="achievement_advanced_cutter">HgkIr62KmoQJEAIQEA</string> <string name="achievement_master_cutter">HgkIr62KmoQJEAIQEQ</string> <string name="achievement_lucky">HgkIr62KmoQJEAIQEg</string> <string name="achievement_cheerful">HgkIr62KmoQJEAIQEw</string> <string name="achievement_exceptional_joyous">HgkIr62KmoQJEAIQFA</string> <string name="achievement_thrust">HgkIr62KmoQJEAIQFQ</string> <string name="achievement_very_persistent">HgkIr62KmoQJEAIQFg</string> <string name="achievement_the_most_resistant">HgkIr62KmoQJEAIQFw</string> <string name="achievement_1_000_000">HgkIr62KmoQJEAIQGA</string> <string name="achievement_2_000_000">HgkIr62KmoQJEAIQGQ</string> <string name="achievement_3_000_000">HgkIr62KmoQJEAIQGg</string> <string name="achievement_unlucky">HgkIr62KmoQJEAIQGw</string> <string name="achievement_loyal">HgkIr62KmoQJEAIQHA</string> <string name="achievement_fan">HgkIr62KmoQJEAIQHQ</string> <string name="achievement_leader_of_mars">HgkIr62KmoQJEAIQHg</string> <string name="achievement_leader_of_neptune">HgkIr62KmoQJEAIQHw</string> <string name="achievement_unhurried">HgkIr62KmoQJEAIQIA</string> <string name="achievement_final_push">HgkIr62KmoQJEAIQIQ</string> <string name="leaderboard_leaderboard">HgkIr62KmoQJEAIQAQ</string> <string name="leaderboard_pack_1">HgkIr62KmoQJEAIQCw</string> <string name="leaderboard_pack_2">HgkIr62KmoQJEAIQDA</string> </resources> 
where app_id is the identifier of your application, strings of the type leaderboard_xxx and achievement_xxx indicate a specific table of records and achievement, respectively. You should create a resource xml file called games-ids.xml in your game’s Android project and place the resources above.

image

GPGS requires a google-play-services_lib project library. His path:
<android-sdk>\extras\google\google_play_services\libproject\google-play-services_lib
By default, gaming services are not installed along with the Android SDK , so nothing will follow this path. To fix this, you need to install two packages in the Android SDK Manager: Google Repository and Google Play services .
')
image

The google-play-services_lib library project needs to be imported into Eclipse . Then set the checkbox “Is library” in the properties of the imported project.

image

Now you need to link together your android-project and google-play-services_lib library. To do this, add the library to the section Required projects on the build path . ( Properties -> Java Build Path -> Projects ).

image

At this stage, you could finish the configuration, but the official Google documentation strongly recommends using the GameHelper helper class . This class greatly facilitates life when working with gaming services. Its installation is similar to the google-play-services_lib installation (import the project as a library, link the project library with the android project). Download BaseGameUtils .

In AndroidManifest.xml, we add two permissions and meta data:

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

Meta data is added inside the application tag:

 <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 

Setup is complete.

Code


In the main Activity we implement two interfaces GameHelperListener , ActionResolver :

 public class MainActivity extends AndroidApplication implements GameHelperListener, ActionResolver 

The interface of the GameHelperListener is as follows:

 public interface GameHelperListener { /** *     .      *   «Sign-in»    */ void onSignInFailed(); /**      */ void onSignInSucceeded(); } 

We create the ActionResolver interface ourselves. It is needed to call the platform-specific code. This technique is described in the official libgdx wiki . Interface example:

 public interface ActionResolver { /**     */ public boolean getSignedInGPGS(); /**  */ public void loginGPGS(); /**      */ public void submitScoreGPGS(int score); /** *   * * @param achievementId * ID .    games-ids.xml */ public void unlockAchievementGPGS(String achievementId); /**  Activity    */ public void getLeaderboardGPGS(); /**  Activity   */ public void getAchievementsGPGS(); } 

Sample code for Main Activity :

 public class MainActivity extends AndroidApplication implements GameHelperListener, ActionResolver { //       private GameHelper gameHelper; //    private TestGame game; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // CLIENT_ALL    API   gameHelper = new GameHelper(this, GameHelper.CLIENT_ALL); //       gameHelper.setConnectOnStart(false); gameHelper.enableDebugLog(true); //       //  (  –    ) getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //   this  ActionResolver.    //   -  GPGS game = new TestGame(this); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); initialize(game, config); gameHelper.setup(this); } //  gameHelper': onStart(), onStop()     // GPGS    android- @Override protected void onStart() { super.onStart(); gameHelper.onStart(this); } @Override protected void onStop() { super.onStop(); gameHelper.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //  gameHelper    ,   //    ,      // Activity gameHelper.onActivityResult(requestCode, resultCode, data); } @Override public boolean getSignedInGPGS() { //   return gameHelper.isSignedIn(); } @Override public void loginGPGS() { try { runOnUiThread(new Runnable() { @Override public void run() { //   .     // .   UI- gameHelper.beginUserInitiatedSignIn(); } }); } catch (Exception e) { e.printStackTrace(); } } @Override public void submitScoreGPGS(int score) { //         ID // “HgkIr62KmoQJEAIQAQ” Games.Leaderboards.submitScore(gameHelper.getApiClient(), "HgkIr62KmoQJEAIQAQ", score); } @Override public void unlockAchievementGPGS(String achievementId) { //    ID achievementId Games.Achievements.unlock(gameHelper.getApiClient(), achievementId); } @Override public void getLeaderboardGPGS() { //  Activity     .   //   Activity    startActivityForResult( Games.Leaderboards.getAllLeaderboardsIntent(gameHelper .getApiClient()), 100); } @Override public void getAchievementsGPGS() { //  Activity   startActivityForResult( Games.Achievements.getAchievementsIntent(gameHelper .getApiClient()), 101); } @Override public void onSignInSucceeded() { } @Override public void onSignInFailed() { } } 

Let's say we have an achievement in the game - to score 1 million game points, then the code implementing it will look like this:

 public static void checkTotalPoints(int points) { ActionResolver res = getActionResolver(); if (!res.getSignedInGPGS()) { return; } if (points >= 1000000) { res.unlockAchievementGPGS("HgkIr62KmoQJEAIQGA"); } } 

It's the same with the highscore table, only even easier: you just need to call the ActionResolver interface’s submitScoreGG (int score) method .

PS To test gaming services, you need to export an apk-file with the same certificate as in the developer console. You also need to add testers accounts in the console. Changes in gaming services take effect after a while.

Used sources


developers.google.com/games/services/android/quickstart
developer.android.com/intl/ru/google/play-services/setup.html
github.com/libgdx/libgdx/wiki

Source: https://habr.com/ru/post/238327/


All Articles