[Solved] Missing something on how to use addXml instead of fromXml

Hi all,



I am trying to make a game wich mainly needs only the GUY on his first stage. Therefore i try using nifty since a little more than a week.



This topic is about getting from using the fromXml method to the addXml one. I used as a basis the Main3.java to try this (the one in the tutorial package of the NiftyGuiDemo.zip).



In Main3.java i replaced the following code :

[java]

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

Nifty nifty = niftyDisplay.getNifty();

guiViewPort.addProcessor(niftyDisplay);

nifty.fromXml(“Interface/tutorial/screen3.xml”, “start”, startScreen);

[/java]



By this one :

[java]

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);

Nifty nifty = niftyDisplay.getNifty();

nifty.addXml(“Interface/tutorial/screen3.xml”);

nifty.gotoScreen(“start”);

MyStartScreen screenControl = (MyStartScreen) nifty.getScreen(“start”).getScreenController();

nifty.addControls();

stateManager.attach(screenControl);

guiViewPort.addProcessor(niftyDisplay);



startScreen = new MyStartScreen();

stateManager.attach(startScreen);

[/java]



My issue is on the screen controller (MyStartScreen.java)

[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]



If I delete the content of the update method (only the content not the method herself) i get a crash :

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

java.lang.NullPointerException

at tutorial.MyStartScreen.update(MyStartScreen.java:58)




Else it works ok.



Would you please have a clue? Code sample would also be appreciated since english ain’t my native language :s



Thanks in advance.

Why not do:



[java]

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);

Nifty nifty = niftyDisplay.getNifty();

nifty.registerScreenController(screenControl);

nifty.addXml("Interface/tutorial/screen3.xml");

nifty.gotoScreen("start");

[/java]



The update() is called because you are an app state, not because of nifty. You must have something set up wrong somewhere.

2 Likes

@zarch : thank you for your quick reply!



So i tried from your code sample this :

[java]

startScreen = new MyStartScreen();

stateManager.attach(startScreen);



NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);

Nifty nifty = niftyDisplay.getNifty();

nifty.registerScreenController(startScreen);

nifty.addXml(“Interface/tutorial/screen3.xml”);

nifty.gotoScreen(“start”);



guiViewPort.addProcessor(niftyDisplay);



[/java]

And it works!



I will let this topic opened until tonigth so i can try to switch between 2 screens loaded in different xml files. If it works i’ll post the code for those who are interested, else I’ll inquiry your help again.



Thanks again.

I was just telling myself the same thing! so i edited my reply and deleted the 2 useless lines.



Can i please as asked let the topic opened until i try to switch between 2 screens. If i succeed i’ll post the code so others noobs as i am might find it usefull, if I fail i won’t spam topics :).

2 Likes

Why are you still extracting the screen controller even though you already set it?



Put a [Solved] tag in the topic (use edit topic button at the top) if the issue is fixed :slight_smile:

1 Like

Ok second test conclued happily so I’ll close definitely the topic.



To switch between different screens using addXml based on the Main3.class from the tutorial package of the NiftyGuiDemo.zip :


  1. create a new XML screen file, called in my case test.xml and of course with a screen definition inside!
  2. define in the XML the controller of the screen, you can either reuse the same (MyStartScreen.java) or create another one
  3. add the XML to nifty with :

    [java]

    nifty.addXml("Interface/tutorial/test.xml");

    [/java]
  4. just switch anywhere (e.g. : inside a Button called method) by a simple gotoScreen :

    [java]

    nifty.gotoScreen("testScreen");

    [/java]



    Thanks again to zarch for his help.
1 Like