liveBook · Manning

Main loop

As explained in High Performance Unity Game Development

A recurring control structure that advances a game through updates, input handling, rendering coordination, and other gameplay systems.

A survival game needs to start at a menu, let the player avoid enemies, and show a game-over screen. If every GameObject runs its own initialization and frame updates, it becomes harder to tell which code runs when. A single Game script can instead drive the main loop: it owns the flow between menus and gameplay and calls other components in a deliberate order. section 5.1.1

Game holds the Balance rules, mutable GameData, and menu references. Its Unity Start() allocates and initializes long-lived game data, initializes Board, and activates the main menu. Its Update() calls Board.Tick() each frame when gameplay needs to run. Board collects player input and displays the action; Logic modifies GameData. Game coordinates these parts rather than putting every responsibility into one script. section 5.3.1 section 5.4 section 5.5

Keep control of execution and state in one place:

  • Have Game call Board.Init() instead of relying on Board.Awake() or Board.Start(). This makes initialization and allocation order explicit. section 5.1.2
  • Have Game call Board.Tick() instead of giving Board its own Update(). A breakpoint in Game then provides a starting point for tracing each frame. section 5.1.1 section 5.1.2
  • Route menu changes through Game: update MenuState through Logic, hide the previous menu, and show the next one. section 5.1.1
Listing 5.12 We use Unity’s Start() to initialize our data
void Start()
{
    Logic.AllocateGameData(m_gameData, m_balance);
    Board.Init(m_balance);
 
    UIMainMenu.SetActive(true);
    UIGameOver.SetActive(false);
}

The Start() listing shows the entry point doing three concrete jobs: allocating GameData, initializing Board, and setting the initial menu visibility. section 5.3.1

The loop can keep driving the same objects as levels or menus change. Store the current state in GameData, load different level values from Balance, and show or hide menu GameObjects rather than making each level or menu a separate scene. One scene is a simplicity choice here, not a requirement that every Unity project abandon its scene workflow. section 5.1.4

How High Performance Unity Game Development covers it

How deep it goes
Works through initialization, per-frame calls, and menu transitions; leaves Board input and data modification to other components.
Examples
Uses a player avoiding enemies, Start() initialization code, and a menu-to-gameplay flow.
What it assumes
Assumes familiarity with Unity GameObjects and lifecycle callbacks while showing how to replace Board's default callbacks with Init() and Tick().
What you can do after
Build it from scratch in Unity with one Game driver, explicit Board initialization and ticking, and controlled menu transitions.
How it connects
Connects data-oriented design, Unity lifecycle methods, game state, menus, and the singleton pattern.

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.