Updating fields on Nifty screen

Hello,

what I’m trying to do is to fill the Nifty text elemnt out with custom data from my Java code. Here is XML code:

[xml]<text text="${CALL.getName()}" id=“name” font=“aurulent-sans-16.fnt” color="#000f" align=“center” valign=“center” />[/xml]

Here is Java code:
[java]
public class Main extends SimpleApplication implements Savable, ScreenController, Controller {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
private String str;

public void onAction(String name, boolean keyPressed, float tpf) {

str= closest.getGeometry().getName();
System.out.println(“INITIALIZED HERE” + str); //debug attached below
}
public String getName() {
System.out.println(“ALREADY INITIALIZED???” + str); //debug attached below
return str + “OLOLOLOLO”;
}

}
[/java]

According to this code I have got on my Nifty screen “nullOLOLOLOLO”. On debugging log I’ve got:

line 152: INITIALIZED HEREdragon // (looks nice)

line 267: ALREADY INITIALIZED???null //:frowning:

What I did wrong? Thanks alot.

You probably haven’t given nifty the SimpleApplication as a ScreenController so it’s created a new blank one. Register the screen controller before switching to the screen.

1 Like

Still didn’t get it.

This how I think I register the screen controller (might be wrong?):
[xml]
<screen id=“start” controller=“mygame.Main”>
[/xml]

And this how I think I switch to the screen:
[java]
private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {
  if (name.equals("Shoot") &amp;&amp; !keyPressed) {
    CollisionResults results = new CollisionResults();
    Ray ray = new Ray(cam.getLocation(), cam.getDirection());
    shootables.collideWith(ray, results);

    String hit = "";
    for (int i = 0; i &lt; results.size(); i++) {
      float dist = results.getCollision(i).getDistance();
      Vector3f pt = results.getCollision(i).getContactPoint();
      hit = results.getCollision(i).getGeometry().getName();
    }
    if (results.size() &gt; 0) {
      CollisionResult closest = results.getClosestCollision();
      mark.setLocalTranslation(closest.getContactPoint());
      str= closest.getGeometry().getName();
      System.out.println("INITIALIZED HERE" + str);
      rootNode.attachChild(mark);

      niftyDisplay = new NiftyJmeDisplay(assetManager,
            inputManager,
            audioRenderer,
            guiViewPort);
      nifty = niftyDisplay.getNifty();
      nifty.fromXml("Interface/newXMLDocument.xml", "start");
      guiViewPort.addProcessor(niftyDisplay);
      inputManager.setCursorVisible(true);
    } else {
      rootNode.detachChild(mark);
      flyCam.setEnabled(true);
      flyCam.setMoveSpeed(20);
      inputManager.setCursorVisible(false);
    }
  } else if (name.equals("Pause") &amp;&amp; !keyPressed) {

      } else {

      }
  }
}

};
[/java]
Thank you.

do

[java]nifty.fromXml(“Interface/newXMLDocument.xml”, “start”, this);[/java]

as, zarch was saying, you need register the screencontroller with nifty. You can also do
[java]nifty.registerScreenController (this); [/java]

before adding any screens

1 Like

Thank you, guys! It halped.