Jump to content
  • Advertisement
Sign in to follow this  

Game State (Menu, GameScreen, ...)

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm in the process of developping a small game and i was wondering if the State Pattern is a good choice for implementing the different game states (Intro, Outro, Menu, GameScreen, ....) At first, it seems to be good to me, but i'm wondering what u guys use. Maybe a more linear solution?! Thx, Jo

Share this post

Link to post
Share on other sites
I'm not entirely familiar with the "State Pattern" is you're talking about. Is this the order of the states, or are you asking specifically for the best way to implement a game state machine?

Share this post

Link to post
Share on other sites
I'm using the state system and i never look back. Previous i had a whole lot of switch/cases which didn't do wonders for readability.

Separating the game in different states allows for more seperated modules. Also, if you allow stacked states you can implement neat things like a submenu state (in game or from the menu), that gets overlaid on the play state.

I put the states in an object factory, so the states can change to another state without the need to know the class (just the key string of the correct generation function).

Share this post

Link to post
Share on other sites
I had a few functions that I called, this is from ika, but you get the idea, I think.. if you read it. (Python scripting)
#Main game loop.
while choice < 4:
# Introduction / splash thingie
if choice == 0:
choice = Intro(title, images, True)

#Do a fade from black to white. (cannot be skipped)
for x in range(0, 256, 5):
ika.SetCaption(title + str(ika.GetFrameRate()))
ika.Video.DrawRect(0, 0, ika.Video.xres, ika.Video.yres, ika.RGB(0, 0, 0, 255), 1)
ika.Video.DrawRect(0, 0, ika.Video.xres, ika.Video.yres, ika.RGB(255, 255, 255, x), 1)

# Main menu thing!
elif choice == 1:
choice = Menu(title, images)

# Play game!
elif choice == 2:
choice = Play(title, images)

# See sekret options and stuff..
elif choice == 3:
choice = Options(title, images)

#End program.

Share this post

Link to post
Share on other sites
Thx Endurion for the fast reply,

You came to the sames conclusion as me, trying to keep things separated.

I thought of implementation a State as:

Initialise - Initialise a new state, preparing everything used in the state
Start - Put the State on the stack or something similar (Setting it as the current one)
Finalize, Shutdown - End of the state, cleaning up everything

As you state, i was planning to use a LoadingState (during long Initialise "GameScreenState")...

Do u use something similar? Any special advice ?


Share this post

Link to post
Share on other sites
My states have a Init, Exit, Display and Update function (also a second Update which does fixed time steps).

I have a state manager class that takes care of the state stack. Every state gets a pointer to the manager so it can add new states or remove itself from it (without accessing a global/singleton).

The state manager calls the update function of all the states currently on the stack. The highest state gets passed the elapsed time, the others on the stack get 0 for the elapsed time value. This makes for a paused game state when a submenu is open. Most of the time there's only one active state though.

If there is some data that needs to be accessed by several states i have a game data singleton.

The state adding and removing is done via a event system since i ran in trouble if for example the gamestate removed itself on a keypress.

My game framework class inherits from the state manager and basically only calls the display and update function of the manager. All the grunt work is done in the states themselves.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!