Switch scenes in monogame?

Hi, I’m fairly new to monogame but I’m working on a tiny game and have pretty much finished coding most mechanics. I’ve run into one thing that I don’t know how to handle though and I would like some input on the best way to handle things.

When my player character dies currently I just reset all the variables and call my Game1.cs LoadContent method again to restart everything. Instead, I’d like to switch to a game over screen or something similar. Or have the start screen when the game is first launched. I’m confused about how to handle this and looking for advice on changing the game state between a start menu > gameplayer > gameover screen > back to main menu or gameplay.

Edit: I understand I could have multiple content manager in my game1.cs file and call the separate loadcontent methods on each one as needed, but that seems like it would make the game1.cs file more complicated. I’d rather have the scenes in separate files. I’m sorry for such a newbie question.

3 Likes

Calling Game.LoadContent() again is not really the best way to go about things. You don;t want to load all content again (though it will skip content it has already loaded if not unloaded previously).

A simple way to get started with different states in the game is to use a simple state machine. Declare an enum with the different game states.

enum GameState
{
    MainMenu,
    Gameplay,
    EndOfGame,
}

In your Game class, declare a member of the GameState type.

GameState _state;

In your Update() method, use the _state to determine which update to run.

void Update(GameTime deltaTime)
{
    base.Update(deltaTime);
    switch (_state)
    {
    case GameState.MainMenu:
        UpdateMainMenu(deltaTime);
        break;
    case GameState.Gameplay:
        UpdateGameplay(deltaTime);
        break;
    case GameState.EndOfGame:
        UpdateEndOfGame(deltaTime);
        break;
    }
}

Now define the UpdateMainmenu(), UpdateGameplay() and UpdateEndOfGame().

void UpdateMainMenu(GameTime deltaTime)
{
    // Respond to user input for menu selections, etc
    if (pushedStartGameButton)
        _state = GameState.GamePlay;
}

void UpdateGameplay(GameTime deltaTime)
{
    // Respond to user actions in the game.
    // Update enemies
    // Handle collisions
    if (playerDied)
        _state = GameState.EndOfGame;
}

void UpdateEndOfGame(GameTime deltaTime)
{
    // Update scores
    // Do any animations, effects, etc for getting a high score
    // Respond to user input to restart level, or go back to main menu
    if (pushedMainMenuButton)
        _state = GameState.MainMenu;
    else if (pushedRestartLevelButton)
    {
        ResetLevel();
        _state = GameState.Gameplay;
    }
}

In the Game.Draw() method, again handle the different game states.

void Draw(GameTime deltaTime)
{
    base.Draw(deltaTime);
    switch (_state)
    {
    case GameState.MainMenu:
        DrawMainMenu(deltaTime);
        break;
    case GameState.Gameplay:
        DrawGameplay(deltaTime);
        break;
    case GameState.EndOfGame:
        DrawEndOfGame(deltaTime);
        break;
    }
}

Now define the different Draw methods.

void DrawMainMenu(GameTime deltaTime)
{
    // Draw the main menu, any active selections, etc
}

void DrawGameplay(GameTime deltaTime)
{
    // Draw the background the level
    // Draw enemies
    // Draw the player
    // Draw particle effects, etc
}

void DrawEndOfGame(GameTime deltaTime)
{
    // Draw text and scores
    // Draw menu for restarting level or going back to main menu
}

You can extend this later on by using separate classes for the different game states.

7 Likes

You could use a variable to hold what state your game is in…
Like an INT where 0 is load-screen, 1 is main menu and 2 is gameplay…
Or a STRING containing “intro”, “menu”, “gameplay”…

Launching the game sets this variable to “load screen”…
Pressing enter sets the variable to “main menu”…
Selecting “start game” from the menu sets this variable to, say “gameplay”…
Being killed sets the variable back to main menu…

Your update and draw methods would then contain a switch or series of if statements, making sure things only happen that correspond to whatever mode you are in…

such as :

Update()
{
if (gamemode == “main menu”)
{
all your menu update stuff…
}
else if (gamemode == “gameplay”)
{
update gameplay stuff…
}
}

You can do it in a lot of different ways, but thats the kind of stuff I used alot with my first stuff…
-It only requires if statements and is pretty simple to understand…

1 Like

Would you mind giving a small sample of how to extend this into separate classes for each state?

Download this code and take a look, it’s very useful and might help you:

http://xbox.create.msdn.com/en-US/education/catalog/sample/game_state_management

yes, it’s for XNA, but it works in monogame 3.4.

1 Like

Very helpful, thanks a lot!