Chapter 5: Game board and game loop
The introduction positions Board and Game as the remaining parts of the DOD architecture and explains why Unity is the setting for implementing them.
After this chapter you will be able to
- Engine agnostic and Unity components. Distinguish the engine-agnostic Balance, GameData, and Logic from Board and Game, the two remaining parts of the architecture to be implemented in Unity. Explain that Unity's OOP design makes it an example of fitting a DOD architecture into an OOP environment.
- Board gameplay role. Describe how Board turns core gameplay into something the player can see and interact with in Unity.
- Game ownership and loop. Explain what Game owns and why it holds Balance and GameData in addition to driving the gameplay loop.
- Single game loop driver. Explain why Game drives the whole game loop instead of letting each GameObject run its own independent lifecycle, and what control that gives the developers.
- Game coordination and menu changes. Describe what Game holds and coordinates, and trace a menu or Board transition at the level explained here.
- Game singleton access. Explain why Game is a singleton in this architecture and how the section limits that convenience.
- Deterministic gameplay Logic. Explain how Logic receives and transforms data independently of Game, including how the section handles randomness.
- Game-controlled Board lifecycle. Explain how Game calls Board’s Init() and Tick() instead of leaving Board to Unity’s default lifecycle, and why that choice matters.
- Core scene object roles. Describe why the scene contains a Main Camera, a Board, and a Game object.
- Data instead of scene switches. Explain why changing a level, menu, or enemy set does not require loading another Unity scene in this architecture. Describe the corresponding change to data or menu state.
- Scene switching complexity. Justify why putting every level or menu in its own scene adds complexity to this data-oriented architecture.
- Board gameplay responsibilities. Describe the Board’s role in holding object pools, displaying gameplay, and handling player input.
- Board lifetime functions. Distinguish what Init(), Show(), and Hide() do, including which data can persist across plays and which data belongs to a particular level.
- Per-frame input flow. Trace the described relationship between Tick() and handleInput(), and describe the input information kept by the Board.
- Board resource lifetime. Explain why Board.Init() allocates the enemy GameObject pool once in this example and when that memory is released. Distinguish this from Show(), which runs each time gameplay starts.
- Game data startup in Show. Explain how Show() prepares gameplay data for a new game using the game data and balance.
- Board display and input readiness. Describe how Show() presents enemies and the in-game UI while preparing camera-based input and controlling virtual joystick visibility.
- Board exit and visual cleanup. Explain that Hide() is called when the player leaves the gameplay board, whether a level ends or the player exits early. Describe how this example hides the in-game UI and the enemy and player GameObjects through their active states or object pools, rather than deallocating level-specific data.
- Per-frame Tick flow. Trace the Board’s Tick() from its inputs through input handling, gameplay logic, visual updates, and the game-over check. Describe the changes it produces.
- Game-over control-flow tradeoffs. Explain why the section does not simply wrap most of Tick() in a game-over condition or return early when the game is over.
- Board input and output. Trace how Board.handleInput() uses mouse actions and m_mouseDownPos to update player direction and the virtual joystick.
- Mouse branch placement. Justify keeping mouse-action branches in Board.HandleInput() rather than moving them into one Logic.HandleInput() function.
- Mouse position space conversion. Trace how mousePosition becomes a position usable alongside the sprites.
- Unconditional conversion cost. Justify converting mouse coordinates every frame, even without player input.
- Game lifecycle ownership. Explain why Game is the sole MonoBehaviour lifecycle owner in this architecture and how its singleton is initialized.
- Start initialization data flow. Trace what Start() takes in, changes, and leaves ready: Game member variables provide assets already in memory; Start() allocates and initializes game data and board pools, activates the main menu, and leaves the UI menus in their correct active states.
- Core gameplay setup. Trace what StartGame() takes in, changes, and leaves ready for play: it uses UI members, the Board, and the main camera; hides non-gameplay menus; and calls Board.Show() to position and display gameplay sprites.
- Per-frame gameplay flow. Trace how Update() drives gameplay each frame: it uses the Board, Balance, GameData, and elapsed time; calls Board.Tick() while in the IN_GAME state; and reaches GameOver() through Board.Tick() when gameplay ends.
- Mutable GameData and visible output. Explain why Board.Tick() makes Update()’s output less obvious, and describe how the static Logic functions make data access clearer: they modify the passed-in GameData, which the caller uses to update sprites.
- Game-over screen transition. Describe how GameOver() hides the board sprites and in-game UI and shows the game-over UI by changing their active states.
- Linear core gameplay flow. Trace how Game, Board, and Logic work together: Game calls Board with Balance and GameData, Board calls Logic, Logic changes GameData using Balance when needed, and Board displays the result.
- DOD and OOP architecture contrast. Explain why the section describes this DOD architecture as easier to follow and modify than an OOP arrangement in which many Unity scripts can react to events and change one another’s data.
- Board and Game Unity responsibilities. Distinguish Board’s Unity-facing gameplay duties from Game’s control of the overall loop. Explain why Board uses a MonoBehaviour but its own Init() and Tick() methods, while Game is a singleton MonoBehaviour that drives the loop through Unity’s default lifecycle functions.
What it builds on
- Simple DOD Architecture (chapter 4) · The DOD architecture introduced in the previous chapter.
- The GameData (chapter 4) · Introduces GameData, which Game holds.
- Tick() - Handling frame-by-frame gameplay (chapter 4) · The use of Tick() for frame-by-frame gameplay.
- The Logic (chapter 4) · Logic as the part that changes gameplay data.
- Reducing code complexity by separating data and logic (chapter 1) · The data-oriented distinction between data and the logic that operates on it.
- Using object pools to avoid memory fragmentation and garbage collection (chapter 3) · The use of object pools to avoid repeated allocation.
- StartGame() - Initializing our gameplay data (chapter 4) · Logic.StartGame() initializes gameplay data.
- Handling user input (chapter 4) · Earlier handling of user input in the gameplay logic
Section by section
Introduction
The introduction positions Board and Game as the remaining parts of the DOD architecture and explains why Unity is the setting for implementing them.
- Engine agnostic and Unity components: Distinguish the engine-agnostic Balance, GameData, and Logic from Board and Game, the two remaining parts of the architecture to be implemented in Unity. Explain that Unity's OOP design makes it an example of fitting a DOD architecture into an OOP environment.
Why it matters: This distinction explains what remains to connect the architecture to Unity.
Key terms: Balance, GameData, Logic, Board, Game, DOD architecture
5.1 DOD architecture, continued
Introduces the roles of Game and Board in the DOD architecture and explains that their ownership pattern applies across different games.
- Board gameplay role: Describe how Board turns core gameplay into something the player can see and interact with in Unity.
Why it matters: Board gives the gameplay data and logic a player-facing representation. - Game ownership and loop: Explain what Game owns and why it holds Balance and GameData in addition to driving the gameplay loop.
Why it matters: Game provides a home for shared data and for coordinating gameplay with the rest of the game. - Reusable ownership pattern: State how the architecture's ownership pattern relates to the data in different games.
Why it matters: The distinction helps readers apply the architecture without assuming every game needs identical data.
Key terms: Board, core gameplay, MonoBehaviour, Game, Balance, GameData, menu state, gameplay loop, custom singleton MonoBehaviour, ownership pattern
5.1.1 Game
This section explains why one Game object drives the Unity game loop, how it coordinates menus and the Board, and why gameplay Logic remains independent of that Unity-facing code.
- Single game loop driver: Explain why Game drives the whole game loop instead of letting each GameObject run its own independent lifecycle, and what control that gives the developers.
Why it matters: One entry point makes the game's execution order easier to control and debug. - Game coordination and menu changes: Describe what Game holds and coordinates, and trace a menu or Board transition at the level explained here.
Why it matters: A menu change must keep the gameplay state and the visible screen in step. - Game singleton access: Explain why Game is a singleton in this architecture and how the section limits that convenience.
Why it matters: The singleton makes the central Unity object easy to reach without making gameplay Logic depend on it. - Deterministic gameplay Logic: Explain how Logic receives and transforms data independently of Game, including how the section handles randomness.
Why it matters: Independent, repeatable Logic can be tested without recreating a Unity scene or singleton state. - Intentional orchestration coupling: Distinguish the direct coupling allowed among Unity-facing objects from the separation required for gameplay Logic.
Why it matters: The boundary keeps real coordination easy to follow without tying gameplay calculations to Unity.
Key terms: Game, game loop, Tick(), Awake/Start/Update, Balance, GameData, MenuState, Logic, Board, singleton
5.1.2 Board
The section explains why Game controls Board’s initialization and per-frame updates while Board remains a MonoBehaviour for editor-assigned assets.
- Game-controlled Board lifecycle: Explain how Game calls Board’s Init() and Tick() instead of leaving Board to Unity’s default lifecycle, and why that choice matters.
Why it matters: This establishes who controls Board’s work and when that work happens. - Board MonoBehaviour purpose: Explain why Board remains a MonoBehaviour even though Game controls its initialization and per-frame calls.
Why it matters: It distinguishes Board’s editor asset assignments from control of Board’s runtime calls.
Key terms: Game, Board, Init(), Tick(), Awake/Start, Update(), MonoBehaviour, Unity editor, assets
5.1.3 Scene hierarchy
This section identifies the small set of objects in the game’s Unity scene and explains how assets are assigned for this simple implementation.
- Core scene object roles: Describe why the scene contains a Main Camera, a Board, and a Game object.
Why it matters: These roles explain how the simple scene separates its visual board from its game loop. - Three gameplay UI canvases: Distinguish the purposes of the Main Menu, GameOver, and In Game canvases.
Why it matters: Each canvas has a distinct player-facing purpose in the scene. - Editor assigned asset references: Explain how this simple implementation assigns assets.
Why it matters: It clarifies that this scene uses a simple editor-assignment approach rather than an asset-loading system.
Key terms: Main Camera, Board, Game, scene hierarchy, Canvas - Main Menu, Canvas - GameOver, Canvas - In Game, assets, public variables, Unity editor
5.1.4 Unity scenes and code complexity
The section explains why this data-oriented game uses one Unity scene and represents changes to levels, menus, and enemies as changes to data rather than scene switches.
- Data instead of scene switches: Explain why changing a level, menu, or enemy set does not require loading another Unity scene in this architecture. Describe the corresponding change to data or menu state.
Why it matters: It lets the game vary what players see and play without making scene switching the basis of its gameplay architecture. - Scene switching complexity: Justify why putting every level or menu in its own scene adds complexity to this data-oriented architecture.
Why it matters: Keeping gameplay state in data avoids extra scene-lifecycle concerns when the desired change is only to the game’s content or state. - Single scene component roles: Describe what the single scene contains and how Game, GameData, Balance, Board, and menus divide responsibility in this architecture.
Why it matters: These roles clarify how a single scene can host a game whose changing behavior is driven by data.
Key terms: Unity scene, level data, menu state, game loop, scene system, game state, single Unity scene, Game, GameData, Balance
5.2 The Board implementation
This section introduces the Board’s gameplay responsibilities, its Unity references, and the custom functions that manage its lifetime and per-frame work.
- Board gameplay responsibilities: Describe the Board’s role in holding object pools, displaying gameplay, and handling player input.
Why it matters: These responsibilities explain why the Board needs both gameplay objects and input-related references. - Board asset references: Explain why this simple game assigns public GameObject references in the Unity Editor, and identify the kinds of objects those references supply.
Why it matters: It clarifies that direct Editor assignment is a choice for this small example, not a general asset-loading strategy. - Board lifetime functions: Distinguish what Init(), Show(), and Hide() do, including which data can persist across plays and which data belongs to a particular level.
Why it matters: Separating persistent setup from entering and leaving gameplay avoids repeatedly allocating objects the game can keep. - Per-frame input flow: Trace the described relationship between Tick() and handleInput(), and describe the input information kept by the Board.
Why it matters: The call order shows where player input enters the Board’s per-frame work.
Key terms: Board, object pools, input, public variables, Unity editor, EnemyPrefab, UI, resource management system, Init(), Show()
5.2.1 Init()
Explains how Board.Init() prepares board resources once for the game’s lifetime while keeping the board hidden until gameplay begins.
- Board resource lifetime: Explain why Board.Init() allocates the enemy GameObject pool once in this example and when that memory is released. Distinguish this from Show(), which runs each time gameplay starts.
Why it matters: The allocation belongs in Init() because this board needs the same pool throughout the game. - Initialization inputs and independence: Describe what Board.Init() uses from the balance and why it does not need game data to allocate its resources.
Why it matters: Keeping initialization tied to the resources it actually needs avoids making the Board depend on unrelated gameplay state. - Hidden board startup state: Explain why Board.Init() explicitly hides sprites and UI, and describe the resulting board state.
Why it matters: Explicitly setting the initial visibility prevents an accidental Editor setting from showing gameplay elements too soon.
Key terms: Init(), Show(), GameObject pool, balance, game data, Player, UI, active false, sprites, Unity Editor
5.2.2 Show()
Show() prepares gameplay data and makes the game board visible when core gameplay starts.
- Game data startup in Show: Explain how Show() prepares gameplay data for a new game using the game data and balance.
Why it matters: Showing the board also prepares the underlying data for gameplay. - Board display and input readiness: Describe how Show() presents enemies and the in-game UI while preparing camera-based input and controlling virtual joystick visibility.
Why it matters: The board must display the starting state while retaining what input handling needs.
Key terms: Show(), Logic.StartGame(), game data, balance, main camera, GameObject pool objects, in-game UI, virtual joystick
5.2.3 5.2.3 Hide()
This section explains when the Board’s Hide() function runs and what it hides in the simple game example.
- Board exit and visual cleanup: Explain that Hide() is called when the player leaves the gameplay board, whether a level ends or the player exits early. Describe how this example hides the in-game UI and the enemy and player GameObjects through their active states or object pools, rather than deallocating level-specific data.
Why it matters: It distinguishes leaving the gameplay board from running gameplay and clarifies what must change visually on exit.
Key terms: Hide(), in-game UI, GameObject active state, object pools
5.2.4 Tick()
This section explains what the Board’s per-frame Tick() does and why its game-over handling favors a straightforward flow over a conditional wrapper or early return.
- Per-frame Tick flow: Trace the Board’s Tick() from its inputs through input handling, gameplay logic, visual updates, and the game-over check. Describe the changes it produces.
Why it matters: This flow connects gameplay calculations to what the player sees each frame. - Game-over control-flow tradeoffs: Explain why the section does not simply wrap most of Tick() in a game-over condition or return early when the game is over.
Why it matters: A shortcut that avoids occasional work can make the frame logic less readable or omit an important final update.
Key terms: Tick(), game data, balance, static logic Tick(), game over, if statement, early returns
5.2.5 handleInput()
This section explains how Board.handleInput() turns mouse input into player direction and virtual joystick updates while keeping branching and frame-time costs visible.
- Board input and output: Trace how Board.handleInput() uses mouse actions and m_mouseDownPos to update player direction and the virtual joystick.
Why it matters: It connects what the player does with both the visible control and the gameplay data. - Input function dependencies: Explain why gameData alone does not reveal all the data Board.handleInput() uses.
Why it matters: A function’s parameter list does not always show everything it reads. - Mouse branch placement: Justify keeping mouse-action branches in Board.HandleInput() rather than moving them into one Logic.HandleInput() function.
Why it matters: Keeping the decision where it first arises makes this input path simpler to follow. - Mouse position space conversion: Trace how mousePosition becomes a position usable alongside the sprites.
Why it matters: The mouse position must be expressed in the same space as the sprites it controls. - Unconditional conversion cost: Justify converting mouse coordinates every frame, even without player input.
Why it matters: A consistently high frame rate must hold up when the player actually interacts with the game.
Key terms: Board, m_mouseDownPos, virtual joystick, gameData, Logic.MouseMove(), Logic.MouseUp(), member variables, Unity’s Input system, branch, Board.HandleInput()
5.3 The Game loop implementation
This section introduces Game as the central game-loop object, identifies the references and data it holds, and explains why it alone follows Unity’s MonoBehaviour lifecycle.
- Game lifecycle ownership: Explain why Game is the sole MonoBehaviour lifecycle owner in this architecture and how its singleton is initialized.
Why it matters: Keeping lifecycle ownership in Game makes the order in which scripts run easier to control. - Game object references and balance: Describe the references assigned to Game in the Unity editor and how this example makes its balance editable.
Why it matters: These fields show what Game needs to connect gameplay, its camera, menus, and editable balance. - Private game data access: Justify keeping m_gameData private and passing it to functions rather than accessing it through the singleton.
Why it matters: Explicit parameters make a function’s dependence on game data easier to see.
Key terms: Game, game loop, MonoBehaviour, singleton, Awake(), Start(), Board, MainCamera, UIMainMenu, UIGameOver
5.3.1 Start()
Start() prepares the game’s data and UI at the game-loop entry point, before core gameplay begins.
- Start initialization data flow: Trace what Start() takes in, changes, and leaves ready: Game member variables provide assets already in memory; Start() allocates and initializes game data and board pools, activates the main menu, and leaves the UI menus in their correct active states.
Why it matters: This explains what must be ready before the game moves into play. - Startup allocation lifetime: Explain why this example allocates all needed memory during Start(): the game data lasts for the game’s lifetime, is deallocated automatically when the game closes, and needs no additional allocation per level or enemy. Distinguish that setup from calling StartGame() to begin core gameplay.
Why it matters: It separates one-time setup from the point where the player begins the core game.
Key terms: Start(), Game, game data, board pools, main menu, StartGame()
5.3.2 StartGame()
StartGame() prepares core gameplay by hiding non-gameplay menus, showing the Board, and connecting the main menu’s Start button to that setup.
- Core gameplay setup: Trace what StartGame() takes in, changes, and leaves ready for play: it uses UI members, the Board, and the main camera; hides non-gameplay menus; and calls Board.Show() to position and display gameplay sprites.
Why it matters: This explains what must be ready on screen before gameplay begins. - Start button binding: Describe how this simple example invokes StartGame() from the main menu’s Start button.
Why it matters: The binding connects the player’s Start-button action to gameplay setup.
Key terms: StartGame(), Board.Show(), main camera, screen coordinates, world coordinates, Start button, Unity editor
5.3.3 Update()
This section explains how Update() drives each frame of gameplay and how changes to GameData become visible on the board.
- Per-frame gameplay flow: Trace how Update() drives gameplay each frame: it uses the Board, Balance, GameData, and elapsed time; calls Board.Tick() while in the IN_GAME state; and reaches GameOver() through Board.Tick() when gameplay ends.
Why it matters: This identifies where each frame’s gameplay begins and how the game-over transition is reached. - Mutable GameData and visible output: Explain why Board.Tick() makes Update()’s output less obvious, and describe how the static Logic functions make data access clearer: they modify the passed-in GameData, which the caller uses to update sprites.
Why it matters: Following the changed GameData explains how gameplay logic produces visible results without mistaking the Logic functions for pure functions.
Key terms: Update(), Start(), IN_GAME, Board.Tick(), Time, GameOver(), static Logic functions, GameData, data out, deterministic
5.3.4 GameOver()
This section explains how GameOver() switches the visible screen to the game-over menu and distinguishes hiding the board from deallocating its data.
- Game-over screen transition: Describe how GameOver() hides the board sprites and in-game UI and shows the game-over UI by changing their active states.
Why it matters: It explains what the player sees when gameplay ends. - Hiding versus deallocating data: Distinguish hiding the board in this example from deallocating level-specific data, and state when the section expects garbage collection to follow.
Why it matters: It prevents confusing a visual change with releasing memory.
Key terms: GameOver(), board sprites, in-game UI, game-over UI, active state, deallocate, level-specific data, garbage collector
5.4 Conclusion
The section recaps the linear DOD gameplay architecture, contrasts it with tangled OOP interactions, and explains how Board and Game divide responsibilities in Unity.
- Linear core gameplay flow: Trace how Game, Board, and Logic work together: Game calls Board with Balance and GameData, Board calls Logic, Logic changes GameData using Balance when needed, and Board displays the result.
Why it matters: This flow makes it easier to see which part changes gameplay state and which part displays it. - DOD and OOP architecture contrast: Explain why the section describes this DOD architecture as easier to follow and modify than an OOP arrangement in which many Unity scripts can react to events and change one another’s data.
Why it matters: Clear relationships help a developer find the source of a behavior when debugging or adding features. - Board and Game Unity responsibilities: Distinguish Board’s Unity-facing gameplay duties from Game’s control of the overall loop. Explain why Board uses a MonoBehaviour but its own Init() and Tick() methods, while Game is a singleton MonoBehaviour that drives the loop through Unity’s default lifecycle functions.
Why it matters: Keeping loop control in Game prevents Board from becoming a second, independent driver of gameplay.
Key terms: Game, Board, Logic, Balance, GameData, DOD, OOP, OnTrigger, MonoBehaviour, singleton
5.5 Summary
This section summarizes how the Board, Game, and a single Unity scene divide responsibilities in the game architecture.
Where this is used later
- Connecting the menus in the game loop (chapter 8) · The menus are connected within the game loop.
- The main menu implementation (chapter 8) · Develops the main menu.
- The game over menu implementation (chapter 8) · Develops the game over menu.
- The in-game menu implementation (chapter 8) · Develops the in-game menu.
- Asset loading (chapter 7) · Covers asset loading after this section’s simple editor-assignment approach.
- Save/Load (chapter 7) · Board as the part that visually shows the game.
- The main menu implementation (chapter 8) · Board's existing elapsed-time formatting.
- Continuing gameplay (chapter 7) · Board.Show() as the method that displays the board
Concepts explained in this chapter
- main loopA recurring control structure that advances a game through updates, input handling, rendering coordination, and other gameplay systems.
- data-oriented programmingAn approach to structuring software around the flow and transformation of data rather than relationships between objects.
- Singleton patternA singleton is a pattern that restricts a class to one shared instance and provides centralized access to it.
- direct couplingAn explicit dependency in which one software component directly refers to and relies on another component.
- balance dataThe example game's designer-authored gameplay settings, kept separate from runtime state.
- Unity sceneA Unity asset that contains a collection of scene objects, environment settings, and other state used to construct a playable or editable portion of a game.
- object poolA preallocated collection of slots that holds data or objects for reuse.
- pure functionA function whose result depends only on its inputs and that produces no externally observable side effects.
- complexity metricA measure of the structural intricacy and difficulty of understanding, modifying, or executing software code.
- virtual joystickA touch- or pointer-driven on-screen control that represents directional input for a game character.
- early returnAn early return is a control-flow technique in which a function exits before reaching its normal completion point.
- branchControl flow that selects among different execution paths, commonly according to conditions.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
