As promised in my previous post, Facebook Android SDK 4.0 Login Manager without button, I’m going to show how to integrate Facebook into your libGDX game. For those of you not familiar with libGDX, it is a cross-platform game development library/framework. LibGDX really makes game development easier.

This tutorial will make your game able to integrate Facebook login and game invites using the native Facebook UI. I will assume that you have followed my previous tutorial linked above. If not, go do that now and then we will continue.

Ok, so after Facebook is installed, you will need a few things to add to your game.

First, you will need an interface class. The interface class will do exactly that…interface your Android game launcher class with the libgdx core game class. The interface will need at a minimum three methods: login, logout, and isConnected. Any other things you plan do to will also need to be added, such as inviting, sharing, etc. For the purposes of this tutorial, we’ll call it Resolver. You can call it whatever you want though.

public interface Resolver
{
  void connectFacebook();
  void disconnectFacebook();
  boolean isConnectedFacebook();
}

As you can see, our methods are connectFacebook(), disconnectFacebook, and isConnectedFacebook. We then will need to add those to our Android launcher class. By default, libGDX calls it AndroidLauncher. We’ll use that. We will add “implements Resolver to the public class declaration. This will allow our game class to call the Facebook methods via the launcher.

public class AndroidLauncher extends AndroidApplication implements Resolver

Now, you will get get a warning that AndroidLauncher must be declared abstract because the methods have not been created yet. One of them already has though, if you followed the previous tutorial. Remember this?

public boolean isConnectedFacebook()
{
  if (accessToken.getCurrentAccessToken() != null)
  {
    return true;
  }
  else
  {
    return false;
  }
}

That is going to be called by our Resolver class interface.

Now, let’s create the other two:

public void connectFacebook()
{
  loginManager.logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends","email"));
}

public void disconnectFacebook()
{
  loginManager.logOut();
}

And that’s all you need for basic login and logout in your launcher class.

Now, we want to be able to make the game itself login and logout to facebook. I will assume you already know how to make buttons and click detection, so I won’t cover that. So, in the main game class (we will call it MyGame for this tutorial), you will need to add the following:

First, in the public class declaration you need the following member, and to modify the class constructor as follows:

public class MyGame extends Game
{
  public Resolver resolver;

  public MyGame(Resolver resolver)
  {
    this.resolver = resolver
  }

Why do we need this? Because we need to get the context for the class into Resolver.

Which means, in your AndroidLauncher, you will now get an error which we need to correct:

// Original piece of code that now gives an error
initialize(new MyGame(), config);

// New code that fixes error
initialize(new MyGame(this), config);

Adding “this” to MyGame() passes the Android launcher context to the core game.

Now, in MyGame, we also need the following methods:

public void connectFacebook()
{
  resolver.connectFacebook();
}

public void disconnectFacebook()
{
  resolver.disconnectFacebook();
}

public boolean isConnectedFacebook()
{
  return resolver.isConnectedFacebook();
}

Why are the methods public, and not private, you ask? Because we will want to be able to call the methods from the game screens, like so:

// Connect to Facebook
MyGame.connectFacebook();

//Disconnect from Facebook
MyGame.disconnectFacebook();

// Check if Facebook is connected
connectedToFacebook = MyGame.isConnectedFacebook();

 

That’s the basics. Doing other things like inviting, publishing to a feed, and so on, simply involves adding a method in the interface, a method in the launcher, and a method in the main game class.

Any questions?