liveBook · Manning

Chapter 7: Separating data and logic

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

The introduction presents separating data from logic and keeping code deliberately simple as ways to reduce complexity and make future features easier to add.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction presents separating data from logic and keeping code deliberately simple as ways to reduce complexity and make future features easier to add.

Key terms: separating data from logic, DOD, OOP engine, code complexity, design patterns, interfaces, frameworks

7.1 Save/Load

This section explains why GameData should hold the state needed to restore gameplay while save and load logic stays separate from the data itself.

Key terms: data-oriented design, GameData, core gameplay, Logic, Board

7.1.1 Storing the save data

This section compares ways to store GameData and explains why the game writes and reads its save data as binary bytes.

Key terms: GameData, binary, JSON, XML, overhead, readability, serialization, deserialization, stream of bytes, FileStream

7.1.2 The GameDataIO static class

The section introduces GameDataIO for local saving and loading, and explains why loading modifies existing game data instead of allocating new data.

Key terms: GameDataIO, SaveLocal(), LoadLocal(), game data, memory fragmentation, garbage-collect, static logic function

7.1.3 Saving to a local file

This section explains how SaveLocal() writes GameData to a local file.

Key terms: SaveLocal(), BinaryWriter, GameData, FileStream

7.1.4 Loading from a local file

This section describes how LoadLocal() reads saved GameData, why it reuses an existing object, and what validation and compatibility issues remain unresolved.

Key terms: LoadLocal(), file stream, branch checks, GameData, garbage collector, memory fragmentation, enemy position arrays, backward compatibility

7.1.5 Backwards compatibility

The section shows how to keep manually serialized GameData saves loadable when the save format changes, using versioned files and separate loaders.

Key terms: GameData, serialization, deserialization, binary layout, version number, SaveLocal(), LoadLocal(), LoadLocalVersion1(), gamedata_v1.dat, gamedata_v2.dat

7.1.6 Handling GameData changes caused by Balance

The section explains how to load saved enemy data when a balance change alters array sizes, including a server-loaded array edge case.

Key terms: balance, number of enemies saved, allocated array, balance.NumEnemies, gameData.EnemyPosition.Length, Seek(), linear, backward compatibility, branch, server API

7.1.7 Debugging

This section explains how to diagnose binary save/load bugs by checking for mismatched reads and writes and inspecting centralized game data with a debugger.

Key terms: end-of-stream exception, binary data, wrong data type, data-oriented design, game data, breakpoint, debugger

7.1.8 Separating loading by source

This section argues for keeping save/load code separate by data source and sharing only small, data-oriented operations so changes to one feature do not endanger others.

Key terms: data source, database, static class, code duplication, shared code, branching, QA, static, DOD paradigm, data in

7.1.9 Continuing gameplay

This subsection explains how Board.Show() displays the appropriate enemies when starting a new game or continuing a saved one.

Key terms: Board.Show(), gameData.AliveEnemyCount, alive enemy indices array, alive enemy GameObjects

7.2 Asset loading

This section compares Unity asset-loading options and argues for choosing one based on the game's needs, including control over memory and CPU use.

Key terms: DOD, black-box system, asset loading, Resources folder, asset bundles, Addressables, black box, asynchronously, memory fragmentation, asynchronous asset loading

7.2.1 The asset manager

This section explains why the game uses a singleton asset manager to provide editor-assigned assets to different parts of the game.

Key terms: asset manager, singleton, initialization, [SerializeField], prefabs, instantiate, private, dedicated method, GetMainMenuUI(), GetMenu(var whichMenu)

7.2.2 Using the asset manager

Board retrieves player and enemy GameObjects through AssetManager, reducing editor-assigned references and making asset sources easier to manage.

Key terms: Board, AssetManager, asset manager singleton, GameObjects, public variables, Unity editor, distinct entry point, asset source

7.2.3 Multiple asset sources

When assets come from multiple sources, keep each source’s loading code separate so its origin is clear and problems are easier to debug.

Key terms: asset sources, bundles, resources folder, addressables, singletons

7.2.4 Other examples

This section matches asset-loading choices to the data being loaded and explains why live updates require particular caution.

Key terms: balance data, tool time, resource folder, runtime, asset bundles, shared common bundle, zone bundle, live-update, Addressables, content delivery network (CDN)

7.3 Conclusion

The conclusion shows how separating data, logic, and data sources keeps save/load and asset code easier to understand, change, and debug.

Key terms: game data, save state, asset data, logic, binary, JSON, XML, version number, LoadLocalVersion1(), LoadLocal()

7.4 Summary

The chapter summary connects the game’s data layout to save/load decisions and matches asset-loading approaches to the needs of the game.

Where this is used later

Concepts explained in this chapter

← Chapter 6: Common data structures: List, Stack, and Queue Chapter 8: Separating asset data and logic →

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.