Skip to content

Initializing and Using gdxAI

davebaol edited this page Nov 2, 2015 · 1 revision

Despite being born as a libgdx extension, the gdx-ai framework is now able to operate properly regardless of the libgdx environment. To make the magic happen, the GdxAI class acts as a service locator with three services, each implementing a specific interface. Such services provide the means to interact with the surrounding environment. They are

  • Timepiece is the AI clock which gives you the current time and the last delta time i.e., the time span between the current frame and the last frame in seconds. This is the only service provider that does not depend on the environment, whether libgdx or not. It is needed because some parts of gdx-ai (like for instance MessageDispatcher, Jump steering behavior and Wait task) have a notion of spent time and we want to support game pause. It's developer's responsibility to update the timepiece on each game loop. When the game is paused you simply don't update the timepiece.
  • Logger provides an abstraction over logging facilities
  • FileSystem exposes the underlying file system(s)

As said before, the gdx-ai framework internally uses the service locator to give you the ability to use the framework inside or outside a libgdx application.

Also, the GdxAI service locator automatically configures itself with proper service providers in the situations below:

  • Libgdx application, if a running libgdx environment (regardless of the particular backend) is detected. You only have to pay attention to not loading the GdxAI class before libgdx is initialized. This simply means that you should not access the GdxAI class before the ApplicationListener.create method is invoked. In this scenario, the service providers like Logger and FileSystem simply delegate Gdx.app and Gdx.files respectively.
  • Non-libgdx desktop application, if no running libgdx environment is found. In this scenario, the libgdx jar must still be in the classpath but you don't need native libraries since the libgdx environment is not initialized at all. Notice that the Logger service provider is actually a NullLogger instance, meaning that logging message are always ignored. If you need to log, you can set at any time the StdoutLogger or any other custom implementation, for instance a Slf4jLogger or whatever.

On the other hand, if you want to use gdx-ai in Android (or any other non desktop platform) out of a libgdx application you have to implement and set proper providers yourself.