Nifty gui screens bind() method

I have several nifty screens. When nifty.gotoScreen(“screen-name”); is called, the bind() method of that screen controller is called automatically.
I want to initialize something of a screen before it gets visible by calling nifty.gotoScreen(“screen-name”);
When initializing my own things, I need a Nifty object. But the problem is that the screen hasn’t got that Nifty object yet, because the bind method is not called yet. Because the screen is not visible yet.

My situation: I have a loading screen for my game, and I want to show that screen while another screencontroller is filling a list with 300 nifty objects, and loading things from files. This takes a few seconds, and I expect to do all my loading stuff when the loading screen is visible.

I believe it’s fine to manually call bind on a controller and supply the screen. This of course requires you to create the controller before hand.

Edit: I actually found an example in my code where I don’t create the controller myself. Just got it from screen.getScreenController() and then called bind on it. Maybe it was for the same reason.

1 Like

I still have a few problems.
I am trying to show a loading screen while another screen gets filled with nifty elements.
For example I have twoo screens: loading-screen and game-hud-screen.
game-hud-screen has a listBox element. I want to fill that listBox with nifty elements, I am doing this with a loop. This process takes about 2 seconds, but there will be more to load in the future. While this loading and element creating process is in progress, I want to show the loading-screen. (Maybe with a progressbar).

I cant get this working. Nifty is always throwing exceptions. Most times my problem is that I never know when the loading process is finished.

It sounds like you could do with a Listener interface. Start the loading screen when you start populating the HUD (and other things). Then let the class that handles the loading notify nifty when it’s done and have it switch screens.
I’ve had some trouble with nifty and loading screens myself. I think it was if the loading is done too quick and gotoScreen is called before the previous screen has been shown (or processed properly). Then I got some kind of deadlock where none of the screens were shown. It was a while, though.

A progress bar is a whole different issue. Settle with something animated for starters. There’s a reason that’s a common thing nowadays.

1 Like

Thanks for your reply.
But I have never heard of a Listener interface, so what is is used for and in which screencontroller (loadingscreen / hud) should I implement it?

Google it. It can be your best friend sometimes :slight_smile:

Roughly:



public interface LoadingListener{

 public void onLoadStarted();

 public void onLoadFinished();
}

This interface is implemented by some class controlling screen switching.

Your class that handles loading stuff has a List (of LoadingListener). You add the class implementing the interface to that list.

Then, when loading starts or finishes, you iterate through the list and call the appropriate method.

It’s picked up by the implementing class and you can execute whatever code you want in those methods.

1 Like

Still having problems with Nifty.
I’m getting tired of “nifty” gui.

Summary of the problem I always have:
I do:

  1. Startscreen. Click on button “singleplayer”.
  2. Switch between appstates, make the loading screen visible.
  3. Load gamedata and populate the hud screen.
  4. Hide the loading screen, switch between appstates, and show the hud screen to start the actual game.

Result I get:

  1. Loading screen doesn’t become visible after clicking “Singleplayer”. The startscreen is still visible.
  2. Loading is in progress while startscreen is still visible.
  3. Loading done, and NOW my loadingscreen becomes visible.
  4. Switching to the hud screen does nothing. The loadingscreen keeps visible.

I think this has to do with the threads of Nifty. So i tried to load my game data within a seperate thread and that worked a little bit, but then I get exceptions:

at de.lessvoid.nifty.Nifty.addControlsWithoutStartScreen(Nifty.java:426)
	at de.lessvoid.nifty.controls.dynamic.PanelCreator.create(PanelCreator.java:30)
	at game.gui.InventoryList.createGuiItem(InventoryList.java:120)
	at game.gui.InventoryList.updatePanelAfterAdd(InventoryList.java:146)
	at game.gui.InventoryList.add(InventoryList.java:43)
	at game.gui.Inventory.fill(Inventory.java:36)
	at game.DefaultGameScreen.fillInventory(DefaultGameScreen.java:217)