Page 1 of 1

Stack of game states

Posted: Thu Aug 16, 2012 4:41 am
by Nordbjerg
Okay, so heres the thing.. I am developing a 2d game engine, and in every game engine a wise choice would be to have some sort of state management. I've researched a bit, and I've found out that a lot of people use a stack.

Then I found some problems about a game state stack, but I solved them by writing some scenarios on a piece of paper and found out that those definitely won't be a problem.. Anyways, I want to have 3 types of game states: primary, overlay and popup.

The primary game states will block any other game states (underneath this state) ability to render, handle events and update.
The overlay game state will block any other game states (underneath this state) ability to handle events and update.
The popup will not block anything.

This will result in a variety of different awesome game states, for instance an InventoryState overlapping a PlayState without pausing the game, and so forth. Now for the problem: how the fudge do I manage all of this? How do I even implement these 3 types?

So far I only have a get type method in my states, but I don't know what to do with the state manager.. How would I e.g. update the PlayState while having a InventoryState open? It's a stack. Should I just go with vectors?

Danke.

Re: Stack of game states

Posted: Thu Aug 16, 2012 5:33 am
by bbguimaraes
Just a quick thought as I eat my breakfast: each state can hold a block rendering and a block handling flag. When rendering, you start at the top of the stack and go down until you find a state that blocks rendering. Then go back up rendering the states. The same thing for handling events, though you'd have to think if event handling should be done forwards or backwards.

Nice concept, by the way.

Re: Stack of game states

Posted: Thu Aug 16, 2012 7:53 am
by Nordbjerg
Thanks, but just how am I going to move up and down a std::stack? Pop it off and add it to a temporary stack?

Re: Stack of game states

Posted: Thu Aug 16, 2012 8:17 am
by bbguimaraes
I'd use a vector, instead of a stack. If you need to access inner states without loosing the top state, it is no longer a stack.

What you can do is provide a vector interface for the manager and a stack interface for the others, depending on your needs.

Re: Stack of game states

Posted: Thu Aug 16, 2012 8:46 am
by Nordbjerg
Okay, thanks. I think I'll just go with a vector then ;)

Re: Stack of game states

Posted: Thu Aug 16, 2012 3:42 pm
by Nokurn
I wrote a post about this very recently that you might want to take a look at.
viewtopic.php?f=13&t=7860&p=82907#p82801
It's a bit more detailed than your design, but you might find it useful.

Re: Stack of game states

Posted: Thu Oct 11, 2012 1:57 pm
by eatcomics
Nokurn wrote:I wrote a post about this very recently that you might want to take a look at.
viewtopic.php?f=13&t=7860&p=82907#p82801
It's a bit more detailed than your design, but you might find it useful.
Thanks for that nokurn, I'm going to have to check that out.