Jump to content

  • Log In with Google      Sign In   
  • Create Account


Game State Management


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 crancran   Members   -  Reputation: 343

Like
0Likes
Like

Posted 20 December 2012 - 11:10 AM

When I was researching game state management a while back, I found the traditional stack-based finite state machine highly recommended. I implemented the system in a current game but there are aspects of this design that feel a bit flawed and often too restrictive.

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.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9119

Like
0Likes
Like

Posted 20 December 2012 - 02:08 PM

I usually use state classes that use the run and return successor pattern. Basically every function called on a state object returns a smart pointer to the state that the system should be in now. This can be a pointer to the original state. If states need to be composed or a state otherwise needs reference to a previous state, then states can pass smart pointers to themselves as arguments to constructors for other states. This means you can build up a stack if you want, but still have the freedom to discard the stack at any time.

I don't see any particular issue in doing things like sharing connection objects between states. If everything is properly encapsulated and destructors properly defined, etc. then you can do things like pass a shared pointer and destruction will take care of clean up.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS