Instance member not updating in app state/nifty screen

Hi guys,

I’m displaying a button at the bottom of the window-while the game is in it’s running state-on press I’m trying to update a boolean. Sounds simple enough but for some reason the method changes the boolean but as soon as we are back in the update loop it’s back to it’s original value! I really have no idea what’s going on.

I have a class that starts things off and extends SimpleApplication. Here’s the relevant code from there.

[java]
public void simpleInitApp() {
//System.out.println(“called”);
//viewPort.setBackgroundColor(ColorRGBA.Yellow);
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
/** Create a new NiftyGUI object /
nifty = niftyDisplay.getNifty();
/
* Read your XML and initialize your custom ScreenController */
//nifty.fromXml(“Interface/tutorial/step2/screen.xml”, “start”);
startScreen = new StartScreen(“hud”);
nifty.fromXml(“Interface/newNiftyGui.xml”, “start”, startScreen);
nifty.addXml(“Interface/hud.xml”);
// attach the Nifty display to the gui view port as a processor
guiViewPort.addProcessor(niftyDisplay);
// disable the fly cam
flyCam.setDragToRotate(true);
ss = new StopState();
ss.setEnabled(false);
this.getStateManager().attach(ss);
}[/java]
I’ve been meaning to change the name but currently stopState is the game running, start screen is the menus. Menus are working fine.
I cant for the life of me get the xml to display. but the relevant parts
controller=“jme3test.helloworld.StopState”

interact onClick=“resume()”

interact onClick=“pause()”

The controller is set to StopState, and the two buttons are registered to pause() and resume() method. StopState has a variable simPaused which I initilize to false in the constructor.
In the update loop I print it out. It is always false.
Here’s the two methods. I know the methods are linked to the buttons properly because the print statements are working.

Any ideas?

Thanks
[java]
public void pause() {

    System.out.print("entered pause");
    simPaused = true;
    
    
    
}

public void resume(){
    System.out.println("entered resume");
    this.simPaused = false;
    System.out.println(simPaused + " INSIDE RESUME");
    
}

[/java]

My guess is that if you changed:
System.out.print(“entered pause”);

To:
System.out.print(“entered pause this:" + this);

And then also print “this” in update, you will see that they are actually different objects for some reason.

I agree to what pspeed said.
There are two different objects of StopState.
If you do not specify an instance of your screen controller, then nifty will create one dynamically.
Try passing your screen controller to the nifty initializing method.

[java]nifty.fromXml(“Interface/newNiftyGui.xml”, “start”, startScreen, ss);[/java]

Thanks guys! That was exactly it!

I was creating a state
and nifty was creating one too!

I have another related question and I don’t really want to clutter up the forum more.

If I want to restart the states so it’s basically the same as first launch. i.e the user clicks a “restart game” button. How would I do this?
Currently I show a ‘finished/game over screen’ and do the following:
[java]
flyCam.setDragToRotate(true);

        ss.setEnabled(false);
        ss.cleanup();
        this.getStateManager().detach(ss);
        startScreen.setEnabled(true);

[/java]
This should be removing the ingame state?

if the user presses restart:

[java]
startScreen.runAgain = false;
startScreen.cleanup();
this.getStateManager().detach(startScreen);

          this.getStateManager().attach(this.startScreen);
        this.getStateManager().attach(this.ss);

[/java]

start a new game state?

However I’m getting weird behaviour the second time round, I also notice some of the screen elements controlled by StopState have retained their values from the first run, for example a checkbox is still checked?

Thanks

Are you destroying and recreating the AppState?

Otherwise, it (like any other instance) would remain the same.

It’s probably simpler to use the same app state and just have it tear down/start up itself.

We actually have models that are separate from the app state in an MVC type scenario. The View is the scene graph, the model knows the logical state, the app state is the controller keeping the two synchronized. For a new game the AppState remains the same but a new model is started.

I wasn’t, so I tried to destroy and recreate by adding this:

[java] startScreen = null;
startScreen = new StartScreen(“hud”);
this.getStateManager().attach(this.startScreen);
[/java]

I’m still getting previously selected GUI elements showing as selected?

Do I have to remove the state as a nifty controller somehow and then add it again?

I create and register the controlers to the nifty display (for the first time on initilisation) like this

[java]
startScreen = new StartScreen(“hud”);
nifty.fromXml(“Interface/newNiftyGui.xml”, “start”, startScreen);

// attach the Nifty display to the gui view port as a processor
guiViewPort.addProcessor(niftyDisplay);
// disable the fly cam
flyCam.setDragToRotate(true);
ss = new StopState();
ss.setEnabled(false);
nifty.registerScreenController(ss);
nifty.addXml(“Interface/hud.xml”);
this.getStateManager().attach(ss);
[/java]