Nifty - Updating property

Hello, how can I update/replace property value?

I’m using property “HP” which get current player HP and print it on screen, so it’s Life: 100 on the game start.
But when player lose HP I want to replace that 100 to new value. How to do that?

I tried:

        prop.remove("HP");
        prop.setProperty("HP", Integer.toString(playerClass.hp));
        nifty.update();

but there’s no result

Hi,

I don´t know if its the “best” way, but I would solve this in the following way:

Are you using a xml for your HUD(?) ?

if yes,

(i) replace the value “100” with “$CALL.METHOD_OF_SCREENCONTROLLER”
(ii) $CALL.METHOD_OF_SCREENCONTROLLER --> in your screencontroller you define a method which will request the latest player HP of another class
(iii) ‘reload’ the xml (for example via nifty.addScreen(FILENAME):wink:

if no,

(i) get the current screen via nifty
(ii) get id of the text(?) element
(iii) replace the value
(iv) reload layout (I am not 100% sure how, but maybe nifty.getCurrentScreen().layoutLayers(); could help)

I hope that helps. :wink:

Thank You for answer but it didn’t helped ;/ I’m calling from nifty.xml getHP method which return HP and in updateLoop i used: updateGUI method which contain nifty.update(); but no effect

Can you show me your xml and your controller?

Part from GUI.xml

      <panel id="hpPanel" height="17" width ="100" childLayout="center"> 
         <control name="label" font="Interface/Fonts/SegoeUISemibold.fnt" align="left"  text=" Life: ${CALL.getPlayerHP()}" />
      </panel>

GameState.java

                nifty = niftyDisplay.getNifty();
		nifty.fromXml("Interface/GUI.xml", "game", this); 
		game.getGuiViewPort().addProcessor(niftyDisplay);
...........
	public void bind(Nifty nifty, Screen screen) {
		this.nifty = nifty;
		nifty.registerScreenController(this);
		nifty.addControls();
	}

	public void onStartScreen() {
	}
    

	public void onEndScreen() {
	}  
............
@Override
	public void update(float tpf) {
		time += tpf;
                updateGUI();
.......
    public int getPlayerHP() {
        return playerClass.hp;
    }
    
    public void updateGUI() {
        nifty.update();
    }

My advice for a “fast fix” would be that you reload the xml every time the hp changes. Which means:

  1. make it possible to access the nifty object via external classes
  2. access nifty object and put nifty.addXML(“filesource”) in the hp controller method - that will reload the whole xml and will request the new value

Hmm, but I think nifty.add will create other instancies of XML not overload current. It can affect in bad performance

As I said, that would be the easy way, but of course you can also try to use a screen related solution, like

// find old text
Element niftyElement = nifty.getCurrentScreen().findElementByName(“score”);
// swap old with new text
niftyElement.getRenderer(TextRenderer.class).setText(“124”);

screen.layoutLayers(); (screen = nifty.getCurrentScreen())

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_java_interaction

Hmm, i returned to Properties solution and found that they are working but only GUI is “not refreshed”

so now i get first HP from GUI by calling $PROP.HP and later after damage playerclass call controler to updateHP

    public void updateHP() {
        prop.remove("HP");
        prop.setProperty("HP", Integer.toString(this.getPlayerHP()));
        nifty.setGlobalProperties(prop);
        nifty.update();
       }

I checked with System.out.print that the prop.HP is setting to correct value just need to force refreshing that HP Text label

Here´s an example I used yesterday without problems. I have no clue about your solution.

Screen screen;
Element element;
            
screen = NiftyDisplay.getNifty().getCurrentScreen();
element = screen.findElementByName("Content-Element"); //Content-Element: ID of my Element which has the specific item which I want to change
element.getRenderer(TextRenderer.class).setText("Test");

The text is changed without refreshing anything else.