liveBook · Manning

Chapter 10: DOD and Dictionaries

What you will learn in chapter 10 of High Performance Unity Game Development

The introduction contrasts dictionary lookups with indexed arrays and explains why data-oriented design favors arrays for enemy balance data despite dictionaries’ O(1) complexity.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction contrasts dictionary lookups with indexed arrays and explains why data-oriented design favors arrays for enemy balance data despite dictionaries’ O(1) complexity.

Key terms: dictionary, hash table, array, O(1), memory access pattern, balance data, key, arrays, index, data-oriented design

10.1 Dictionary performance

This section explains why dictionary lookups can be slower than array access in gameplay code and how integer enemy IDs allow direct balance-array lookups.

Key terms: dictionary, resized, memory fragmentation, garbage collection, key, hash, bucket, hash collision, entries array, collision chain

10.2 Supporting multiple enemies in the balance

The section identifies the data needed to add a second, bigger, faster, rarer-looking enemy to the game.

Key terms: data-oriented design, velocity, radius, weight, GameObject

10.2.1 Storing the new enemy data

This section sets up separate ScriptableObjects for enemy types and explains how their prefab names connect designer-assigned visual data to prefabs available in-game.

Key terms: ScriptableObject, weight, ID, unique enemy name, balance parsing, prefab, AssetManager, m_enemyPrefabs, [SerializeField], prefab name

10.2.2 Parsing the balance data

This section explains how BalanceParser gathers enemy ScriptableObject data when parsing the balance.

Key terms: BalanceParser, parse(), AddObjectsFromDirectory(), enemy ScriptableObject

10.2.3 Loading the balance data

The section updates Balance for multiple enemy types and states that loading the balance allocates and fills the new per-type data arrays.

Key terms: Balance, MaxEnemies, NumEnemyTypes, EnemyVelocity, EnemyRadius

10.2.4 Referencing different enemies

The section explains how enemy types are linked to balance-array data and how spawn weights are supplied for multiple enemy types.

Key terms: enemy type, ID field, EnemySO, balance arrays, AssignIDs(), SpawnData, weights, BalanceSO, tooltime, EnemyVelocity

10.3 Runtime data

The section adds runtime tracking of each enemy’s type so the game can select the corresponding balance data.

Key terms: EnemyType array, GameData, balance

10.4 The gameplay logic

This section updates gameplay logic to allocate enemy-type data, choose spawn types by balance weights, and use each enemy’s type for movement and collision checks.

Key terms: EnemyType, AllocateGameData(), balance.MaxEnemies, getRandomEnemyTypeByWeight(), spawnEnemy(), EnemySO, gameData.EnemyType, moveEnemies(), doEnemyToEnemyCollision(), checkGameOver()

10.5 The board

Explains why a pool for randomly spawned enemy types needs a fixed capacity but must allocate individual enemies as needed.

Key terms: GameObject pool, enemy types, memory fragmentation, garbage collection

10.5.1 Setting the pool size

This section sizes and manages a type-aware GameObject pool so the Board can display enemies whose runtime mix differs from their spawn weights.

Key terms: MaxEnemyPoolSize, Board, Balance, weights, m_enemyPool, m_enemyPoolType, m_enemyPoolUsed, m_enemyToPoolIndex, m_enemyPoolCount, EnemyType

10.5.2 Showing newly added enemies

This section explains how the board displays newly spawned enemies by selecting a GameObject from the appropriate pool and retaining its pool index for later updates.

Key terms: addedEnemyIndices, addedEnemyCount, gameData.EnemyType, getFreeEnemyPoolIndex(enemyType), pool index, enemy index

10.5.3 Hiding removed enemies

This subsection explains how to hide a removed enemy’s GameObject when enemy indices must first be mapped to pool indices.

Key terms: removedEnemyIndices, pool index, GameObject, active=false, unused

10.5.4 Moving alive enemies

Explains how the board identifies the correct GameObject when moving an alive enemy.

Key terms: pool index, GameObject

10.6 Backwards compatibility

The section explains why enemy indices can break saved games when balance files change and compares two ways to preserve backward compatibility.

Key terms: EnemySO, IDs, balance, alphanumeric order, linearly increasing number, file list, unique identifier, game data, index

10.6.1 The Implementation

The section shows how to preserve saved enemy types when their numeric indices change in a new balance, using identifier lookup tables and a dictionary confined to loading.

Key terms: identifier, index, lookup tables, array, dictionary, unique identifier, EnemyNameToID, current balance, gameData.EnemyType, preallocate

10.6.2 Handling edge cases

Explains how swapping enemy type IDs during balance updates can overwrite data and how to prevent it.

Key terms: enemy types, new balance, stack-allocated array, garbage collector

10.7 Conclusion

This conclusion explains how to avoid frequent dictionary access during gameplay and how to manage an object pool when its contents cannot be known in advance.

Key terms: dictionaries, tooltime, indices, balance data, arrays, backward compatibility, runtime, object pools, maximum pool size, unused

10.8 Summary

This summary explains how tool-time indexing, preallocated pools, and load-time mappings keep costly lookups and memory changes out of gameplay.

Where this is used later

Concepts explained in this chapter

← Chapter 9: Tooltime data parsing Chapter 11: Branching, performance, and extendibility →

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.