Nifty: Not possible to change variables

Hello, i am using following appState to manage my Gui - Program interactions:

[java]

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */

    package mygame.AppStates;



    import com.jme3.app.Application;

    import com.jme3.app.SimpleApplication;

    import com.jme3.app.state.AbstractAppState;

    import com.jme3.app.state.AppStateManager;

    import com.jme3.niftygui.NiftyJmeDisplay;

    import de.lessvoid.nifty.Nifty;

    import de.lessvoid.nifty.screen.Screen;

    import de.lessvoid.nifty.screen.ScreenController;



    /**

    *
  • @author zuegg

    */

    public class GuiState extends AbstractAppState implements ScreenController{

    final int GameStart=1;

    final int GamePreLevel=2;

    SimpleApplication app;

    NiftyJmeDisplay niftyDisplay;

    Nifty nifty;



    int currentDisplay=0;

    public int requestedDisplay=0;



    @Override

    public void update(float tpf) {

    System.out.println(this.requestedDisplay);

    if(this.currentDisplay!=this.requestedDisplay){

    switch(this.requestedDisplay){

    case GameStart:{

    this.app.getStateManager().getState(ScoreState.class).resetGame();

    this.app.getStateManager().getState(ScoreState.class).clearLevel();

    this.app.getStateManager().getState(TowerState.class).clearTower();

    this.app.getStateManager().getState(TowerState.class).buildTower(this.app.getStateManager().getState(ScoreState.class).currentLevel);

    this.currentDisplay=this.requestedDisplay;

    this.requestedDisplay=this.GamePreLevel;

    break;

    }

    }

    }

    }



    @Override

    public void initialize(AppStateManager stateManager, Application app) {

    this.app = (SimpleApplication) app;

    niftyDisplay = new NiftyJmeDisplay(app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());

    nifty = niftyDisplay.getNifty();

    nifty.fromXml("Interface/Menu.xml", "start");

    app.getGuiViewPort().addProcessor(niftyDisplay);

    }



    public void gameStart(){

    this.requestedDisplay=this.GameStart;

    System.out.println("request game start");

    }



    }



    [/java]



    The problem is that even gameStart() is called correctly, the variable requestedDisplay in the update loop never gets changed.



    How to solve that problem?

Are you really sure that your variable doesn’t change? Put a System.out. in the end of update() to proof?



PS: You might consider an enum to tag your screen states. Thats much better than using int-values.

@enum said:
Are you really sure that your variable doesn't change? Put a System.out. in the end of update() to proof?

PS: You might consider an enum to tag your screen states. Thats much better than using int-values.


there is a Sysout in the beginning of the update() which always prints 0.
the Sysout in the gameStart() prints correctly "request game start" so the call works correctly

How do you get the instance of your GuiState class?

Did you create your own instance and pass it to Nifty as a ScreenController for your screen (e.g. at fromXml(fileName, startScreenId, screenControllerClass)) or did you get the Nifty-created instance (that Nifty creates when starting your screen) with nifty.getScreen(screenId).getScreenController())?



This looks like a problem that is common with Nifty, that perhaps you let Nifty create its own instance (not giving it an instance in fromXml) and then created your own instance of the class (GuiState), ending in two separate instances. If that’s the case, I’m guessing you’re calling the gameStart() method on one of them and the update method is called on the other.

1 Like
@Tumaini said:
How do you get the instance of your GuiState class?
Did you create your own instance and pass it to Nifty as a ScreenController for your screen (e.g. at fromXml(fileName, startScreenId, screenControllerClass)) or did you get the Nifty-created instance (that Nifty creates when starting your screen) with nifty.getScreen(screenId).getScreenController())?

This looks like a problem that is common with Nifty, that perhaps you let Nifty create its own instance (not giving it an instance in fromXml) and then created your own instance of the class (GuiState), ending in two separate instances. If that's the case, I'm guessing you're calling the gameStart() method on one of them and the update method is called on the other.


Oh god, thank you very much. The solution for the problem was to change:
[java]
nifty.fromXml("Interface/Menu.xml", "start");
[/java]
to
[java]
nifty.fromXml("Interface/Menu.xml", "start",this);
[/java]