Develop with LibGDX

The LibGDX game engine is a fantastic project for Java developers who're building cross-platform games. It's open-source and incorporates lots of components to accelerate development.

Nakama server is a perfect complement to add server-side features to a LibGDX game. We'll use the official Java client to communicate with the server and setup a project to develop locally. With this tutorial we'll use Docker to quickly and easily run the server.

Setup project

Before you can create the LibGDX project you'll need to install the Android SDK and tools. Have a look at the setup guide on the Android website.

We'll use the command line tool to setup a new LibGDX project. You can customize the values in the cmdflags to suit your project.

java -jar ./gdx-setup.jar --dir beahero --name be-a-hero \
    --package com.heroiclabs.games.beahero --mainClass BeAHeroMain

Now we've got our game project setup creatively called "Be A Hero". I'll use IntelliJ with the project and import it with "File > Import Project > Import from existing sources".

Setup and run server

You could setup the server and database server yourself after you download the binaries and connect the server with the database but instead we'll use Docker to manage it for us. I'll use the Docker compose file available here.

I've got a macOS machine and will create a Docker machine with the Virtualbox driver.

docker-machine create --driver virtualbox default
eval $(docker-machine env default)
docker-machine start default

Now we can download Docker images and start the server and database server in the Docker container.

docker-compose -f ./docker-compose.yml up

For more detailed instructions on how to use Docker have a look at the quickstart. When everything has completed you'll see interleaved output for both "cockroachdb_1" and "nakama_1" which shows them setup and started.

Tip

You can stop the Docker machine with docker-machine stop default.

Add client

Now we've got the game server started and a LibGDX project we'll add the Java client to the codebase as a dependency. In the build script you should add the client library.

project(":core") {
    apply plugin: "java"

    dependencies {
        // ...
        compile "com.heroiclabs.nakama:client:0.1.0" // set your version
        compile('org.slf4j:slf4j-api:1.7.25') {
            force = true // don't upgrade to "1.8.0-alpha2"
        }
    }
}

We can add some code to the "BeAHeroMain" class generated by the LibGDX command line tool which will create a client object.

Client client = DefaultClient.builder("defaultkey")
        .host("127.0.0.1")
        .port(7350)
        .ssl(false)
        .build();

That completes how to setup the client and server with a LibGDX project. You now have a Nakama server running locally for development and a client ready to connect. The Java client guide has more code examples and describes features of the client in more detail.