Archived

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

uutee

Managing game states

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:

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

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

shmoove

Share this post


Link to post
Share on other sites
Hi, here is a good tutorial about managing game states in C++
http://tonyandpaige.com/tutorials/game1.html

Hope it helps,
Oscar

Share this post


Link to post
Share on other sites