Trouble with GameStates

Im entirely confused about the GameStateSystem. I have copied the code from TestGameStateSystem into my game to see if it works.

It almost works but it starts another GameState on top of the old one.

I use this to create a new game state when button is pressed:

                  GameState ingame = new SinglePlayerGameState("ingame");
                      ingame.setActive(true);
                      GameStateManager.getInstance().attachChild(ingame);
                      GameStateManager.getInstance().deactivateChildNamed("main menu");


and I use this to go back to menu when another button is pressed:

              GameStateManager.getInstance().activateChildNamed("main menu");
            // And remove this state, because we don't want to keep it in memory.
            GameStateManager.getInstance().detachChild("ingame");


and here is the result when going from game to menu then back to game, as you can see I have 2 of everything:
If I go back to menu again and return to game there will be 3 of everything.
I thought I should not create new object each time

GameState ingame = new SinglePlayerGameState("ingame");

but otherwise I have to create all game states before activating them which would hog memory. How does TestGameStateSystem create a new game state each time without consuming memory? :?

mud2005 said:
How does TestGameStateSystem create a new game state each time without consuming memory? :?

It does not :|

The current game state system is not airtight in this respect. You have to decide yourself if you want to remove (that is cleanup yourself) an existing game state and recreate it once you need it again, or just keep it and reactivate it when needed.

A really good concept for loading (in background) and switching states with assured resource freeing is still needed in jME, I think.

I have done quite a bit of work on GameStates for use in Roll-A-Rama and created a GameManager that has ManagedGameStates that extend GameState and provide a lot more features for client/server mode support, enabling/disabling, transition effects, and much more.  You can see the current efforts on this from my repository:



http://www.captiveimagination.com/svn/public/ciutils/trunk



For example, how I do the fade-in for Roll-A-Rama is as follows:


GameManager manager = new GameManager("Roll-A-Rama", settings);    // GameManager has a GameSettings object that contains all your initializing game information
manager.start();
manager.waitForStatus(GameManager.STATUS_MAIN_LOOP);    // Wait for all preliminary initialization to complete before loading and transitioning to game
TransitionGameState.transitionGameStatus(null, MenuGameState.getInstance(), 5.0f, RRClient.class.getResource("/data/images/loading.jpg"));    // Simply takes it from the current game state (null), displays the loading.jpg on the screen while it is initializing MenuGameState (it's a singleton), and once it is completed loading takes 5.0f seconds to fade-into the game



Pretty simple and straight-forward.  I also have a progress loader I am working on, but am not ready to contribute that yet until it's more stable.

darkfrog

well Im back to working on this issue and im still confused, Irrisor stated

You have to decide yourself if you want to remove (that is cleanup yourself) an existing game state and recreate it once you need it again, or just keep it and reactivate it when needed.


well, my game keeps the states and reactivates them when needed, problem is there are 3 large states: singleplayer, multiplayer/host, and multiplayer/client. I would like to cleanup and recreate each state when switching states so they are not all in memory at the same time, only i can not figure out how to destroy a game state once created. I always get 2 gamestates on top of each other when swithing back to a gamestate. I tried

GameStateManager.getInstance().detachChild("ingame");

and

GameStateManager.getInstance().cleanup();

neither of which worked. This line

PhysicsWorld.getInstance().cleanup();

helps remove the physics attached to the objects but not the objects themselves.

What do I do? :?



edit: the same problem also occurs when I leave a GameState and go to the main menu then back to that same GameState.

It doesnt get cleaned up it just creates another on top of old one.

I'll have to go back and look, but I believe I added some explicit code to handle cleanup with my extensions onto GameStates.  I really want to sit down and re-work the whole GameState system when I have time and if nobody objects.  I think there can be a lot of things done that will make it far more useful.



darkfrog

maybe i've been going about this all wrong, I was thinking today, maybe instead I should have only one gameState for single/multiplyer and deal with everything in one instead of three. My problem is when I create a new game state it creates a new foosball table which, I think, is the biggest memory user.

I'm going to try doing it all with one state and one table :smiley: