6 Separating data and logic

 

This chapter covers

  • How separating data and logic reduces code complexity.
  • How to implement a save/load system.
  • How to implement asset management.

This chapter will explore how separating data from logic reduces code complexity, making our code simpler, cleaner, and easier to work with. We’ll expand on the simple game we created in Chapters 4 and 5 by adding a save/load system and asset management.

Note

The key point of this chapter is how we solve problems by separating the data from the logic. We’ll use Unity to show how to merge DOD code with an OOP engine, but how we solve problems applies to any engine and language.

The code we write in this chapter may seem simple, but that is by design. The first step in reducing code complexity is writing less code. We are going to avoid using design patterns, interfaces, or frameworks. We want our code to be as basic as possible. The less code we write, the simpler our code will be to understand, maintain, and extend. Plus, the less code we write, the less code the CPU has to process and the faster our game will run.

6.1 Save/Load

6.1.1 Storing the save data

6.1.2 The GameDataIO static class

6.1.3 Saving to a local file

6.1.4 Loading from a local file

6.1.5 Backwards compatibility

6.1.6 Debugging

6.1.7 Separating loading by source

6.2 Asset loading

6.2.1 The asset manager

6.2.2 Using the asset manager

6.2.3 Multiple asset sources

6.2.4 Other examples

6.3 Conclusion

6.4 Summary