Displaying multiple pickups of different objects as text totals via a dynamic Dictionary<> of PickUp objects and enum pickup types
While the previous recipe worked fine, any old text could have been typed into the description of a pickup or perhaps mistyped (star, Sstar, starr, and so on). A much better way of restricting game properties to one of a predefined (enumerated) list of possible values is to use C# enums
. As well as removing the possibility of mistyping a string, it also means that we can write code to deal with the predefined set of possible values. In this recipe, we will improve our general-purpose PickUp
class by introducing three possible pickup types (Star
, Heart
, and Key
), and write inventory display code that counts the number of each type of pickup being carried before displaying these totals via a UI Text object on the screen. We will also switch from using a List
to using a Dictionary
, since the Dictionary
data structure is designed specifically for key...