Nifty GUI rendering blocks in the initialize method?

Hi,

I have been using nifty and I have defined it in the initialize() method of one of my AppState(which implements ScreenController) as follow.

[java] niftyDisplay = new NiftyJmeDisplay(app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());

    // Create a new nifty GUI object
    nifty = niftyDisplay.getNifty();

    // Read your XML and initialize your custom ScreenController
    nifty.fromXml("Interface/startScreen.xml", "startScreen", this);
    nifty.addXml("Interface/moduleScreen.xml");
    app.getGuiViewPort().addProcessor(niftyDisplay);

[/java]

However I’m not clear when in which thread nifty’s GUI renders because when I try to do any computations in the same AppState OnClick() methods related to nifty, it simply blocks the GUI rendering.

Say for example, I have the following code

[java] public void onQuit() {
System.out.println(“On Quit!!”);

    de.lessvoid.nifty.elements.Element niftyElementButton = nifty.getScreen("endScreen").findElementByName("QuitModuleButton");
    de.lessvoid.nifty.elements.Element niftyElement = nifty.getScreen("endScreen").findElementByName("score");

    niftyElementButton.disable();

   // DOING VERY COMPUTATIONAL PROCESS OR
   // DOING NETWORK COMMUNICATION THAT HAS POTENTIAL TO BLOCK OR
   // Thread.sleep(10000);

}[/java]

The niftyElementButton.disable(); action does not reflect instantly. I tried doing app.enqueue() and it works well but when I write another app.enqueue() inside the previous again my process blocks.

The functionality that I want is when he clicks OnQuit(), the button should immediately disable so that he can’t press it again. Then I want to do some network communication (which can take time). Mean while nifty should display “Please Wait” in one of the Label elements. Uptill this time all good using app.enqueue(). Then I want if the network communication is successful, there should be a message that “Your communication was successful”. Then after a 10s delay the application should exit automatically. Displaying the “You communcation was …” and making sleep requires anoteher app.enqueue() which will nest in the previous callable. Somehow this don;t work and it again blocks and the message “Your commnicat…” never appears.

I’m not sure how you are doing your network communication from the post, but if you are doing anything computationally intensive in an onClick method you should really move that to a separate thread. The app.enqueue() works because you make the change right before you render in the render thread. When you sleep in app.enqueue() you actually sleep the render thread which causes a hang and means that your gui elements will never update.

this tutorial is for making a proper loading bar that animated while you do work on another thread: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:loading_screen

its more meant for actually loading the game, but you could easily adapt it for what youre doing.