Google play services getGamesClient

Anything libgdx related goes here!

Google play services getGamesClient

Postby seanoseanohay » Sun Mar 02, 2014 7:53 pm

Greetings all...I am following TheInvader360's tutorial on how to use the Google play services. But I am getting a problem when trying to use gameHelper.getGamesClient.submitScore that says getGamesClient is undefined under GameHelper.

Is this an update in the GameHelper SDK? How to fix it?

I am using the BaseGameHelper I got from[url][https://developers.google.com/games/services/downloads/#samples/url]
seanoseanohay
 
Posts: 84
Joined: Sun Jan 06, 2013 4:01 pm

Re: Google play services getGamesClient

Postby seanoseanohay » Sun Mar 02, 2014 8:10 pm

What I did was copied the version of BaseGameUtils from the repo that TheInvader360 had...It seems to fix the problem but I assume this is a bad practice.
seanoseanohay
 
Posts: 84
Joined: Sun Jan 06, 2013 4:01 pm

Re: Google play services getGamesClient

Postby Brainman » Tue May 20, 2014 2:44 am

Topic is old, but still relevant.

TheInvader360 does not update the library.

If i understood correctly, currently, instead of
Code: Select all
gameHelper.getGamesClient()

you should use appropriate fields of the Games class:
Code: Select all
Games.Leaderboards
Games.Achievements


Example:
Code: Select all
gameHelper.getGamesClient().unlockAchievement(achievementId);

is replaced with:
Code: Select all
Games.Achievements.unlock(gameHelper.getApiClient(), achievementId);

Hope this helps someone.

Update:
Another problem arises with the updated library and the solution is here:
theinvader360.blogspot.com comment
Sébastien Laoût10 May 2014 04:31
Well, it is actually tougher because I get a NullPointerException when instantiating the GameHelper because they modified the constructor:

public GameHelper(Activity activity, int clientsToUse) {
mActivity = activity;
// NEW LINES:
mAppContext = activity.getApplicationContext(); // THIS ONE GIVES NullPointerException
mRequestedClients = clientsToUse;
mHandler = new Handler();
}

The NullPointerException is because getApplicationContext() is:

class ContextWrapper {
...
public Context getApplicationContext() {
return mBase.getApplicationContext(); // mBase IS NULL HERE
}

I'm currently only working a few minutes at a time on my project. I have to go. I will keep you posted when I have solved the problem.

Sébastien Laoût13 May 2014 14:16
OK, I fixed the NullPointerException:

Remove the GameHelper instantiation in the constructor and move it at the very end of onCreate(Bundle):

if (gameHelper == null) {
gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
gameHelper.enableDebugLog(true);
}
gameHelper.setup(this);

Also, in order to avoid a "java.lang.IllegalStateException: GoogleApiClient must be connected." when the user in not connected and clicked to show leaderboard or achievements screen, I wrapped the code like this:

@Override
public void showLeaderboardScreenGPGS(String scoreId) {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), scoreId, 100);
} else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}

@Override
public void showAchievementsScreenGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 100);
} else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
Brainman
 
Posts: 2
Joined: Thu Mar 14, 2013 11:05 pm

Re: Google play services getGamesClient

Postby villegas_j » Tue Feb 17, 2015 9:01 pm

I found this tutorial which helped me solve the issue:

http://pygmalion.nitri.de/google-play-g ... x-489.html

It describes how to use the new constructor.

Here is what my code looks like:

Code: Select all
public class AndroidLauncher extends AndroidApplication implements GameHelperListener, ActionResolver{
   
   private GameHelper gameHelper;
   
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
      initialize(myGame(this), config);
      if (gameHelper == null) {
         gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
         gameHelper.enableDebugLog(true);
      }
      gameHelper.setup(this);
   }
   
   
   
   
   //...
   
      @Override
      public void onStart() {
         super.onStart();
         gameHelper.onStart(this);
      }
   
      @Override
      public void onStop() {
         super.onStop();
         gameHelper.onStop();
      }
   
      @Override
      public void onActivityResult(int request, int response, Intent data) {
         super.onActivityResult(request, response, data);
         gameHelper.onActivityResult(request, response, data);
      }
   
      @Override
      public boolean getSignedInGPGS() {
         return gameHelper.isSignedIn();
      }
   
      @Override
      public void loginGPGS() {
         try {
            runOnUiThread(new Runnable() {
               public void run() {
                  gameHelper.beginUserInitiatedSignIn();
               }
            });
         } catch (final Exception ex) {
         }
      }
   
      @Override
      public void submitScoreGPGS(int score) {
         Games.Leaderboards.submitScore(gameHelper.getApiClient(),
               "[replace with your AppID]", score);
   
      }
   
      @Override
      public void unlockAchievementGPGS(String achievementId) {
         Games.Achievements.unlock(gameHelper.getApiClient(), achievementId);
   
      }
   
      @Override
      public void getLeaderboardGPGS() {
         if (gameHelper.isSignedIn()) {
            startActivityForResult(
                  Games.Leaderboards.getLeaderboardIntent(
                        gameHelper.getApiClient(), "[replace with your AppID]"),
                  100);
         } else if (!gameHelper.isConnecting()) {
            loginGPGS();
         }
   
      }
   
      @Override
      public void getAchievementsGPGS() {
         if (gameHelper.isSignedIn()) {
            startActivityForResult(
                  Games.Achievements.getAchievementsIntent(gameHelper
                        .getApiClient()), 101);
         } else if (!gameHelper.isConnecting()) {
            loginGPGS();
         }
   
      }
   
      @Override
      public void onSignInFailed() {
         // TODO Auto-generated method stub
   
      }
   
      @Override
      public void onSignInSucceeded() {
         // TODO Auto-generated method stub
   
      }
}
villegas_j
 
Posts: 17
Joined: Tue Feb 17, 2015 8:54 pm


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], JohnDawn and 7 guests