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).
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?
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.
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…
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.