Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
"Perhaps multiple threads tried to create a service about the same time?" I'm not familiar with Slick, or even Java, but what about a global mutex, so initNifty can only be called once another has finished. Is that possible or am I way off? –  Luke San Antonio Jul 12 '13 at 1:26
add comment

1 Answer 1

I am not familiar with the Nifty GUI, so take this advice with a grain of salt, but I would suggest trying to determine if you really need to initialize the GUI multiple times. It might be better to simply initialize once, and use other methods to make the changes you need when you change states.

If this is the case, you can either store a boolean value to track whether you have initialized the GUI already, and check this before calling initNifty. If you have already initialized it, simply skip the initialization call.

share|improve this answer
    
I actually can't do that. Nifty will create a new instance for each new state so the nifty object i have in my state will never be initialized. The problem is that this new Nifty instance HAS TO be initialized, even if i don't use it in my state. So a call to initNifty is needed in each states, which lead to my problem since it will reinitialize the event bus with the same name (i looked the code) and then raisethe exception. I'm really starting to think that there is something wrong in the slick2d nifty implementation. –  nathan Nov 13 '12 at 19:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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