Sign in to follow this  
ProblemBaby

Overlapping Game States

Recommended Posts

Hi Okey here is my problem... To keep my game code as organized as possible I have several game states. Like: Developer Intro Game Intro Main menu Main Game etc. The system is like this, a function called SetState takes an init a release and a run function then it run the current release function (if any) then the init function. My problem is sometimes I want to make effects between states which requires resources from both states now things starts to get complicated. I dont want to release a lot of stuff and then load it and probably cause a little latency. My question is, how do people solve this kind of problems. Maybe my State system is that good, what should a perfect state system look like? My quesions may sound a little diffuse, tell me if Ive to clearify something. thanks

Share this post


Link to post
Share on other sites
The approach I use is to have a stack of gamestates, inspired by this article:
Managing Game States in C++ .

Works really well for me with small adjustments. The user can start a game
Stack:
PlayGameState

press escape to get back to the menu
Stack:
MenuGameState
PlayGameState

Then maybe look at the highscores
Stack:
HighscoresGameState
MenuGameState
PlayGameState

then go all the way back to the PlayGameState like nothing had happend (in code by popping the top-most gamestates)

The GameStateManager::update() method calls update() on the top-most gamestate, so that only the "visible" game state gets updated, and the state of the other game states are preserved.

Share this post


Link to post
Share on other sites
Really interesting, thanks! the link you gave me talked about my main problem with overlapping effects but didnt really handle it I think. When some of the resources are used in the beginning of the effect and depening on which state it comes from. with push/pop it feels like a waste to have everything "behind".

Share this post


Link to post
Share on other sites
Quote:
Original post by ProblemBaby
Really interesting, thanks! the link you gave me talked about my main problem with overlapping effects but didnt really handle it I think. When some of the resources are used in the beginning of the effect and depening on which state it comes from. with push/pop it feels like a waste to have everything "behind".

Personally I use a game state manager along with an Enginuity Kernel. Using these together alllows me to solve a very broad spectrum of problems (that being said, the game I am building on top of the engine is not overly complex).

Concerning the leaving-everything-behind; you could make a game state free its resources when (using the same terminology as in the article) its CGameState::Pause() method gets called. Then the game state could reallocate its resources in its CGameState::Resume() method. This would ofcourse create a probably noticable delay when changing between states, but that's the price you'll have to pay if you want as much resources as free as possible. Maybe you could make some connection with a resource manager so the resources for a game state was only free:ed when there is a resource shortage.

In any case, the article is more like a hint (at least it was for me). It takes a while to get it to work right, but that's the fun part of it [smile].

Share this post


Link to post
Share on other sites
Couldn't you just implement what the article says and just add in some ways for the states to communicate with other states on the stack.. that way you could have the topmost member gamestate call the previous member's updatetransition function or something.

Share this post


Link to post
Share on other sites
For a transition between states, have a new type of state called Transition which has references to the 'before' and 'after' states. Its own rendering method does the appropriate mix of the two states, and when it is done it removes itself from the state queue and replaces itself with the 'after' state.

Share this post


Link to post
Share on other sites
That's kind of what i do too. When i create a new state, it gets a pointer to the old state in case it wants to save anything. Then the old state is deleted and the new state properly set up in it's place.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this