Right now, my game has characters in it, and the characters have a component called StatDictionary. In this StatDictionary, I have a few declarations:
public float basespellPower, baseDRFlat, baseDRPercent, baseSpeed;
public enum stat {SpellPower, DRFlat, DRPercent, Speed}
Dictionary<stat, float> statMap = new Dictionary<stat, float>();
This allows me to create effects that have a drop-down enumeration, and link it to my stat dictionary without having to know what stat I’m using beforehand - and, if I ever add more stats, I just add to the enumeration and dictionary initialization.
However, it seems that when I initialize the dictionary, it does not finish populating it before moving on - so if I try to access it shortly after telling it to initialize, it doesn’t finish in time and gives me a KeyNotFoundError.
Is there any way to wait until the dictionary’s fully initialized before moving on, or am I using the wrong tool for this?
I initialize it using its Start(), as it is a monobehaviour:
public float basespellPower, baseDRFlat, baseDRPercent, baseSpeed;
public enum stat {SpellPower, DRFlat, DRPercent, Speed}
Dictionary<stat, float> statMap = new Dictionary<stat, float>();
[SerializeField];
// Use this for initialization
void Start() {
statMap [stat.SpellPower] = basespellPower;
statMap [stat.DRFlat] = baseDRFlat;
statMap [stat.DRPercent] = baseDRPercent;
statMap [stat.Speed] = baseSpeed;
}
This is called the instant a character is created, and they are created in this class:
public class EncounterData : MonoBehaviour {
Hm, my partner wrote this, so maybe this is the source of the issue, so let me paste out their logic.
for (int i = 0; i < monsterPos.Count; i++)
{
GameObject monster = Instantiate(monsterPrefabs*, GameObject.Find("BattleGridController").transform.FindChild("MapData").FindChild("CreatureData"));*
gridController.GetComponent().AddObject((int)monsterPos_.x, (int)monsterPos*.y, monster); gridController.GetComponent().objectList.Add(monster); } After all the Instantiate(monsterPrefabs), the encounter is loaded, and controller.GetComponent().StartTurnSystem(gridController.GetComponent().objectList); is called, which then fetches stats (specifically speed) for the purposes of turn order. actor.GetComponent ().timeTilAction = actor.GetComponent ().charStats.GetStat(StatDictionary.stat.Speed);* This line throws the NullReferenceException, as StatDictionary.stat.Speed is not a key in the stat dictionary. But, if I wait about a second to call this function (with a Coroutine-mandated wait yield), it works perfectly. What part of this is not quick enough?_