Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Managing game states

This topic is 5263 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

Hello, We all know that games usually consist of many states and screens, for example the main menu, walking on the map, fighting and a shop. When I used to code on DOS back in the day, I had a separate function for "entering" a single game state: for example, to go to shop, I''d call function Shop(a_lot_of_arguments). This function then had an infinite loop consisting of rendering the shop and handling the shop GUI (and exiting the loop aka. the shop when user wishes). Nowadays, on Windows, the situation seems a bit different: from the most basic Windows tutorials (back in the day, again) I read that games usually consist of only one "main loop", for example in Main, which goes along the lines: while(1) { ProcessGameFrame(); HandleMessages(); } Then ProcessGameFrame() can itself deduce the state the game is in, eg.: void ProcessGameFrame() { switch( gameState ) { case shop: ShopFrame(); break; case mainmenu: MainMenuFrame(); break; case cutscreen_animation: CutScreenFrame(); break; // and so on default: break; } } I''ve been doing this. But once the applications grow bigger and the states become more "hierarchial", this technique makes brain explode. So: what kind of methods do people use for handling this kind of game states? - Mikko

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
We built a state machine to keep track of that where I work.

Share this post

Link to post
Share on other sites
The game states in the main loop are usually only the more general states: load, menu, game, end_game. For every one of these states you can have an object, with it''s own sub-states that deals with them.
The keyword is "hierarchical". You used this word, but in the code you posted there was nothing hierarchical: all the states where mixed together. In a hierarchical setup it would be something like:

main menu
options menu
settings menu
sound settings menu
graphics settings menu
in level
in battle
in shop
finished level
level stats
cut-screen animation

So would have one "switch statement" for every separate level of the hierarchy or something.


Share this post

Link to post
Share on other sites
Hi, here is a good tutorial about managing game states in C++

Hope it helps,

Share this post

Link to post
Share on other sites

  • 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!