[SOLVED] Unable to detach AppState with NiftyGUI

Title basically says it all. I’m trying to detach AppStateMyHud that have all the Nifty Code inside and I can’t. I tried putting nifty.exit() in cleanup method but it doesn’t help either… Gui is still there (and still updating as it has a Label that tracks my mouse position).

How do I completely close Nifty GUI along with appstate in which it resides?

Did you change the screen to “exit”, a end screen that draws nothing.

Not detaching was my mistake after all. I forgot super.initialize statement in my appstate init method.

But now detaching and exiting gives me (even though it works):

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?

Why is that? I thought nifty.exit() destroys the instance completely so there shouldn’t be any service existing…

Nifty is created only once and in 3.0 (haven’t checked in the master 3.1) you cannot create a new nifty instance or you will get that error.

My workaround was to use the different screens in nifty and have a controller for each screen.

Yeah but I want to be able to kill Nifty completely or at least detach it completely. If I did then there shouldn’t be trouble creating new one right?

Or should I just change my init method to include something like:
if niftydisplay exists add it to view port
if niftydisplay doesn't exist create new one and add it to view port
…and accept the fact that this sonofagui is unkillable? And if so, how do I do that?

And most importantly - how do I check if nifty instance exists?

You described the problem already - the rest is basic programming. A simple variable that you check for null would do.

Obviously I did check for null for:

  • nifty,

  • niftydisplay,
    For both it didn’t work, it still instantiates itself and sells me a warning.
    I had:

    if (nifty == null) {
    create instance stuff here


if (niftyjmedisplay == null) {
create instance stuff here

I even searched for something like guiViewPort.hasProcessor(niftyJmeDisplay) but there isn’t such a method. Any other ideas?

Correct me if I am wrong, but you want to cleanup your whole screen regarding gui elements, right? Wouldn’t it be easier to remove the screen or all layers itself instead of removing nifty?

I want to be able to detach my appstate completely with everything that this appstate created (in this case nifty) so there’s no trace of it left anywhere (except for some trash left for garbage collector to take care of).
If it was just the screen I’d just change nifty screen I have now to empty nifty screen.

Well this will not be possible… you cannot exit nifty completely. You have to use the specific instance of nifty after the init process. Easiest way is to use different screens as bloodwalker mentioned.

If you create a global nifty variable and assign the nifty instance you create its impossible that the null check doesn’t work. You did something wrong.

Solved the issue: it wasn’t related to creating nifty per se, but to creating appstate in which nifty resides. In the end right solution (and probably the only one) to switch appstate with nifty on/off is either:

  • Check if appstate already exists instead of forcing creation and relying on the fact that second “same” appstate overrides previous (kind-of-a-singleton),
  • Force garbage collector after detaching appstate, so whatever is left of nifty (and there is a lot) is utterly destroyed the moment appstate is detached.
    I chose the first solution because forcing garbage collector is kinda slow.