Jump to content
  • Advertisement
Sign in to follow this  
Shadowwoelf

Game states!

This topic is 3751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright well I am back again with more questions about game states. I was trying to use a simple switch statement, but that left a lot of global variables and such so I decided to go with the Class state manager way. And now I am completely clueless on how to go about with using game states and classes.(can't find any good tutorials) So far all I was able to think up with was making a base state class and then having all my other states derived from it. But then I got confused on Game state managers and such...

Share this post


Link to post
Share on other sites
Advertisement
A simple method that many people use is to create a stack of states.

Each state derives from your State base class and implements methods such as input(), render(), and update(). Your main loop simply handles the standard input and timing, then calls the appropriate functions of the top state on the stack.

Normally upon starting your game you would push a MainMenu state on the stack. When you open, for example, the settings screen from the main menu, you'll push a Settings state. When the Settings state exits you pop it off the stack, then you'll be back to MainMenu. This is a way to implement a simple hierarchy of screens without a lot of work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shadowwoelf
Do you have any examples/tutorials I can look at?


[google]. "Game state stack", or something like that. Here are some ideas, though.

STL has a stack template. clicky. You could do a std::stack<GameStateClass>, or maybe a stack of pointers.

std::stack<GameState> states;

GameState MainMenuState;

states.push_back(MainMenuState)

while(1){//states.top() accesses the last thing you pushed onto it, i.e. the current state.
states.top().input();
states.top().update();
states.top().render();
}
MainMenuState, at some point, would call states.push_back(SettingsState) or states.push_back(PlayingGameState), and these, when finished, would call states.pop() to remove themselves and return to the previous state. If you read the page I linked, and do a little googling, you'll probably see how it works. The only tricky bit is where to put the stack (global? Singleton? *shudder*), and the states.

edit: spelling, nitpicky punctuation

Share this post


Link to post
Share on other sites
I'd suggest the stack method but I recently finished a game that used a long list of booleans as the states (personally, I plan on switching to a stack implementation). But with all those bool's I had relatively no global variables outside my State class. I had a global object that was an object of that class and accessed the state bool's as properties, I was using C# (hence properties) but the same should be easily done in C++ or whatever language you're using. I don't think you would need loads of global variables for a state manager even if it used a list of bool's. Also as bools I never had anything being derived. Again, this isn't really the 'preferred' method but it's still a viable solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shadowwoelf
Alright well I am back again with more questions about game states. I was trying to use a simple switch statement, but that left a lot of global variables and such so I decided to go with the Class state manager way.

Whether you're using a switch statement or a state stack, having many globals shouldn't have anything to do with it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!