I don't feel comfortable using multiple for-loops

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…

Welp, after a bunch of refactors, I had come up with this chunky code.

This now can be used as

public float this[in CharacterClass characterClass, in StatType statType, in int level] =>
    progressionMatrix[characterClass, statType, level];

public float this[in StatType statType, in int level] =>
     progressionMatrix[characterClass, statType, level];

public float DefaultHp => this[StatType.Health, level];

public float XpReward => this[StatType.Experience, level];

You should also be able to write that as progressionMatrix[i][j][k] or progressionMatrix[i, j][k] or progressionMatrix[i][j, k], accessing the respective CharacterProgression, StatProgression, or the individual levels freely, as might be needed in loops.

I felt a promise in Sam’s next video. I’ve switched over to C# lookup Dictionaries with custom indexers for ProgressionMatrix.

I lost the progressionMatrix[i][j, k] representation and asserts for ensuring proper setup, but it’s otherwise all good in the hood.

Privacy & Terms