Game State (Menu, GameScreen, ...)

Started by
5 comments, last by LowRad 18 years, 4 months ago
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
Advertisement
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?
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).

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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()
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
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Thank for great comments :)

I'm now more confiant in my choice.

Good Nite

This topic is closed to new replies.

Advertisement