Getting Started with GameState

Hello!



I’m new to jME, I think it’s awesome, I can’t wait to get good at it. I have a few questions for the vetrans, though.



I understand the idea of GameStates in general, and I’d like to implement them before I get too deep into my first jME project. I’m just a little bit confused as to how to use jME’s system. I’ve searched through the boards, and I’ve examined the Javadocs generated from the freshest CVS, but I have a few questions.



(If anybody has a good link about this, I’ll settle for that!)



So, we have our little BaseGame implementation which we create initially. And that initializes the system, the game, and calls updates and renders as appropritate. I’ve got that much going.



Now, I override a BasicGameState and one of those will be my menu, the other the simulation-bit. Here is where I get confused. I understand that GameStates are little vessels which can encapsulate a bit of your game, they have a render and an update.



So, the way my brain works is that there would be a thing in my BaseGame that says calls “currentGameState.update()” every update and likewise for render.



Now, I know that jME has a more elegant way of doing that, using a tree structure to store states and stuff like that. Can somebody give me a little, pseudocode example of setting this up?



Each GameState seems to have its own rootNode, if I want to share a node between a few GameStates (like the node that has all the FPS and triangle statistics), do I have to pass it ahead of time? Or can I use the hierarchy to render it at the end?



I’m keeping a little log of my newbie adventures, I hope to turn it into a little tutorial that somebody might benefit from.



Thanks!

Rob

the game state doesn’t need to have its own rootNode, although it can. If you want two states to share the same rootNode, just stick it in a class and declare the rootNode to be public and static.



However, there is probably a better way fo doing it and since Per made the system, he knows it best…



DP

I don’t know, but maybe this thread where I kept a little changelog could help:

http://jmonkeyengine.com/jmeforum/viewtopic.php?t=1756



But more importantly, it seems like you have missed the jmetest.game.state.TestGameStateSystem.


Can somebody give me a little, pseudocode example of setting this up?


init:
GameStateManager.create();
GameState menuState = new MenuState();
menuState.setActive(true);
GameStateManager.getInstance().attachChild(menuState);

update:
GameStateManager.getInstance().update(tpf); // Updates all attached, active GameStates.

render:
GameStateManager.getInstance().render(tpf); // Renders all attached, active GameStates.



Each GameState seems to have its own rootNode, if I want to share a node between a few GameStates (like the node that has all the FPS and triangle statistics), do I have to pass it ahead of time? Or can I use the hierarchy to render it at the end?

First off, as DP said, a GameState isn't forced to maintain a rootNode.

To accomplish this I'd update and render this node separate from everything that has to do with gamestates. If a state needs a reference to this node, I'd simply pass it along in the constructor, as usual.

Hope that helps!
"Per" wrote:
But more importantly, it seems like you have missed the jmetest.game.state.TestGameStateSystem.

I totally did miss that. I skipped the tests entirely! :// I bet it would have answered my questions.

And you're right about rendering the node outside of the gamestates. I guess I just haven't fully wrapped my head around the architecture yet, but it makes more sense now.

Thanks a lot for your replies,
Rob