Multiple Nifty instances

I’m working on a game that is going to require a fairly complex UI system - a HUD, popup windows, menu overlay, social overlay, all of which should have different states and some of them might have draggable/resizable elements and may steal input from one-another based on the draw order.

I’m not currently aware of a way to define nifty layers of a screen in seperate xml files, so for lack of a desire to bloat a single nifty xml to 5000+ lines of code, I’d like to keep things neatly seperated in their own screens and nifty instances if possible, but do correct me if there’s some obvious easy way to do this.

Now, my problem is that when trying to initialize the seperate nifty instances, I get the following error:

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? at org.bushe.swing.event.EventServiceLocator.setEventService( at de.lessvoid.nifty.Nifty.initalizeEventBus( at de.lessvoid.nifty.Nifty.initialize( at de.lessvoid.nifty.Nifty.<init>( at com.jme3.niftygui.NiftyJmeDisplay.<init>(

This basically tells me that nifty has a hardcoded name for its event bus which makes me think i might have to dig into the sourcecode and get dirty with it. As a note, I have tried to use seperate viewports for each nifty display as suggested in another thread, made no difference.

I am having issues trying to access the online jmonkey docs so if someone could point out what would be the best course of action, and if it’s indeed possible to import other XMLs in the middle of a screen/layer definition, that would be much appreciated!

I imagine it would be a very large job to get separate instances of nifty to play nice together. I’m not exactly sure why you think you need separate instances or why you would want to. One of the greatest speed improvements Nifty has made in recent years is batch rendering which wouldn’t work with separate instances.

The easiest way to get that separation if you want to use xml would be to have everything in your layer be a nifty component which could have its own xml file. Then you would have one file that has all the layers defined and under each layer you put the component. That way you can separate out all of the nifty code. I tend to have a lot of java code as well since there are so many dynamic elements.

There is a way, too use multiple xmls, and it’s very unptactical too use multiple Nifties.
Read the wiki:

That’s not quite what I had in mind. I have already been using multiple files, that’s not technically the problem, the issue is that i want a very complex UI system where i am not forced to use a single screen.

Take this example:

I want to have a game manual accessible from any point of the game. The main menu, the options menu, the loading screen, multiplayer lobby, ingame hud screen, ANYWHERE. It’s a complicated strategy game that has a learning curve and it must be possible to read on at the player’s absolute convenience.

Now, as far as I am aware there are two ways to go about this:

-Either I create a copy of the manual structure on every screen. This also means i have to transfer the contents every time I switch the screen. Example, if the player is reading up on something while in a multiplayer game lobby and the host player starts the game, the screen may switch to a loading screen and then the hud. Copying the currently viewed pages of the manual (we’re talking dozens of pages, with images not just text), that’s quite redundant and excessive.

-Use only one screen ever in the game, and what was supposed to be done through screens would be done via layers. This is an absolute mess unless there is a way to stitch code in like this:

    <other xml file that adds a layer />
       <other xml file that adds the typical content of a layer that will be present on all screens />

Please let me know if there is a way to do something like this.

I’ll provide a couple more examples of why this is important:

Not being able to access the ingame menu from a random appState to adjust volume is just malicious to the player. I can do that technically by switching the screens out but then he/she might miss key events that are happening on the ingame screen, so an overlay would be necessary. Without the code injection demonstrated above (which afaik might not be possible?) I’d be forced to copy the ingame menu over to EVERY SCREEN, bloating everything beyond imagination.

I also want to have a chat overlay that can display messages sent by friends or the server to appear in every appState. You should be able to see an ingame notification that the master server is under maintenance, regardless of whether you’re in the options or the loading screen or wherever.

Could you not use several popups? One for the help menu, one for the options menu, …
Popups are defined separately and can be added to any screen.
That’s what I’m typically doing.

Ah yes, I see your problem now. I believe the usend of multiple Nifties results in several problems.

I suggest you do the following:
Create one screen (yes, I said one).
Put a h*ll lot of layers in it.
In these layers you have popups (or buttons, you know, basic panel stuff)

How does this work? Nifty can only show one screen, and you have to tell it which one it is. In your case, let’s call it “in-game”. This screen can, however use multiple layers, all active. If you want to show ano inventory, just show/unhide the element. You can structure it with an inventory-like layer, a help layer, an hud layer, etc.
The way to make sure only specific elements can be opened at specific times, is easier , you just check whenever the input is called, let’s call the input “map_options”, whether the correct elements are shown, let’s say the “map” screen.

However, if you don’t manage to solve anything, maybe you should create your own nifty controls, like a special window or something.

Another question for you: how much experience do you have with nifty? Have you created more guis like these?.

Thanks for the tip, I’ll look into popups. Strange that I’ve not heard about them before.

I have some experience. I have a game release under my belt that used nifty, so i’ve touched a number of different aspects of it, but I’ve always been disappointed at the lack of thorough documentation and the fact that jme doesn’t bundle the source/javadocs with it, making it a chore to track down the proper matching versions.
A lot of google searches / old forum posts point to a wiki that no longer seems to exist (just forwards to the github DL page), and the documentation on the github wiki is very limited and incomplete. I’ve mostly learned from there and a lot of trial and error.

As for your suggestion, it’s basically what I listed as one of two possible solutions in my previous post. I’m reluctant to jump into using it because it definitely creates a bloated file which becomes hard to maintain, unless there were injection commands in nifty to link parts of a screen/layer, which there donesn’t seem to be.

I’ll look into the popup/control approach though, hoping that it will make it bearable.

Thank you all for the input.

It is partially the same, but in my suggestion you use only one screen from where you can get all the information from.

What I understood from old posts, is that nifty was created out of lack of a proper system. Feel free to make your own.
The only part you can customise in nifty are controls and styles. You can make your own how you feel like, but there are some limitations.
Of course, if you want to go completely monkey balls, you could also rewrite some of the source.

I’m still at a loss as to why you need multiple screens in order to pull off your gui. What you described just sounds like different layers.

The game that I worked on before had 5200 lines in its xml file. Poor practice on my part sure, because there were seperate screens, but it would be the same amount of lines if i replaced the screen lines with layers.

Now, this game was not even that heavy on the UI, but the upcoming game will be. Even if i rotated my monitor to portrait mode, navigating those lines would be a nightmare. Never again.

I don’t think it’s an outrageous design principle.

Yes, but separating each of those layers into components that have their own files would allow you to logically separate those components. Then your screen line count would only be 3X(num of layers). So you wouldn’t have the same number of lines in one file.

Correct, that’s why I asked in the first post if there was a simpler way to go about this. I’ve never said I have a compulsion to use multiple screens, just that it may have been a solution in case multiple nifty instances were a thing, which has since been answered with a no.

A small note about the documentation:
I use the following pdf file Download nifty-gui from for the general documentation,
and the wiki page Home · nifty-gui/nifty-gui Wiki · GitHub for the controls.
That’s all I have found.
The pdf explains the popups (more or less) well.