Game States

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

Game States

Post by dejai »

I have been Reviewing the work of some of bitplane and other participants of the weekend challenges and I have also looked briefly at the source code of other exterior projects by various members of the irrlicht community.

Now, I have Noticed the use of game states within these projects, and I can grasp at least the concept in which it seems to portray about changing the states within the game from Menu, to Help Section, To Game Loop Etc.

Now my question is, where can information or documentation be found on states? I have never come across them before.
Programming Blog: http://www.uberwolf.com
Daaark
Posts: 19
Joined: Sun Jan 14, 2007 11:14 pm
Location: IUnknown

Post by Daaark »

You can use function pointers that change depending on state, or just a switch statement in the main update function of the game class.

A state is just an integer variable like any other, and it changes to one of your possible states whenever certain things happen. If the user clicks on the high scores button from the main menu, then you switch your state into STATE_HIGHSCORES.

Code: Select all

void CGame::Update(float &d)
{
    switch(state)
    {
         case STATE_HIGHSCORES:
         {
              break;
         }
         //etc...
    }
    return;
}
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

You can google for finite state machines and maybe for automata theory. The problem is that the results will probably be very theorethical and look more complicated than the stuff you need in games really is.

But the important thing is usually:
1. You have only finite number of states.Which means you can write each state down. Very often you will use one entry in an enum for each state (but you could also use a number or a string).
2. There's a certain number of actions which belong to a state.
3. You have to care about the transitions between states. First of all it's important when a state change will happen, for example when clicking a button (but any complex conditions can be checked here). Then it's important from which state you can switch to another state. Also often you have to do some cleanup in the old state on leaving and some initializing in the new state on entering.

As usual there are a lot of different ways to implement that. That depends very much on what you need. For example sometimes you will need several statemachines, like one for the gamestate (countdown, playing, paused), one for the UI (hud, pausescreen, menu). Other times the states can be hierarchical (often used in AI).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Maybe this would help:
Understanding State Pattern in C++
Post Reply