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.setProperty("HP", Integer.toString(playerClass.hp));

but there’s no result


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()}" />

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

	public void onStartScreen() {

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

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

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

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.setProperty("HP", Integer.toString(this.getPlayerHP()));

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

The text is changed without refreshing anything else.