I’ve always felt the system for maps (mapping of values to keys) is a better route to follow than looping to check for stats. To that end, I had this code in place:
static readonly Array characterClasses = Enum.GetValues(typeof(CharacterClass));
[SerializeField] ClassProgression[] classes = new ClassProgression[characterClasses.Length];
public ClassProgression this[in CharacterClass index] => classes[(int)index];
void Awake()
{
for (int i = 0; i < classes.Length; ++i)
{
// Needs to be cast to obtain access
(classes[i] as IClassProgressionAccess).CharacterClass = (CharacterClass)characterClasses.GetValue(i);
}
}
The use of IClassProgressionAccess
is to restrict access to the internal characterClass
parameter to the Progression class alone, which sets up a mapping with respect to the CharacterClass values. While this makes certain things rough (strictly disallowing editing the enum values in the editor), this forgoes for-loops and acts as a stand in for Unity not having serializable maps and proper editor representations of them. Unless…