Sign in to follow this  
DioCaballeroRojo

Game State Manager

Recommended Posts

Okay so Im currently working on my Game State Manager class and Im kinda new to game development, but I cant figure this one out, what is expected to be the methods of a GSM? Im working on my GSM right now and I really feel like im going the wrong direction... should the GSM be the one responsible to render the states?

Share this post


Link to post
Share on other sites
What do you have it doing? I can see a ChangeState() method, a GEtCurrentState() method, a RegisterState() method. I can't think of much else off the top of my head.

Share this post


Link to post
Share on other sites
What is a game state [u]manager[/u]? This is for beginners, so I recommend keeping it simple. The simplest solution is a switch statement over a GameState enumeration:
[code]
enum GameState
{
MainMenu,
RunGame,
GameOver,
HighScore,
Exit
};

int main(...)
{
setup();

GameState current = MainMenu;
while(current != Exit)
{
Event event;
while(PollEvent(&event))
{
switch(current)
{
case MainMenu: handleMenuEvents(event); break;
case RunGame: handleGameEvents(event); break;
case GameOver: handleCutSceneEvents(event); break;
case HighScore: handleHighScoreEvents(event); break;
}
}

GameState next = current;

switch(current)
{
case MainMenu:
next = updateMenu();
renderMenu();
break;

case RunGame:
next = updateGame();
renderGame();
break;

case GameOver:
next = updateCutScene();
renderCutScene();
break;

case HighScore:
next = updateHighScore();
renderHighScore();
break;
}

if(current != next)
{
transition(current, next);
current = next;
}
}

cleanup();
}
[/code]
I think adding a "manager" only confuses the situation here. Converting each enumeration into a class with state is pretty easy:
[code]
class GameState
{
public:
virtual ~GameState()
{
}

/*
* Returns a new GameState instance if a transition should occur.
* Returns nullptr if final state.
* Otherwise returns "this".
*/
virtual GameState *update() = 0;

virtual void render() = 0;

virtual void handleEvent(const Event &) = 0;
};

class MainMenu : public GameState
{
// ...
};

// Other GameState implementations

int main(...)
{
setup();

GameState *current = new MainMenu();

while(current)
{
Event event;
while(PollEvent(&event))
{
current->handleEvent(event);
}

GameState *next = current->update();
current->render();

if(current != next)
{
delete current;
current = next;
}
}

cleanup();
}
[/code]
For a beginner, anything more complicated is probably over-engineering.

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