Jump to content
  • Advertisement
Sign in to follow this  
DioCaballeroRojo

Game State Manager

This topic is 2600 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

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
Advertisement
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 manager? This is for beginners, so I recommend keeping it simple. The simplest solution is a switch statement over a GameState enumeration:

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();
}

I think adding a "manager" only confuses the situation here. Converting each enumeration into a class with state is pretty easy:

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();
}

For a beginner, anything more complicated is probably over-engineering.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!