Chapter 4: Data and logic
Introduces the survivor game used to illustrate a data-oriented architecture, distinguishing balance, runtime game data, and logic while outlining the example’s gameplay and pooling constraint.
After this chapter you will be able to
- Balance game data and logic. Distinguish balance data set by designers at tool time from game data changed at runtime, and describe logic as modifying the game data. Explain that these are the engine-agnostic parts addressed here, while the board handles input and visuals and the game loop drives the board and menus.
- Balance and GameData split. Distinguish Balance from GameData by whether the data changes during gameplay, and give an example of each from the survivor game or another example in the section.
- Logic ownership of state changes. Explain how static Logic functions transform data and why the architecture reserves GameData changes for those functions.
- Board and Game responsibilities. Describe the distinct jobs of Board and Game, and how each uses Logic without Logic calling them directly.
- Balance and GameData boundary. Distinguish settings that should not change during gameplay, which go in Balance, from data that changes during gameplay, which goes in GameData.
- GameData contents. Describe the gameplay data stored in GameData: enemy positions, player direction, elapsed and best times, and menu state.
- Omitted gameplay values. Explain why GameData does not store enemy velocity, enemy direction, or player position.
- Player centered coordinates. Explain how keeping the player at the origin and moving enemies relative to it helps preserve floating-point accuracy.
- Logic only GameData updates. Justify why only the Logic class should modify GameData.
- Static logic and GameData updates. Explain how Logic follows the data-oriented pattern and why the architecture makes its class and methods static.
- Deterministic self-contained Logic. Explain why Logic is a static class and what it means for its functions to be deterministic and self-contained.
- Mutable and read-only data roles. Distinguish the roles of GameData and Balance in Logic functions, and explain why those functions are not necessarily pure.
- Enemy position allocation flow. Trace how AllocateGameData uses Balance to determine the number of enemies and allocates the enemy-position array in GameData.
- Allocation parameter tradeoff. Explain the alternative of passing enemyPosition and the enemy count directly, why enemyPosition needs to be passed by reference, and the tradeoff against passing GameData and Balance.
- Gameplay data reset. Explain when StartGame() runs and how it prepares GameData for play without allocating data, including what it resets for a new game or retry.
- Starting enemy positions. Describe why and how StartGame() positions the enemies in this simple survivor game.
- Frame logic data flow. Explain what Logic.Tick() receives, what it changes or returns, and how the Board uses those results.
- Per-frame gameplay sequence. Trace the gameplay actions Tick() performs in this example, including why the bounds check precedes enemy-to-enemy collision checks.
- Mouse movement direction update. Trace how MouseMove() uses the current local position and mouse-down position to update the player's direction in GameData for Tick() to use.
- Logic ownership of GameData changes. Justify keeping input-driven GameData changes in static Logic functions rather than in Game or Board, and explain why mouse down needs no gameplay action in Logic.
- Movement Based Spawn Patterns. Explain how the player's movement determines the spawn pattern and how rotating a vector produces a random direction within that pattern.
- Spawn Function Data Flow. Describe what getEnemySpawnPosition() takes in and returns, and where the resulting position is used.
- Tick gameplay operation order. Trace the order in which Tick() calls its gameplay functions and describe how its game-over result reaches the Board.
- Enemy movement toward origin. Explain how moveEnemies() updates enemy positions and why it normalizes their direction vectors.
- Enemy overlap detection and response. Explain how the section detects overlapping enemies, moves them apart, and avoids an unnecessary square root.
- Player movement through enemy positions. Explain how movePlayer() simulates player movement while keeping the player at the center.
- Game-over distance test. Explain how checkGameOver() detects an enemy reaching the player and what it changes.
- Architecture component responsibilities. Describe what Balance, GameData, Board, and the Game loop each do, and distinguish design-time data from changing gameplay state.
- Logic as sole data modifier. Explain the role and data-oriented pattern of the Logic functions.
What it builds on
- Reducing code complexity by separating data and logic (chapter 1) · The earlier discussion of separating data and logic.
- Using object pools to avoid memory fragmentation and garbage collection (chapter 3) · The earlier treatment of object pools as a way to avoid allocations.
- Copying by value and performance (chapter 2) · The performance implications of copying by value.
- Struct vs. class - how data affects performance (chapter 2) · The choice of a class for a data holder that includes heap-stored arrays.
- Passing by value vs. by reference - performance and common issues (chapter 2) · The distinction between passing values and passing references.
- Data locality and contiguous memory - arrays of structs vs arrays of classes (chapter 2) · The performance benefit of storing data in contiguous arrays.
Section by section
Introduction
Introduces the survivor game used to illustrate a data-oriented architecture, distinguishing balance, runtime game data, and logic while outlining the example’s gameplay and pooling constraint.
- Balance game data and logic: Distinguish balance data set by designers at tool time from game data changed at runtime, and describe logic as modifying the game data. Explain that these are the engine-agnostic parts addressed here, while the board handles input and visuals and the game loop drives the board and menus.
Why it matters: This separation establishes what each part of the example architecture is responsible for. - Survivor example gameplay rules: Describe the basic survivor-game scenario: the player avoids enemies that move toward them, contact ends the game, and enemy spawning depends on whether the player is moving.
Why it matters: These rules give a concrete setting for the data and logic developed in the chapter. - GameObject pool predictability: Explain why the example’s identical enemies make a GameObject pool easy to preallocate, and contrast that with a Merge-2 game whose needed objects cannot be predicted before play.
Why it matters: It identifies a limit of the simple example’s allocation strategy.
Key terms: balance, game data, logic, board, game loop, survivor/bullet-heaven game, player, enemies, GameObject pool, preallocate
4.1 Simple DOD Architecture
The section introduces a simple data-oriented game architecture that separates fixed Balance data from changing GameData and assigns distinct roles to Logic, Board, and Game.
- Balance and GameData split: Distinguish Balance from GameData by whether the data changes during gameplay, and give an example of each from the survivor game or another example in the section.
Why it matters: This split determines where the game's rules and its changing state belong. - Logic ownership of state changes: Explain how static Logic functions transform data and why the architecture reserves GameData changes for those functions.
Why it matters: One place for state changes makes gameplay behavior easier to trace and reuse. - Board and Game responsibilities: Describe the distinct jobs of Board and Game, and how each uses Logic without Logic calling them directly.
Why it matters: The call direction keeps gameplay logic separate from the code that runs and displays the game. - Simpler engine-independent core: Explain the section's reason for favoring a simpler codebase and identify which parts of the architecture are engine agnostic.
Why it matters: The separation is intended to keep the core design simple without making it dependent on Unity.
Key terms: Balance, GameData, tool time, runtime, Logic, static logic functions, Board, Game, game loop, DOD
4.2 The Balance
This section defines which survivor-game settings belong in Balance, why Balance is a class, and how designers can edit its values.
- Balance and GameData boundary: Distinguish settings that should not change during gameplay, which go in Balance, from data that changes during gameplay, which goes in GameData.
Why it matters: This distinction determines where each piece of game data belongs. - Balance class choice: Explain why Balance is a class rather than a struct in this example.
Why it matters: The choice avoids copying a potentially growing collection of settings when passing Balance around. - Enemy count and shared settings: Describe how NumEnemies, EnemyVelocity, and EnemyRadius serve the example game, including why speed and radius need only one value each.
Why it matters: Shared settings avoid storing duplicate values for every enemy. - Spawn movement and collision settings: Describe what SpawnRadius, PlayerVelocity, and MinCollisionDistance control, including the collision threshold rule.
Why it matters: These settings let the example control spawning, movement, and collision checks without treating them as changing game state. - Editor-based balance configuration: Explain how the simple example lets a designer set Balance values in Unity.
Why it matters: Designers can tune this simple game's settings in the editor rather than changing gameplay code.
Key terms: Balance, GameData, class, struct, by value, by reference, NumEnemies, EnemyVelocity, EnemyRadius, enemy arrays
4.3 The GameData
The section defines what the survivor game's GameData stores, why some values are omitted, and how player-centered coordinates and Logic-only updates shape its use.
- GameData contents: Describe the gameplay data stored in GameData: enemy positions, player direction, elapsed and best times, and menu state.
Why it matters: These fields define the state the game logic operates on and can preserve. - Omitted gameplay values: Explain why GameData does not store enemy velocity, enemy direction, or player position.
Why it matters: Choosing what not to store keeps GameData focused on state that actually needs to persist. - Player centered coordinates: Explain how keeping the player at the origin and moving enemies relative to it helps preserve floating-point accuracy.
Why it matters: The coordinate choice prevents long-running movement from undermining calculations near the player. - Player relative data boundary: Distinguish the player-relative gameplay data in GameData from what the Board may draw on screen.
Why it matters: Player-centered gameplay coordinates do not constrain the entire visual world. - Logic only GameData updates: Justify why only the Logic class should modify GameData.
Why it matters: A single owner for state changes makes gameplay reproducible without the visual layer.
Key terms: GameData, EnemyPosition, PlayerDirection, GameTime, BestTime, MenuState, enemy velocity, enemy direction, player position, balance
4.4 The Logic
This section establishes Logic as a static class whose methods transform data and provide the intended way to modify GameData.
- Static logic and GameData updates: Explain how Logic follows the data-oriented pattern and why the architecture makes its class and methods static.
Why it matters: This explains where changes to the game's data are meant to happen and why those functions can be called throughout the game.
Key terms: Logic, GameData, static functions, DOD
4.4.1 Logic accessibility
This section defines the boundaries of the static Logic class and frames its public methods in terms of the data they receive, change, and return.
- Deterministic self-contained Logic: Explain why Logic is a static class and what it means for its functions to be deterministic and self-contained.
Why it matters: These boundaries make the inputs and timing of gameplay logic explicit. - Mutable and read-only data roles: Distinguish the roles of GameData and Balance in Logic functions, and explain why those functions are not necessarily pure.
Why it matters: It clarifies how Logic can have predictable inputs while still updating the game's runtime state. - Data-oriented public interface: Describe the data-oriented way to examine a Logic function and identify the five public-method roles in this game architecture.
Why it matters: This gives the reader a way to describe each public Logic function without mixing its job with hidden game state.
Key terms: Logic, static class, deterministic, self-contained, hidden state, GameData, Balance, pure, mutable runtime state, read-only input
4.4.2 AllocateGameData() - Allocating all the data for our game
This section explains how the game allocates its enemy-position data and why its allocation function takes GameData and Balance rather than individual values.
- Enemy position allocation flow: Trace how AllocateGameData uses Balance to determine the number of enemies and allocates the enemy-position array in GameData.
Why it matters: This distinguishes allocating storage for gameplay data from initializing its values. - Fixed enemy array lifetime: Explain why this game's EnemyPosition array can be allocated at game start and retained until the game exits.
Why it matters: A fixed enemy count lets this example use one allocation across all levels. - Allocation parameter tradeoff: Explain the alternative of passing enemyPosition and the enemy count directly, why enemyPosition needs to be passed by reference, and the tradeoff against passing GameData and Balance.
Why it matters: The choice of parameters affects both how reliably the new array is assigned and how easy the code is to work with.
Key terms: AllocateGameData, GameData, Balance, EnemyPosition, deallocating, reference, multithreading
4.4.3 StartGame() - Initializing our gameplay data
StartGame() prepares the existing GameData for each new play session by resetting gameplay values and positioning every enemy.
- Gameplay data reset: Explain when StartGame() runs and how it prepares GameData for play without allocating data, including what it resets for a new game or retry.
Why it matters: A retry can reuse allocated data while reliably returning gameplay to its starting state. - Starting enemy positions: Describe why and how StartGame() positions the enemies in this simple survivor game.
Why it matters: Positioning every active enemy up front leaves the enemy data ready for gameplay.
Key terms: StartGame(), GameData, Balance, menu state, game time, player direction, enemy arrays, getEnemySpawnPosition(), starting locations
4.4.4 Tick() - Handling frame-by-frame gameplay
This section explains how Logic.Tick() processes one frame of gameplay and passes the resulting changes to the Board for display.
- Frame logic data flow: Explain what Logic.Tick() receives, what it changes or returns, and how the Board uses those results.
Why it matters: This keeps the frame's gameplay decisions in Logic while letting the Board show their results. - Per-frame gameplay sequence: Trace the gameplay actions Tick() performs in this example, including why the bounds check precedes enemy-to-enemy collision checks.
Why it matters: The order shows both what a frame must accomplish and where this example avoids unnecessary collision checks. - Explicit function names and complexity: Explain why the section gives Tick()'s functions explicit names while warning that a short function is not necessarily easy to understand.
Why it matters: Clear names help readers navigate Tick(), but they should not mistake a tidy-looking function for fully understandable logic.
Key terms: Logic.Tick(), Board, GameData, balance, gameOver, game time, out of bounds, enemy-to-enemy collision detection and response, game over, explicit names
4.4.5 Handling user input
This section shows how mouse movement and release update the player's direction in GameData, and why those updates belong in static Logic functions.
- Mouse movement direction update: Trace how MouseMove() uses the current local position and mouse-down position to update the player's direction in GameData for Tick() to use.
Why it matters: It connects a mouse drag to the direction the gameplay update uses to move the player. - Mouse release direction reset: Describe what MouseUp() changes and why the player stops moving when the mouse is released.
Why it matters: It explains how releasing the mouse ends movement without a separate stop command to Tick(). - Logic ownership of GameData changes: Justify keeping input-driven GameData changes in static Logic functions rather than in Game or Board, and explain why mouse down needs no gameplay action in Logic.
Why it matters: Keeping state changes in Logic prevents gameplay rules from depending on Unity when run elsewhere.
Key terms: MouseMove(), GameData, mouse-down position, player direction, Tick(), MouseUp(), zero vector, DOD, static logic function, Game
4.4.6 getEnemySpawnPosition() - Placing an enemy within range of the player
This section explains how getEnemySpawnPosition() uses player movement and a configured spawn radius to choose an enemy spawn position.
- Movement Based Spawn Patterns: Explain how the player's movement determines the spawn pattern and how rotating a vector produces a random direction within that pattern.
Why it matters: The spawn pattern keeps enemies around a stationary player or ahead of one who is moving. - Spawn Function Data Flow: Describe what getEnemySpawnPosition() takes in and returns, and where the resulting position is used.
Why it matters: Keeping the inputs and returned position clear makes the same placement calculation usable at game start and during gameplay.
Key terms: player direction, up vector, spawn radius, 180-degree arc, getEnemySpawnPosition(), GameData.PlayerDirection, Balance.SpawnRadius, Data-In, Transformation, Data-Out
4.4.7 Functions called by Tick()
This section explains the ordered gameplay operations called by Tick(), from moving and repositioning enemies through collision handling, simulated player movement, and game-over detection.
- Tick gameplay operation order: Trace the order in which Tick() calls its gameplay functions and describe how its game-over result reaches the Board.
Why it matters: The order shows how one gameplay tick produces the state the Board displays. - Enemy movement toward origin: Explain how moveEnemies() updates enemy positions and why it normalizes their direction vectors.
Why it matters: Normalizing separates the direction of travel from the speed set in balance. - Out-of-bounds enemy respawning: Describe when and why checkEnemyOutOfBounds() repositions an enemy.
Why it matters: Repositioning keeps outrun enemies relevant without repeatedly respawning them at the boundary. - Enemy overlap detection and response: Explain how the section detects overlapping enemies, moves them apart, and avoids an unnecessary square root.
Why it matters: This gives a straightforward collision response while avoiding a costly calculation in every pairwise check. - Player movement through enemy positions: Explain how movePlayer() simulates player movement while keeping the player at the center.
Why it matters: The apparent movement preserves a fixed player position for the other gameplay calculations. - Game-over distance test: Explain how checkGameOver() detects an enemy reaching the player and what it changes.
Why it matters: The fixed player position makes the game-over collision check a direct test of each enemy's position.
Key terms: Tick(), Board, gameOver, moveEnemies(), balance, delta time, normalized vector, origin, checkEnemyOutOfBounds(), spawn radius
4.5 Conclusion
Summarizes the roles of the five components in the data-oriented game architecture and the implementation choices for Balance, GameData, and Logic.
- Architecture component responsibilities: Describe what Balance, GameData, Board, and the Game loop each do, and distinguish design-time data from changing gameplay state.
Why it matters: These responsibilities clarify which part of the architecture owns each kind of data or player-facing work. - Logic as sole data modifier: Explain the role and data-oriented pattern of the Logic functions.
Why it matters: Keeping gameplay changes in Logic gives the architecture a clear place for state transformations. - Balance and GameData class choices: Justify why this example uses classes for Balance and GameData, and describe what the GameData arrays contain.
Why it matters: The choice of data holder follows the characteristics of the data being stored.
Key terms: Balance, GameData, Board, Game loop, Logic, deterministic static functions, game data, data-oriented pattern, class, arrays
4.6 Summary
Summarizes how Balance, GameData, and static Logic divide the game’s rules, changing state, and state updates, then gives guidelines for keeping logic functions clear.
Where this is used later
- (chapter intro) (chapter 5) · The DOD architecture introduced in the previous chapter.
- DOD architecture, continued (chapter 5) · Introduces the simple DOD architecture that Board belongs to.
- The Board implementation (chapter 5) · Implements the Board's gameplay-facing responsibilities.
- The Game loop implementation (chapter 5) · Implements the Game loop.
- Game (chapter 5) · The basic DOD architecture's separation of responsibilities.
- Handling GameData changes caused by Balance (chapter 7) · The balance that determines gameplay data.
- Start() (chapter 5) · The game data allocated during initialization.
- Replacing List with an array (chapter 6) · The earlier allocation of gameplay data for enemies.
Concepts explained in this chapter
- balance dataThe example game's designer-authored gameplay settings, kept separate from runtime state.
- brute-force distance checkA collision-checking technique that tests distances between every pair of entities directly.
- data-oriented programmingAn approach to structuring software around the flow and transformation of data rather than relationships between objects.
- static classA class that cannot be instantiated and whose members are associated with the class itself rather than with object instances.
- main loopA recurring control structure that advances a game through updates, input handling, rendering coordination, and other gameplay systems.
- vector normalizationThe operation of scaling a nonzero vector to have a magnitude of one while preserving its direction.
- object poolA preallocated collection of slots that holds data or objects for reuse.
- pass by referenceA parameter-passing behavior in which a function receives access to the original argument or its storage location rather than an independent copy.
- pass by valueA parameter-passing behavior in which a function receives a copy of an argument's value.
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.
