Switch GameStates / Change GameStates

Hi,

I wanna switch my gamestates from menu to ingame but using the GameState.setActive(boolean) method does not work.

If I deactivate my menu gamestate it is still displayed.



So I tried to use the GameStateManager.deactivateChildNamed(String) method. But I got a NullPointerException. I dont' really understand why this error occurs because the String of the gamestates name is correct (the same String which I used initializing the gamestate via the constructor.





My GameStates extend the BasicGameState.





Here's a bit of code:


    private static void createGameStates()
    {
   gameStateManager = GameStateManager.create();

   root = new RootGameState();
   root.setActive(true);
   menu = new MenuGameState();
            menu.setActive(true);   
   ingame = new IngameGameState();
   ingame.setActive(false);
   
   
   root.getRootNode().attachChild(menu.getRootNode());
   root.getRootNode().attachChild(ingame.getRootNode());

   gameStateManager.attachChild(root);
   
    }

Your GameStateManager knows only about RootGameState().

You need to add the different gamestates to the manager to be able to activate/deactivate them by name.

   gameStateManager.attachChild.attachChild(menu);
   root.getRootNode().attachChild(ingame);

but why doesn't the gamestatemanager recognize that the passed rootgamestate has got a family with two children?

  root.getRootNode().attachChild(menu.getRootNode());
   root.getRootNode().attachChild(ingame.getRootNode());



So there is no way to pass the hierachy I created in the RootNodes?

Or is there maybe a better solution for my problem than deactivation by name?


   gameStateManager.attachChild.attachChild(menu);
   root.getRootNode().attachChild(ingame);


I'm sure this is what you meant:


   gameStateManager.attachChild(menu);
   root.getRootNode().attachChild(ingame.getRootNode());

Ok, I got it.

there was a mistake in may source code.

It was like this:


 private static void createGameStates()
    {
   gameStateManager = GameStateManager.create();

   root = new RootGameState();   
            root.setActive(true);
   menu = new MenuGameState();
            menu.setActive(true);   
   ingame = new IngameGameState();
            ingame.setActive(false);
   
   
   root.getRootNode().attachChild(menu.getRootNode());
   root.getRootNode().attachChild(ingame.getRootNode());

   gameStateManager.attachChild(root);

   
    }



The problem with it is, that the GameState.setActive Method only works when a GameState is already added to the GameStateManager (thanks to core-dump). Furthermore if you do it like me:

   root.getRootNode().attachChild(menu.getRootNode());


You should not use GameStates. Because they are expected to get handeled by the gamestatemanager not by you!
So, what about the following:

 private static void createGameStates()
    {
   gameStateManager = GameStateManager.create();

   root = new RootGameState();   
   menu = new MenuGameState();   
   ingame = new IngameGameState();   

   gameStateManager.attachChild(root);
   root.setActive(true);
   gameStateManager.attachChild(menu);
   menu.setActive(true);
   gameStateManager.attachChild(ingame);
   ingame.setActive(false);
    }



Well, that's it.
So we have created a root-GameState that includes al the others and can get deactivated at the end of the program to deactivate all GameStates automatically - of course this GameState should be active at the runntime of our programm - and a menu GameState that should be active at the beginning of the game. At last there's the ingame GameState that is going to be activated after the start MenuButton being clicked.

There are only two questions remaining:
How can I create a hierachy within the GameStates?
E.g. GameState A is a generalization of GameStates B and C.

And Second, why is the skybox, that I create in the Constructor of IngameGameState visible when IngameGameState is not active? Isn't the skybox simply one part of the rootNode of my IngameGameState? So it should be deactivated with the GameState, because deactivating a gamestate should nothing else then deactivating it's rootNode?


If there are further mistakes or other ways making it better correct me.

root.getRootNode().attachChild(menu.getRootNode());


You just attach a node to another node there, the GameStateManager knows only about the RootGameState.

You can use  GameStateNodes to create a hierarchy of GameStates.
Although i never used them yet.

Thanks a lot again to you Christoph!



Finally I solved my second skybox problem:


And Second, why is the skybox, that I create in the Constructor of IngameGameState visible when IngameGameState is not active? Isn't the skybox simply one part of the rootNode of my IngameGameState? So it should be deactivated with the GameState, because deactivating a gamestate should nothing else then deactivating it's rootNode?


The problem was, that I did not cleanly review the code of my skybox creation methods after I startet using GameStates. So my skybox was not attached to the rootnode of the right gamestate....