How handle nifty initialization in a Slick2D state based game?

Posted by nathan on Game Development See other posts from Game Development or by nathan
Published on 2012-11-10T23:07:34Z Indexed on 2012/11/10 23:18 UTC
Read the original article Hit count: 446

Filed under:
|
|

I'm using Slick2D and Nifty GUI. I decided to use a state based approach for my game and since i want to use Nifty GUI, i use the classes NiftyStateBasedGame for the main and NiftyOverlayBasicGameState for the states.

As the description say, i'm suppose to initialize the GUI in the method initGameAndGUI on my states, no problem:

@Override
protected void initGameAndGUI(GameContainer gc, StateBasedGame sbg) throws SlickException {
    initNifty(gc, sbg) 
}

It works great when i have only one state but if i'm doing a call to initNifty several times from different states, it will raise the following exception:

org.bushe.swing.event.EventServiceExistsException: An event service by the name NiftyEventBusalready exists.  Perhaps multiple threads tried to create a service about the same time?
at org.bushe.swing.event.EventServiceLocator.setEventService(EventServiceLocator.java:123)
at de.lessvoid.nifty.Nifty.initalizeEventBus(Nifty.java:221)
at de.lessvoid.nifty.Nifty.initialize(Nifty.java:201)
at de.lessvoid.nifty.Nifty.<init>(Nifty.java:142)
at de.lessvoid.nifty.slick2d.NiftyCarrier.initNifty(NiftyCarrier.java:94)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:332)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:299)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:280)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:264)

The initializeEventBus that raise the exception is called from the Nifty constructor and a new Nifty object is created within the initNifty method:

public void initNifty(
    final SlickRenderDevice renderDevice,
    final SlickSoundDevice soundDevice,
    final SlickInputSystem inputSystem,
    final TimeProvider timeProvider) {
    if (isInitialized()) {
       throw new IllegalStateException("The Nifty-GUI was already initialized. Its     illegal to do so twice.");
    }

    final InputSystem activeInputSystem;
    if (relayInputSystem == null) {
       activeInputSystem = inputSystem;
    } else {
      activeInputSystem = relayInputSystem;
      relayInputSystem.setTargetInputSystem(inputSystem);
    }

    nifty = new Nifty(renderDevice, soundDevice, activeInputSystem, timeProvider);
  }

Is this a bug in the nifty for slick2d implementation or am i missing something? How am i supposed to handle nifty initialization over multiple states?

© Game Development or respective owner

Related posts about java

Related posts about slick2d