I’m new to C# and Unity (used it for 2 weeks), and recently I have been bogged down about this problem.
I don’t know if it can be made cleaner, but basically I have an enum and struct that maps information about the Food.
public enum Food {
Apple,
Orange,
Pizza
};
public struct CalorieMap {
public int Apple;
public int Orange;
public int Pizza;
}
The method I can think of has the side effect that the number of lines in the function would linearly scale with the number of Food items (could also be a switch statement, but would have same number of lines as if statement).
public int GetCalories(Food food) {
if (food == Food.Apple) return CalorieMap.Apple;
if (food == Food.Orange) return CalorieMap.Orange;
if (food == Food.Pizza) return CalorieMap.Pizza;
return 0;
}
However, I was wondering if it was possible to do something like this which would be more maintainable and clean?
public int GetCalories(Food food) {
return CalorieMap[Food];
}
Of course I could use a Dictionary, but with a struct, is it possible? The reason why I wanted to use the struct is because I initialise/customise the caloric contents in the inspector. If I have 1000 foods, then the first code would be inefficient (would be over 1000 lines) and I would prefer having the one-liner instead. Another problem with using a Dictionary is that the user might have forgotten to set one of the calories for a food item, but I guess I could check that in run-time or something. Although, if using a dictionary is actually best practice, let me know.
I have been trying to think of the best way to solve it and the best practices that would be easy to maintain. Instead of those ints, those could also be a Sprite, or an Object, or any kind of mapping.
Maybe the food has other information, such as portion size, density, etc. Even if I extract it out Foods to classes, it still has linear line complexity for the methods unless there is a special technique I can use in Unity C#.
Thanks in advance!