liveBook · Manning

Branch

As explained in High Performance Unity Game Development

Control flow that selects among different execution paths, commonly according to conditions.

A game loop may need to spawn enemies, move them, handle collisions, and check for game over. A branch—such as an if, switch, or conditional operator—selects which work runs. Each added condition also gives a reader or debugger another path to consider. section 11.0 section 5.2.5

Listing 11.2 The Tick() function with a lot of branches
public static void Tick()
{
    if(canSpawnEnemy())
        spawnEnemy();
 
    if(canMoveEnemies())
        moveEnemies();
 
    if(shouldBoundCheck())
        checkEnemyOutOfBounds();
 
    if(collisionsOn())
        doEnemyToEnemyCollision();
 
    if(canMovePlayer())
        movePlayer();
 
    checkGameOver();
}

The conditional Tick() makes each game-loop step depend on a separate decision. Compare it with the straight-line Tick() in the same section: the latter makes the sequence of calls immediately visible. section 11.0

Branches in hot loops add instructions and can disrupt the CPU's work when a predicted path is wrong. Unpredictable conditions are especially costly; even a predictable check can cost more than a branchless alternative. This is a reason to examine repeated checks, not to remove every if statement. section 11.0 section 11.8

Move decisions to where they are needed, rather than repeating them for every object or hiding them inside shared logic:

  • Keep alive and dead enemy indices in separate arrays, then iterate the alive array without testing every enemy's status. Moving indices between arrays requires bookkeeping. section 11.0
  • Keep an input decision in the board's input handler instead of passing all input to a deeper logic function that must make the same decision. section 5.2.5
  • Use separate scripts for menu top bars whose behavior differs, rather than growing one shared script full of menu-specific conditions. Share simple, single-purpose logic where appropriate. section 11.5 section 7.1.8

Watch for less visible decisions too. A defaulted C# parameter can conceal a conditional inside a function whose call shows no such argument. Repeated Unity GameObject null checks also do more work than an ordinary reference check; when ownership and loading rules permit, check once rather than repeatedly in a hot loop. Prefer the simpler structure when the extra arrays or scripts would not earn their maintenance cost. section 11.6 section 11.7 section 11.0

How High Performance Unity Game Development covers it

How deep it goes
Defines branches, compares conditional and straight-line code, and explains prediction costs and ways to relocate or remove checks.
Examples
Compares conditional Tick() calls, separated enemy indices, menu top bars, input handling, and a defaulted enemy-movement parameter.
What it assumes
Assumes familiarity with C#-style game functions and Unity input while defining a branch from a basic if statement.
What you can do after
Diagnose/debug with it by spotting repeated or hidden checks and moving decisions outside hot loops or shared feature code.
How it connects
Connects branching to data-oriented design, branch prediction, CPU instruction caches, Unity null checks, and shared-code complexity.

Also explained in

Compare how 7 books explain branch →

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.