Sign in to follow this  
LowRad

Game State (Menu, GameScreen, ...)

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)
fps.doLimit()
ika.Video.ShowPage()
ika.Input.Update()


# 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.
ika.Exit()

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 ?

Thx
Jo

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

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