Nifty GUI Progress > update/variable

Hi all.

  1. I’m progressing NiftyGuiDemo and I’m trying to figure out how work writing (variable) to GUI. And i don’t understand why if i start “Screen3.java” => in “hud” screen score is set to basic “123” and if i start “Main3.java” file > in same screen(hud) score work as it should (by method in controller). Everything seems be similar… both GUIs use same controller (MyStartScreen.java).

    Can you help me with understand it PLS.


  2. Another task > In topic http://hub.jmonkeyengine.org/groups/gui/forum/topic/niftygui-use-a-variable-from-simpleapplication-in-screencontroller/

    I will need write score variable to GUI. Solution in this topic seems be like single “shot” (for variable which won’t change). It is true or can i use it for the changing variable like “score” is?



    THX for answers. I will be really glad…

    *sry for my english

http://hub.jmonkeyengine.org/groups/gui/forum/topic/niftygui-use-a-variable-from-simpleapplication-in-screencontroller/#post-136013



AppStates ;).

I mean this parts:



[java]

control(new LabelBuilder(){{

id("score");

color("#000");

text("123");

width("100%");

height("100%");

}});

[/java]

and

[java]

public void update(float tpf) {

if (screen.getScreenId().equals("hud")) {

Element niftyElement = nifty.getCurrentScreen().findElementByName("score");

niftyElement.getRenderer(TextRenderer.class).setText(tpf*1000/10 + ""); // fake score

}

}

[/java]

this do not work in my app

Your update code looks valid, I certainly do similar and it works. I’d do String.valueOf() not +"" though. I’m not sure if the compiler will be smart enough to do +"" without a stringbuilder.



Put a log statement in to make sure the code is getting called. Also keep in mind that tpf may well be the same each time so you might want to use an incrementing variable or something.

Finally i continue… BUT

But my code is doing error when i swith to empty screen(if i go to from menu to setup > it work). In my ScreenControler:



[java]

public void update(float tpf) {

if (screen.getScreenId().equals(“menu”)) {

Element niftyElement = nifty.getCurrentScreen().findElementByName(“textScoteALL”);

niftyElement.getRenderer(TextRenderer.class).setText("Score: " + score + " Top Score: " + topScore); //THIS LINE

}

}

[/java]



[java]

IV 07, 2012 2:50:37 ODP. com.jme3.app.Application handleError

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at mygame.MyScreenController.update(MyScreenController.java:105)

at com.jme3.app.state.AppStateManager.update(AppStateManager.java:249)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:255)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Thread.java:722)

[/java]



Error show when i try close “menu” screen (change to empty screen). Before i did this code for update text everything work fine.

EDIT: I tryed move this code to some metod and update text only when is value “score” would change but seems that i can’t do it when screen(with this text) is not active…

And one more question:

What is better? > set screen by screencontroller or by nifty in my Main class (game).

[java]nifty.gotoScreen("gameOver");

startScreen.toScreen("gameOver");//screencontroller(my metod)[/java]

Without looking in detail at what you are doing I expect that if the screen isn’t empty then you can’t find the component in order to set the text as the element isn’t on the active screen at that time. Only try and update the text if the screen is active and when the screen is switched to then make sure it is updated to the correct value at that time.

Thx man. I fix this… I created method which i call after opened screen with text. Code will do only once and everything is fine. I don’t know why this solition i didn’t think faster.

One more thx…