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 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)
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)
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
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”);
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.