liveBook · Manning

Data-oriented programming

As explained in High Performance Unity Game Development

An approach to structuring software around the flow and transformation of data rather than relationships between objects.

In a survival game, hundreds of enemies may need to move every frame. An object-oriented design might give each enemy its own position, direction, velocity, and Move() method. Data-oriented programming instead asks what the movement calculation reads and writes, then organizes the data and logic around that transformation. It is the data-and-logic separation aspect of data-oriented design. section 1.2.1 section 1.3

Store positions, directions, and velocities in arrays; process them with functions that take the required data as input and update the positions. A loop makes the work explicit. Keeping data used together close in memory can also make more of each CPU cache line useful, reducing time spent waiting for memory rather than calculating movement. section 1.3 section 1.2.1 section 1.6

Listing 1.7 Class holding all the data for our enemies
public class EnemyData
{
    public Vector2[] Position;
    public Vector2[] Direction;
    public float[] Velocity;
    public bool[] TeleportOnHit;
}

The enemy-data listing places position, direction, velocity, and teleport-on-hit flags in separate arrays rather than inside individual enemy objects. section 1.3

Add a teleport-on-hit enemy

Start with the new behavior’s inputs: position, damage received, and whether this enemy teleports when hit. Add a function that calculates the resulting position instead of first finding a place for the behavior in an inheritance hierarchy. The same approach can set an enemy’s direction toward the player or change its velocity based on distance. section 1.3 section 1.4

For a playable Unity implementation, keep designer-set rules in Balance and changing state in GameData. Static Logic functions transform GameData using rules and input. Board handles input and displays the result; one Game script drives the loop. This makes the order of updates and the ownership of state easier to follow than independent Update() calls on many GameObjects. section 4.6 section 5.1 section 5.1.1

Preallocated arrays can serve as data pools alongside a GameObject pool. Track whether an enemy is alive in its own data array: a GameObject’s active state may change merely to hide enemies while the game is paused. Allocate capacity deliberately and validate indices. For frequent, large workloads this structure can be useful; a small menu or loading-only tool may gain little. Profile the target hardware before adding further optimization. Unity GameObjects can still handle presentation, and Burst, Jobs, and ECS are optional—not prerequisites. section 3.3.4 section 1.1.1 section 1.2 section 1.5

How High Performance Unity Game Development covers it

How deep it goes
Works through array-based enemy behavior and a five-part Unity game architecture, then discusses pooling and selective optimization.
Examples
Uses moving and teleporting enemies, an enemy-data array listing, and a Unity survival-game loop.
What it assumes
Assumes familiarity with object-oriented game code while introducing the array-and-function alternative before ECS.
What you can do after
Build it from scratch: organize gameplay state in arrays, transform it with static functions, and drive a simple Unity game loop.
How it connects
Connects data locality and CPU caches to object-oriented programming, game loops, object pooling, and optional Unity DOTS tools.

Also explained in

Compare how 6 books explain data-oriented programming →

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.