Nifty and AppStates

Okay, so basically, my probably is that while my game works fine, I get this warning:

WARNING: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time? 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?

which I know indicates that I have multiple instances of nifty running. And I know exactly where I create multiple instances. What I need help in is understanding how to use one single nifty event, or how to properly close a nifty instance before opening another one in differing AppStates.

I thought I’d be able to create the nifty instance in my main, and then switch states. The state I have switched to would call the nifty.goToScreen blah blah blah. But I have no clue whether or not this thinking is right, because I can’t seem to figure out how to get the nifty instance (well, the niftyJmeDisplay too) from the main app.

What am I doing wrong? I don’t think this requires me posting my code, because I’m fairly certain it’s my lack of understanding nifty’s intertwining with app states.

I went the same route, and closing Nifty instances down doesn’t really work (even the jme3 code for that is commented out with a remark of “doesn’t work”). And, as you just discovered, Nifty isn’t built for multiple active instances at the same time (which is a shame because you can’t run multiple displays with Nifty in them at the same time, from within the same JVM).

I’m keeping a single global Nifty instance and switching screens, that’s been working well.

@toolforger said: I went the same route, and closing Nifty instances down doesn't really work (even the jme3 code for that is commented out with a remark of "doesn't work"). And, as you just discovered, Nifty isn't built for multiple active instances at the same time (which is a shame because you can't run multiple displays with Nifty in them at the same time, from within the same JVM).

I’m keeping a single global Nifty instance and switching screens, that’s been working well.

From his post, it sounds like his problem is more basic than that.

Quote: “because I can’t seem to figure out how to get the nifty instance (well, the niftyJmeDisplay too) from the main app.”

Sounds like he doesn’t know how to expose it or pass it to the states… which is more of a Java questions than a JME question.

Just pass the nifty reference to your app states…or store it somewhere central that all the app states can then access.

Okay, so I can do it the way toolforger said to do it, and I’m happy to do it that way too.

@pspeed and @zarch
how does one pass the nifty reference? I know how to pass the references for guiViewPort or settings and other things using their getter methods, but I don’t see how to “get” the nifty reference. Is there a getter that I don’t know of? Or should I just create my own inside the main app and have each app state call that getter to pass the nifty reference?

You created the nifty in your simpleInit right?

Since you created it you have it…store it or pass it around from there…

Well, I am trying to pass it around. I’m not quite sure what the right thing to do is. I’m currently making it static and trying to access it in the other states, but that isn’t working out too well. When I make it static and try to call it from another state, I get a NPE when using nifty.addXML or any method with nifty. So I’m obviously not passing it correctly.

Is it also an issue if I have that simpleInit in a main in a package separate from the rest of my classes?

Smoking hot potatoes, I got it. I was just using the static reference in the wrong area. Now I’ve maid the nifty in the main private static, and used a public static Nifty getNifty method. What did the trick was setting it inside the initialize phase. I assume that’s because if I set it in the constructor, I call the getNifty() method on a nifty object that had to yet to be instantiated, right?

Thanks for the tips guys.

Constructor is always the first thing called, therefore constructor is called before simpleInit is run, therefore nifty is null in your SimpleApplication at the point you tried to set it in your static, therefore null value when into static.

Okay, I will try to remember next time not to something as silly as that :slight_smile: Thanks!

This is an old topic but I found a way to create new nifty instances in a working way and I want to share my way with you so that the next person with that problem does no t have to search for hours like I did :wink:

After you are finished with all your nifty stuff (in my case: After I called my application to stop():wink: just add the following lines:

 try {
            EventServiceLocator.setEventService("NiftyEventBus", null);
        } catch (EventServiceExistsException ex) {}

Now you can create a new nifty instance if you want and it will work without problems.