Headless / CLI LibGDX

I am coding the server side for a small game with LibGDX support and stumbled upon a problem. Every time I try to use any methods of Gdx.files.* , A NullPointerException meets me.

Apparently this is because I am not implementing ApplicationListener, so LibGDX was not initialized.

Is there any way to initialize LibGDX in mute / CLI mode? I need to be able to load TiledMap objects on the server side.

 MapLoader(Request request) { TiledMap tmp = new TmxMapLoader().load("maps/" + request.name + ".tmx"); } 

Exception in the Server thread java.lang.NullPointerException at com.tester.Example.server.ExampleServer $ 2.received (MapLoader.java:83) at com.esotericsoftware.kryonet.Server $ 1.received (Server.java:60) at com.esotericsoftware.kryonet.Connection.notifyReceived (Connection.java:246) at com.esotericsoftware.kryonet.Server.update (Server.java:202) at com.esotericsoftware.kryonet.Server.run (Server.javahaps50) at java.lang.Thread.run (Thread.java:722)

+4
source share
3 answers

I would not recommend using libGDX for a headless environment, it just isn't intended to be used that way, and you might run into problems in the future, as the libGDX command changes structure. However, since Rod noted that it is possible to do this, and below is a snippet of how you do it. To initialize the global Gdx.files, you will need to create a class in the backend package and configure the global variables yourself:

 package com.badlogic.gdx.backends.lwjgl; import com.badlogic.gdx.Gdx; public class Headless { public static void loadHeadless() { LwjglNativesLoader.load(); Gdx.files = new LwjglFiles(); } } 

The rest should be pretty simple. Just call Headless.loadHeadless (); at the beginning, and then you should be able to use parts of the required structure.

As I said, I would not suggest doing this, but I did not find good solutions for using libgdx with a client / server architecture.

Edit :

Some time ago (after I wrote this answer initially) libgdx added a headless backend that is designed for this kind of purpose. This is the correct and correct way to use libgdx in a headless environment and works very well for creating a server using libgdx.

+2
source

As of December 23, 2013 ( pull request # 1018 ), libGDX has a headless backend that you can use for this purpose.

+4
source

Take a look at the back end of LWJGL, in particular LwjglApplication.java . The constructor for LwjglApplication initializes all Gdx. * Globals such as Gdx.files.

If you were to call the steps in the constructor from your own code, with the exception of calling initialize (), then this should get what you want.

+2
source

All Articles