liveBook · Manning

Validation

As explained in High Performance Unity Game Development

The process of checking data or references against validity constraints before use.

Suppose a designer sets the number of enemies to zero. If the game checks that value every time it allocates enemy arrays, a data-entry mistake adds branches to runtime code. Validate the balance before parsing it instead: reject the bad value during tool time, then let allocation use the checked data. section 9.4 section 9.3

Keep the boundary explicit:

  • Load the editable balance asset and check constraints that matter, such as an enemy count greater than zero and no higher than its allowed maximum. section 9.4
  • Throw an exception on failure so invalid data is not parsed or saved. Call validation before parsing. section 9.4
  • Check assigned sprites, prefabs, or other references when missing assignments cause problems. Avoid an exhaustive check of every field without a reason. section 9.4
Listing 9.8 A function to validate our data before parsing
public static void validate()
{
    BalanceSO balanceSO =
        (BalanceSO)AssetDatabase.LoadAssetAtPath(
            "Assets/Data/Balance.asset", typeof(BalanceSO));
 
    if (balanceSO.NumEnemies <= 0)
        throw new System.InvalidOperationException(
            "NumEnemies needs to be bigger than 0! Currently set to " +
            balanceSO.NumEnemies);
 
    if (balanceSO.NumEnemies > 10000)
        throw new System.InvalidOperationException(
            "NumEnemies is set to " + balanceSO.NumEnemies +
            ". Max is 10000");
}

The Unity editor validation function loads a balance ScriptableObject and rejects enemy counts outside the permitted range before the balance is parsed. section 9.4

Validation belongs at boundaries, not repeatedly inside a controlled update loop. Loading disk or server data, parsing files, handling user input, and accepting editor-assigned references can all warrant checks. Once an assumption has been established, investigate its cause if it later breaks rather than hiding the bug behind another early return. Moving checks out of hot code can simplify it and avoid unnecessary branches; it does not make every runtime check disposable. section 11.2 section 9.7

How High Performance Unity Game Development covers it

How deep it goes
Works through a Unity validation function, failure exceptions, and its place before parsing, while recommending selective rather than exhaustive checks.
Examples
Contrasts repeated enemy-array allocation checks with a pre-parse enemy-count check and considers missing assigned assets.
What it assumes
Assumes familiarity with Unity ScriptableObjects and game-balance data; demonstrates where a validation function fits in the parsing workflow.
What you can do after
Build it from scratch as a Unity editor check that rejects invalid balance values before parsing and removes redundant runtime checks.
How it connects
Connects ScriptableObjects and binary parsing to data-oriented allocation, runtime branches, and validation at system boundaries.

Also explained in

Compare how 44 books explain validation →

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.