Stack of game states

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
Nordbjerg
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Sun May 27, 2012 5:00 am

Stack of game states

Post 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.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Stack of game states

Post 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.
Nordbjerg
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Sun May 27, 2012 5:00 am

Re: Stack of game states

Post 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?
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Stack of game states

Post 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.
Nordbjerg
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Sun May 27, 2012 5:00 am

Re: Stack of game states

Post by Nordbjerg »

Okay, thanks. I think I'll just go with a vector then ;)
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Stack of game states

Post 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.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Stack of game states

Post 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.
Image
Post Reply