chapter ten

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.

10.1 Dictionary performance

10.2 Supporting multiple enemies in the balance

10.2.1 Storing the new enemy data

10.2.2 Parsing the balance data

10.2.3 Loading the balance data

10.2.4 Referencing different enemies

10.3 Runtime data

10.4 The gameplay logic

10.5 The board

10.5.1 Setting the pool size

10.5.2 Showing newly added enemies

10.5.3 Hiding removed enemies

10.5.4 Moving alive enemies

10.6 Backwards compatibility

10.6.1 The Implementation

10.6.2 Handling edge cases

10.7 Conclusion

10.8 Summary