Tutorials: GameState

Jul 2, 2011 at 2:27 PM

I know the majority of the time working on this project should be spent on the code, but for noobs like me, I need lots of documentation/tutorials. Can anyone give me a use example of the GameState and GameStateManager?

Jul 4, 2011 at 7:29 PM

Actually, nevermind. I finally figured it out! It wasn't that hard actually. Incase anyone is a noob like me, all you have to do is create an instance of the GameStateManager in your game class. Then create instances of your GameStates in the game class. Next call the Push() method of the GameStateManager, passing each of the GameStates. Finally, call the switch method of the GameStateManager passing the desired GameState you wish to switch to, and place the GameStateManager's Update and Draw methods inside your game class's Update and Draw methods.

Jul 4, 2011 at 7:51 PM

Don't Push() your game states onto the stack all at once!

Just create a GameStateManager in your game's constructor and add it to the Game.Components collection.

Then Push() the game state your wish to begin with (typically called something like TitleScreenState or IntroState).

Game States are managed as a stack: If you Push() a state, it will "sit on top" of the previous one. When you then call Pop(), the state sitting on top will be taken off and the previous state becomes active again.

Depending on whether the Push()ed states are pushed with GameStateModality.Popup or GameStateModality.Exclusive, the states below will still keep updating and drawing (so GameStateModality.Popup is only useful if you have a small menu of something that doesn't cover the whole screen).

Switch() is the same as calling Pop() and then Push(): It replaces the topmost state with something else.

Jul 4, 2011 at 10:00 PM

OH!!! Ok. That makes more sense. The way I was thinking it worked made me wonder why you even had push() and pop() since couldn't you just switch() everything.