http://gamedevgeek.com/tutorials/managing-game-states-in-c/
Anyway, that's the rough design of it, though I don't have the stack of states (although looking at the downloaded code... I couldn't immediately see what advantage the stack gave but maybe I'm tired) like in that one.
As you can see, whenever a state changes, the new stats has its init() function called which initializes the state. That's fine but now I've come across problems when you want to return to already created states when certain things should only occur once and I'm not totally sure how to approach reorganizing things.
For example, I have my GameplayState that handles the in-game state of my program. The init() function clears any keyboard/mouse input handlers set up by previous states and sets up its own (which should happen every time the state is changed back to GameplayState) but it also does a few other things, one of which is loading XML data about game Tiles and another is loading the game's first level from XML.
Clearly, that's going to cause major problems if they're being called every time the state changes back. Right now I'm implementing level transitions so when the player gets to a certain location in level 1, the game moves to level 2. I'm doing that by first entering an intermediate InterLevelState which will consist of a black screen with some info on it, then the user presses return to proceed to the next level.
Now obviously, if level 1 is finished, then the new level is loaded somehow and the state changed to InterLevelState... as soon as it changes back to GameplayState it's just going to call the original loadMap() function and start again on level 1.
How should I approach this? I've a few partially-developed ideas but I don't know which approach makes more sense.
Do I really NEED the ability to call some things the first time a state is run, or should that be structured differently altogether so that it's hopefully unnecessary?
I'm thinking, I could have a "currentLevel" variable of some kind and then GamePlayState could just call that to load the level all the time or something, but then what about loading data from XML? If it's only required for GameplayState, then surely I shouldn't load it any earlier... but if it never changes then I shouldn't load it needlessly so I can't see a way around that.
Another related question: Say I have a top down shooter where WASD controls a player character. Then I want to implement some kind of purchasing system where the player does something that brings up a menu where WASD now serves to alter the menu selections. Seems to me that making that a different state would be inappropriate (especially if you hypothetically wanted to pop the menu up with the gameplay still in the background) so what then is a neat way of handling the different input "states" within the same program state? Some kind of simple variable/enum system that determines what the keys do?
if( gameplay )
{
WASD = movement
}
else if( menu )
{
WASD = menu
}
Something like that?