A method called by nifty won't detach states

Hi, I have a GUI overlayed on my game AppState. When I hit quit, I want to go back to main menu and detach the game AppState (and detach all children from rootNode).

The method quitGame() is called when I click the QUIT button and the GUI changes to main menu but it does not detach the state.



[java]public void quitGame() {

nifty.gotoScreen("start");

stateManager.detach(this);

}[/java]



By the way my AppStates are implementing ScreenControllers. But I am not sure how to use the [java]bind(Nifty nifty, Screen screen)[/java] method, because I have two AppStates → two ScreenControllers and in the XML file for nifty I bind each of these AppStates to two different screens.

in this AppState I have a method:



[java]@Override

public void stateDetached(AppStateManager stateManager) {

System.out.println(“GAME DETACHED”);

}[/java]



And it never gets called, that’s how I know the state was not detached. Also when I attach the state again from main menu, the objects are initialized again so the object count doubles. 8O

The pattern I use is to enable/disable (or attach/detach) the state for the screen in the nifty start/end screen callbacks.



Just remember that nifty screens and application states are completely separate systems. Your code is what needs to glue them together appropriately to your needs.

@vojta-polivka said:
Hi, I have a GUI overlayed on my game AppState. When I hit quit, I want to go back to main menu and detach the game AppState (and detach all children from rootNode).
The method quitGame() is called when I click the QUIT button and the GUI changes to main menu but it does not detach the state.

[java]public void quitGame() {
nifty.gotoScreen("start");
stateManager.detach(this);
}[/java]

By the way my AppStates are implementing ScreenControllers. But I am not sure how to use the [java]bind(Nifty nifty, Screen screen)[/java] method, because I have two AppStates -> two ScreenControllers and in the XML file for nifty I bind each of these AppStates to two different screens.


@vojta-polivka said:
in this AppState I have a method:

[java]@Override
public void stateDetached(AppStateManager stateManager) {
System.out.println("GAME DETACHED");
}[/java]

And it never gets called, that's how I know the state was not detached. Also when I attach the state again from main menu, the objects are initialized again so the object count doubles. 8O


Do you have a similar "GAME ATTACHED" debug statement in the stateAttached() method? Do you see that message? Where do you attach the state?

Also try putting System.out.println()s at the beginning and end of your quiteGame() method. I sort of half remember that exceptions thrown from these kinds of methods get swallowed but maybe that has been fixed since I last updated.

Thanks, I have refactored my game and made ScreenController separate from AppState. I honestly don’t see any advantage of implementing ScreenController into AppState, because it just makes my head spin. I initialize my app from a ScreenController constructor and that way I can access everything in my app. (I hope I am not doing something wrong).



In the ScreenController I have couple methods calling nifty.gotoScreen(xyz) and sometimes they attach/detach states. It works now. I think I had a little trouble understanding how AppStates really work. :slight_smile:



[java]

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



public class GUIController implements ScreenController{



private Nifty nifty;

private Screen screen;

private MyApp app; //extends SimpleApplication



// My custom constructor to initialize the app field, called from the MyApp object in SimpleInit() this way:

// nifty.fromXml(“Interface/screen.xml”, “start”, new GUIController(this))



public GUIController(MyApp app) {

this.app = app;

}



/* This method is called in the XML by nifty */

public void createGame() {

nifty.gotoScreen(“hud”); // nifty switches to different screen

app.getStateManager().attach(new GameAppState()); // new game state, initializes the game objects

app.getStateManager().detach(app.getStateManager().getState(MenuAppState.class)); // detaching the menu state?

}



// continues…

[/java]



Pls let me know if this is how it is supposed to work.