The best way to deal with game states?

What is the best way to have a game that behave like SimpleGame but supports game states?



I have some sugestions:

  1. Subclass SimpleGame then use result to subclass StandardGameState;
  2. Subclass StandardGame and add funcionality similar to SimpleGame.


If I'm not mistaken any game can use game states.  You just need to call the GameStateManager's update and render methods in the games update and render methods.

Hal is correct that any game can support GameStates, it only needs to have support for GameStateManager.  The easiest way to support GameStates and behave like SimpleGame is to create a new StandardGame and add a DebugGameState to it:


StandardGame game = new StandardGame("Test Game");
game.start();

DebugGameState debug = new DebugGameState();
GameStateManager.getInstance().attachChild(debug);
debug.setEnabled(true);



Note that the above code was written by memory and I haven't actually gotten any time to do any jME coding lately, so it might be a little off. :o

I've been using VariableTimestepGame. Is it possible to use game states with that game? I ask because it's based on AbstractGame, not StandardGame. If not, how do I make separate nodes disappear? I have a main scene node and a FPS node and I'm trying to toggle the main scene on and off. It's got to be simple, but I'm just not seeing the answer.



Thanks

Joe

Yes, you can make it support GameStates by simply adding the appropriate calls to GameStateManager from within your update/render methods in your game.

Hmmm… From the reading I am doing, it looks like StandardGame is the way to go for a production setting. So I'm moving from VariableTimeStepGame to StandardGame. I followed the example of TestStandardGame. The only thing I'm confused about (right now anyway :slight_smile: ) is that I get an error when I try to override some of StandardGame's functions like initGame or update:



public class Main {


Ideally you should never be extending StandardGame.  It's is a very different approach than every other Game type in jME.  The intended approach is to do everything through GameStates instead.  You can create your own custom GameState (or use or extend BasicGameState) and implement your own update/render methods that do what you want to do and then inject them into GameStateManager and applies it to StandardGame.



You can very closely follow to what TestStandardGame does, except create your own version of GameState instead.

darkfrog said:

You can very closely follow to what TestStandardGame does, except create your own version of GameState instead.


Ah! Bear with me while I paraphrase, so I make sure I understand:

I subclass a GameState and use it's update and render methods for each section of the game. Let's say:

GameState is itself abstract, so if you replace every reference to GameState with OtherGameState you're on track. :wink:



That's one of the coolest features of GameStates is that you can have many, one, or none active at any given time.  It's a great way to separate logic.  For example, in the game I'm developing I have a main menu game state and a game state for the actual game.  I hide the menu game state when the game starts, but if the user hits Escape the menu reappears in front of the game.

…and it works!



Thanks my light-avoiding amphibian friend…

:slight_smile:

Any time. :slight_smile: