Another Race Condition

Somehow during this lesson I have another race condition occurring.


The cause seems to be where we set the following in Health:

float healthPoints = -1f;

I think this creates a race condition where during loading the health is trying to be accessed before the Health script sets it in start();

Any suggestions of how to fix this one would be welcome, besides the obvious making health positive which could cause other problems?

Hi,

We changed one of the lectures in which we switched to damage points but reverted back.
This could be an issue of part of them as this was an error i received as well and i thought i had missed a line of code.

@sampattuzzi This was the error i mentioned.

The solution for me was to go to the prefab and removed and readd the base stats script and the issue disappeared.

If you could first make a ZIP file of your project with this bug in it so we can take a look i would be grateful but also after this couldd you try the removal and readd of the basestats script on the prefab and see if it solves the issue.

Not sure if this is a race. Take a look at what index it is trying to look up in the progression. Seems like the level being requested doesn’t exist there. Could just be a configuration issue?

Later today I will zip my project and send it. How can I send it to you?

Then I’ll try the basestats readding and I will triple check my progression configuration and which level is being requested.

Now that I think about it, I am not sure how healthpoints = -1f would create an out of bounds condition. Any ideas on this, I don’t have the script in front of me so I am not sure. But changing health from -1f to 1f did solve the bug, at least in my brief test.

If you upload to google drive and post the link here but it might be idea to copy and paste the progression script using the angled brackets to create preformatted text (Its above the reply box :slight_smile: )

using UnityEngine;
using System.Collections.Generic;
using System;

namespace RPG.Stats {
    [CreateAssetMenu(fileName = "Progression", menuName = "Stats/NewProgression", order = 0)]
    public class Progression : ScriptableObject
    {
        [SerializeField] ProgressionCharacterClass[] characterClasses = null;

        Dictionary<CharacterClass, Dictionary<Stat, float[]>> lookupTable = null;

        public float GetStat(Stat stat, CharacterClass characterClass, int level)
        {
            BuildLookup();
            float[] levels = lookupTable[characterClass][stat];

            if (levels.Length < level)
            {
                return 0;
            }
            return levels[level-1];
        }

        public int GetLevels(Stat stat, CharacterClass characterClass)
        {
            BuildLookup();
            float[] levels = lookupTable[characterClass][stat];
            return levels.Length;
        }

        private void BuildLookup()
        {
            if (lookupTable != null) return;

            lookupTable = new Dictionary<CharacterClass, Dictionary<Stat, float[]>>();

            foreach (ProgressionCharacterClass progressionClass in characterClasses)
            {
                var statLookupTable = new Dictionary<Stat, float[]>();

                foreach (ProgressionStat progressionStat in progressionClass.stats)
                {
                    statLookupTable[progressionStat.stat] =  progressionStat.levels;
                }

                lookupTable[progressionClass.characterClass] = statLookupTable;
            }
        }

        [System.Serializable]
        class ProgressionCharacterClass
        {
            public CharacterClass characterClass;
            public ProgressionStat[] stats;
            //public float[] health;
        }

        [System.Serializable]
        class ProgressionStat
        {
            public Stat stat;
            public float[] levels;
        }
    }
}

So I added a print (stat) to to GetStat() in BaseStats. Results show it’s definitely health problem…

https://drive.google.com/open?id=1_LC2wW3DMXcCe0W9Yfp2Y2zZLpTs52U5

Here is the link to the .ZIP of the Project.

So I think this is where the race condition is occurring. The start() in Health is being called here before level has been calculated in BaseStats.

private void Start()
        {
            GetComponent<BaseStats>().onLevelUp += RegenerateHealth;
            if (healthPoints < 0)
            {
                print("The problem lies here");
                healthPoints = GetComponent<BaseStats>().GetStat(Stat.Health);
            }
        }

I confirmed this by changing Start() in BaseStats to Awake() and the error did not persist. Sorry if this is a lot of posts, let me know if I should consolidate these or something.

Hi,

We have released a couple of videos on where this issue occurs in the first instance.
You should be able to go back and patch in the changes as they shouldnt cause a problem with the flow.
If you are using source control then you could revert the project.

The edited and new lecture numbers are L131 and 132.

Please do let me know if you have any issues along the way. Either here or in the Q&A on Udemy :slight_smile:

1 Like

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

Privacy & Terms