liveBook · Manning

Finite-state machine

As explained in High Performance Unity Game Development

A computational model that represents behavior as a finite set of states and transitions between them.

A finite-state machine tracks which menu is active and changes the interface when that state changes. Here, an enum in MetaData stores states such as MAIN_MENU, IN_GAME, GAME_OVER, and PAUSE_MENU. section 8.2.3

SetMenuState() saves the old state, updates MetaData through a logic function, then hides the old menu and shows the new one. Some transitions also hide the board, such as entering the pause menu; the game-over menu leaves it visible. section 8.6.1

Listing 8.26 Method to set the menu state in Game
public void SetMenuState(MENU_STATE newMenuState)
{
    MENU_STATE oldMenuState = m_metaData.MenuState;
    Logic.SetMenuState(m_metaData, newMenuState);
    if (oldMenuState == MENU_STATE.MAIN_MENU)
    {
        m_mainMenuVisual.Hide();
    }
    else if (oldMenuState == MENU_STATE.GAME_OVER)
    {
        Board.Hide();
        m_gameOverVisual.Hide();
    }
    else if (oldMenuState == MENU_STATE.IN_GAME && newMenuState == MENU_STATE.PAUSE_MENU)
    {
        Board.Hide();
    }
    else if (oldMenuState == MENU_STATE.PAUSE_MENU)
    {
        m_pauseMenuVisual.Hide();
    }
 
    if (newMenuState == MENU_STATE.MAIN_MENU)
    {
        m_mainMenuVisual.Show();
    }
    else if (newMenuState == MENU_STATE.GAME_OVER)
    {
        m_gameOverVisual.Show();
    }
    else if (newMenuState == MENU_STATE.PAUSE_MENU)
    {
        m_pauseMenuVisual.Show();
    }
    else if (newMenuState == MENU_STATE.IN_GAME)
    {
        Board.Show();
    }
}

The method’s conditional branches show how menu transitions control menu and board visibility. section 8.6.1

How High Performance Unity Game Development covers it

How deep it goes
Implements enum-based state updates and conditional visual changes, while noting alternative implementations without comparing them.
Examples
Uses main-menu, in-game, game-over, and pause-menu states, with board visibility varying by transition.
What it assumes
Assumes menu states are represented by an enum in MetaData and changed through logic functions.
What you can do after
Apply it via SetMenuState() to switch menu states and update the corresponding visuals.
How it connects
Connects menu state to metadata, menu visuals, and game-board visibility.

Also explained in

Compare how 13 books explain finite-state machine →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.