0
\$\begingroup\$

I have an android game built with libgdx and what I'd like to do is to add some kind of authentication so I can connect the game with user data to firebase database.

I have some experience with firebase using it in native android apps, but I have no idea on how to connect "native code" with libgdx.

Researching I found these third party libs https://github.com/mk-5/gdx-fireapp/tree/master/gdx-fireapp-core and https://github.com/TomGrill/gdx-firebase

But as it does not provide any example it didn't help me much.

So what's the best way to "mix" native android code and libgdx? I want to be able to achieve things like storing data on db, implementing in-app-purchases, authenticating users. If I'm looking the wrong way please let me know too.

\$\endgroup\$
1
\$\begingroup\$

In a libGDX application, you have 2 'main' types of codebase. The first is the libGDX game itself - this contains all your screens, assets, engine code ... the stuff that makes the game work.

The other is the target specific code needed to start the application. This is the desktop, Android or iOS code. I'm going to talk about Android only here (as desktop is ... just easy, and I know nothing about iOS), but these principles apply to all of them.

Android (and I suspect iOS) are a little harder than desktop as there are environment specific things going on. You need to call out to the outer system for things like Toast messages and you need the Android Context to connect to databases (well, at least SQLite, I suspect Firebase is similar).

So, when you create your game you need to hand it a link to the outer system. If you are targeting only one outer system type, this becomes easier (eg: does Firebase even exist on iOS?)

The start for this is the Launcher class of the project - this is where the program starts and then fires up the actual game. Before you start the game, you can do anything you like that is native to the target. In my code, I start up a database handler class.

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    actionResolver = new AndroidActionResolver(this);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new Game(actionResolver), config);
}

Here is my AndroidActionResolver:

public class AndroidActionResolver implements ActionResolver {

    Handler uiThread;
    Context appContext;
    private AndroidDataSource dataSource;

    public AndroidActionResolver(Context appContext) {
        uiThread = new Handler();
        this.appContext = appContext;
        dataSource = new AndroidDataSource(appContext);
    }

    @Override
    public AndroidDataSource getDB() {
        return dataSource;
    }

    public void makeToast(final String message) {
        uiThread.post(new Runnable() {
            @Override
            public void run() {
                  Toast.makeText(appContext.getApplicationContext(), message,
                  Toast.LENGTH_SHORT).show();
            }
        });
    }
}

You can see in here that I have exposed a database connection and provided a method for the game to call back to the native code to raise a Toast.

Then, when you launch your game class, you pass in the native handler instance that you just created;

initialize(new Game(actionResolver), config);

Over on the game side, create an interface for the resolver class, including all methods you want to expose. Make sure that your resolver on the native side implements this interface.

public interface ActionResolver {
    public DataSource getDB();
    public void makeToast(String message);
}

Then, in your main game class on the libGDX side, store this class as a local member and declare it to be of the interface type:

private ActionResolver actionResolver;

public Game(ActionResolver actionResolver) {
    this.actionResolver = actionResolver;
}

Now that you have this class in your game, you can pass it around as needed and it provides the bridge between your native code and the game code. You can put in as many methods as you need to pass information between the two.

Apologies if some of the Android code presented here is a little dated - I've copied and pasted from an old project where I've had this stuff working.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.