I’m working on a building game that has a number of resources like wood, stone, and metal- I’m not finished iterating on the design, but it looks like I’ll end up with 20-25 unique resource types. Each resource is just represented as an enum, and every category of resource is stored as a custom ResourceStorageSlot struct:
[System.Serializable]
public struct ResourceStorageSlot
{
public ResourceType resource; //Wood, metal, or something else
public int amount; //Amount held in whole units
public int capacity; //Total possible amount that can be held
}
Where I’m finding myself indecisive is how I should actually be storing these; my first thought was to create a dictionary<ResourceType, ResourceStorageSlot>, and just get/set resources via resourceDict[ResourceType.Wood] or what have you. However, once initialized the dict will never change (since every game will let you store the same set of resources), and I don’t like blindly trusting that my desired key will be present, even if I explicitly initialize it every time.
The alternative that seems obvious to me would be using a list manually configuring it in the inspector, and doing something like the following:
List<ResourceStorageSlot> resourceList;
public void AddResource(ResourceType newType, int newAmount){
switch(newType){
case ResourceType.Wood:
resourceList[woodIndex].Amount+= newAmount;
}
}
As far as I can tell the dict is preferable, since my understanding is that dictionaries take longer to add/insert data into than lists, but less time to search and get data out of, but I have no clear, technical criteria to base my decision on. I could moderate some of the list iteration by using a switch to test the incoming ResourceType in advance and target a specific index, but I suspect the switch block would average out to the same number of operations over time as the iteration. Is either of these options (or something I missed entirely) obviously preferable for reasons I’m unaware of?