In a networked game, you might have states such as Login, SelectAvatar, and Play. By using a stack-based approach for states, you might consider establishing your server connection in the Login state and by using the stack, that connection can remain open & valid until the Login state is either destroyed or some event triggers the destruction of the connection.
But lets assume that during the Play state a loss of connectivity happens, now the player must be sent to the login screen. Since we've built up this stack of states, navigation back to the start isn't easy. Using a switch() call won't eliminate the states that exist below the play state. Furthermore it seems like bad design to allow passing a numeric count to pop() to remove N number of states from the stack too, particularly if N could vary depending on various conditions.
An alternate would be to avoid using the stack all together and instead traverse linearly between the three states. Since games often use a layered systems approach, there would likely be some network layer abstraction or subsystem that could hold the references to connections made and simply provide an API that various states could use to interact with the connection. Therefore, the connection is established in one state may cleaned up by another. That again feels like poor design to me but maybe others feel it's typical.
I have seen references where game states are treated like screens. Now we're wrapping UI aspects into these states. If you've approached game state using a stack-based approach, that may or may not work well with aspects around UI. By nature, UI is generally not stack-driven. You often have more than one UI screen open at any given time which may need input, logic updates, etc. Furthermore, UI screens can be opened in varying order and depending upon the order of operations, you get different outcomes. For example, opening panel A followed by panel B implies that A would get closed. But if you opened panel B first followed by panel A, then both are acceptably open with no conflicts. One could introduce panel C into the picture which can be opened or closed independently from the other two panel's open/close order.
This has all lead me to believe that trying to approach game state management like this feels like really poor design. I have begun to feel that a different approach is needed, perhaps multiple state machines per subsystem. In order for these subsystem state machines to interact is through some event/messaging system or well defined subsystem interfaces.
But before I take any approach, I'm curious how others have addressed game state around managing your UI interactions and various screens with other subsystems like networking and audio where you create a connection/sound in one state and it remains active until another state is reached downstream. It could simply be my approach using the stack-based solution is flawed somehow and if so, feel free to correct me where my understanding may be inaccurate.
Edited by crancran, 20 December 2012 - 11:42 AM.