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.
Game States
Game States
Programming Blog: http://www.uberwolf.com
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.
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;
}
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).
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Maybe this would help:
Understanding State Pattern in C++
Understanding State Pattern in C++