10 DOD and Dictionaries
This chapter covers
- The dictionary data structure and its impact on performance.
- Avoiding dictionary usage in runtime by assigning indices at tooltime.
- Accessing balance data using indices saved in game data.
- Efficiently implementing dynamic object pools.
Arguably, the most popular data structure used in programming today is the dictionary. As an O(1) complexity data structure, dictionaries are used to solve numerous problems, from storing data to counting frequencies to caching frequently used data. Conveniently, C# and many modern languages include a built-in dictionary data structure. Many, many interview questions are designed so that using a dictionary is the optimal solution.
Unfortunately, dictionaries do not work well with data-oriented design. If we were building an OOP survivor game and wanted to support different enemy types, a dictionary would be a good choice for storing their balance data. We could use anything as the key, even the name of the enemy, and then store all the data in an object inside the dictionary. To access the data, all we would need is the key.
As we saw in chapter 9, with data-oriented design, we want to store all our data in arrays and to use an index into those arrays to retrieve our data. This is especially true for problems where a dictionary would normally be the solution. In this chapter, we’ll show how to add multiple enemy types to our game without using a dictionary.