Getting to Know StandardGame and GameStates

My project hit a critical mass of unwieldliness and some redesign is now underway.  I decided to explore StandardGame and GameStates to give me what I was achieving with BaseGame and Node trees to define different areas in the game – attaching and detaching from the root Node as needed and using some monsterous switches to keep track of what was going on.



In trying to wrap my head around StandardGame and GameStates, I thought I would throw together a test app (based on the Tutorial/faq) that basically has three GameStates that can be displayed (activated seperately or in combination) to simulate in-game area changes, visual overlays, etc.



However, when trying to access the active state of a particular GameState I am getting a null pointer exception that I can't quite find a way to get beyond.  My idea is to pickup a keypress and then check the active status of the GameState in question and setActive to what is opposite of its current active status.



I have tried using:



gs2Bool = GameStateManager.getInstance().getChild("gamestate2").isActive();



to access the current active status of the state (in order to flip gs2Bool), and



GameStateManager.getInstance().getChild("gamestate2").setActive(gs2Bool);



to try to set the state directly (after flipping the boolean directly).



The exceptions thrown in both cases are essentially the same and note:



Jul 4, 2008 12:19:11 PM com.jmex.game.DefaultUncaughtExceptionHandler uncaughtException

SEVERE: Main game loop broken by uncaught exception

java.lang.NullPointerException

at worldgamestates.BaseGameState.update(BaseGameState.java:68)

at com.jmex.game.state.GameStateNode.update(Unknown Source)

at com.jmex.game.StandardGame.update(Unknown Source)

at com.jmex.game.StandardGame.run(Unknown Source)

at java.lang.Thread.run(Thread.java:619)



In this case, line 68 is



GameStateManager.getInstance().getChild("gamestate1").setActive(gs1Bool);





I'm sure I'm just overlooking something, but I'm not sure what and haven't found any clues in the docs or in forum searches.  Any ideas?

Well… heh… pardon me.  I knew it was something simple…



It turns out that I was trying to access the GameState by its create() name and not its given (setName) name.  So, in this instance I was creating the GameState with



GameState2 gamestate2 = new GameState2("GS2");

GameStateManager.getInstance().attachChild(gamestate2);

gamestate2.setActive(false);



and then trying to access it via "gamestate2" instead of "GS2".



O forum!  Thanks for being a great sounding board! (if even I only heard my very own echos herein :wink: