Creating a progression lookup

so every time I try to do this task, building a lookup table scriptable object, my brain goes into complete melt down and tries to convince me that “This is not the path I’m looking for”. would someone be so kind as to check my code to see if I’ve missed something?

This progression table will store three different progression rates (good, fair, poor) for four stats (attacks/Round, fortSave, reflexSave, willSave).

using UnityEngine;
using RPG.Core;
using System.Collections.Generic;

public class StatProgression : ScriptableObject
{
    [SerializeField] StatProgressionRate[] statProgressionRates;

    Dictionary<string, Dictionary<string, float[]>> progressionLookup = new Dictionary<string, Dictionary<string, float[]>>();

    void BuildLookup()
    {
        if (progressionLookup != null) { return; }

        progressionLookup = new Dictionary<string, Dictionary<string, float[]>>();
        foreach (StatProgressionRate pRate in statProgressionRates)
        {
            var statLookup = new Dictionary<string, float[]>();
            foreach (CharacterStatProgression cStat in pRate.stats)
            {
                statLookup[cStat.stat.ToString()] = cStat.levels;
            }

            progressionLookup[pRate.rate.ToString()] = statLookup;
        }
    }

    [System.Serializable] class StatProgressionRate
    {
        public ProgressionRate rate;
        public CharacterStatProgression[] stats;
    }

    [System.Serializable] class CharacterStatProgression
    {
        public CharacterStat stat;
        public float[] levels;
    }
}

Thank you for your time!

@sampattuzzi
I completely missed this as it got buried under the flood of unity stuff.
Any ideas?

Question that might help clarify things. How do you want to be accessing the cache? What are the values you are looking up with?

Morning Sam,

First my appologies for taking so long to reply, I’ve been having some issues with my vision recently.

Now, as to your questions, I am using the D20 system as layed out in Dungeons & Dragons v3.5 for my stats and progression rates. As part of this system many character classes share the same progression rate for various stats. That is what I’m storing with this particular SO. As for accessing the information, I’m using the following function (which is essentially a copy of what you wrote for the course).

    public int GetStat(CharacterStat stat, ProgressionRate rate, int level)
    {
        BuildLookup();

        if (progressionLookup.ContainsKey(rate.ToString()))
        {
            if (progressionLookup[rate.ToString()].ContainsKey(stat.ToString()))
            {
                float[] levels = progressionLookup[rate.ToString()][stat.ToString()];
                if (level > levels.Length - 1)
                {
                    Debug.LogWarning("Level out of Range");
                    level = (int)levels[levels.Length - 1];
                }
                return (int)levels[level];
            }
            Debug.LogError("Stat Not Found");
            return 0;
        }
        Debug.LogError("Progression Rate Not Found");
        return 0;
    }

The issue isn’t so much a matter of functionality, the code works fine and I have in fact expanded the material stored in it to include stuff that only has one progression rate. The problem is that my brain simply wants to convince me that there is a better way to do this, one that requires less repetition to create, modify, and update the tables.

@sampattuzzi

Do you think a function like this could help?

        static T GetByLevel<T>(T[] values, int level)
        {
            if (values.Length == 0)
            {
                return default;
            }
            if (level > values.Length)
            {
                return values[values.Length - 1];
            }
            if (level <= 0)
            {
                return default;
            }
            return values[level - 1];
        }

I’m going to mark this solved since the scripts I have seem to be working fine. For those interested, here are the links to the scripts as of 4 Feb 2020.

SpellProgression.cs
StatProgression.cs
XPRewardProgression.cs

and here are the SOs created by them.
Spell Progression Tables
Stat Progression Tables
XP Reward Table

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms