Traits not working

Hello, the traits are not working when I click on Confirm Allocation, the UI works but not the additive or percent bonus. So far I doubled checked the code, I doubled checked the component on the player to make sure I have the correct elements, and I doubled checked the trait store to make sure the trait row ui has the correct trait. I included the code and screen shots.

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

namespace RPG.Stats
{
    public class TraitStore : MonoBehaviour, IModifierProvider, ISaveable
    {
        [SerializeField] TraitBonus[] bonusConfig;
        [System.Serializable] class TraitBonus
        {
            public Trait trait;
            public Stat stat;
            public float additiveBonusPerPoint = 0;
            public float percentageBonusPerPoint = 0;
        }

        Dictionary<Trait, int> assignedPoints = new Dictionary<Trait, int>();
        Dictionary<Trait, int> stagedPoints = new Dictionary<Trait, int>();

        Dictionary<Stat, Dictionary<Trait, float>> additiveBonusCache;
        Dictionary<Stat, Dictionary<Trait, float>> percentageBonusCache;

        private void Awake()
        {
            additiveBonusCache = new Dictionary<Stat, Dictionary<Trait, float>>();
            percentageBonusCache = new Dictionary<Stat, Dictionary<Trait, float>>();
            foreach(var bonus in bonusConfig)
            {
                if (!additiveBonusCache.ContainsKey(bonus.stat))
                {
                    additiveBonusCache[bonus.stat] = new Dictionary<Trait, float>();
                }
                if (!percentageBonusCache.ContainsKey(bonus.stat))
                {
                    percentageBonusCache[bonus.stat] = new Dictionary<Trait, float>();
                }
                additiveBonusCache[bonus.stat][bonus.trait] = bonus.additiveBonusPerPoint;
                percentageBonusCache[bonus.stat][bonus.trait] = bonus.percentageBonusPerPoint;
            }
        }

        public int GetProposedPoints(Trait trait)
        {
            return GetPoints(trait) + GetStagedPoints(trait);
        }

        public int GetPoints(Trait trait)
        {
            return assignedPoints.ContainsKey(trait) ? assignedPoints[trait] : 0;
        }

        public int GetStagedPoints(Trait trait)
        {
            return stagedPoints.ContainsKey(trait) ? stagedPoints[trait] : 0;
        }

        public void AssignedPoints(Trait trait, int points)
        {
            if (!CanAssignPoints(trait, points)) return;

            stagedPoints[trait] = GetStagedPoints(trait) + points;
        }

        public bool CanAssignPoints(Trait trait, int points)
        {
            if (GetStagedPoints(trait) + points < 0) return false;
            if (GetUnassignedPoints() < points) return false;
            return true;
        }

        public int GetUnassignedPoints()
        {
            return GetAssignablePoints() - GetTotalProposedPoints();
        }

        public int GetTotalProposedPoints()
        {
            int total = 0;
            foreach(int points in assignedPoints.Values)
            {
                total += points;
            }

            foreach (int points in stagedPoints.Values)
            {
                total += points;
            }
            return total;
        }

        public void Commit()
        {
            foreach(Trait trait in stagedPoints.Keys)
            {
                assignedPoints[trait] = GetProposedPoints(trait);
            }
            stagedPoints.Clear();
        }

        public int GetAssignablePoints()
        {
            return (int)GetComponent<BaseStats>().GetStat(Stat.TotalTraitPoints);
        }

        public IEnumerable<float> GetAdditiveModifiers(Stat stat)
        {
            if (!additiveBonusCache.ContainsKey(stat)) yield break;

            foreach(Trait trait in additiveBonusCache[stat].Keys)
            {
                float bonus = additiveBonusCache[stat][trait];
                yield return bonus * GetPoints(trait);
            }
        }

        public IEnumerable<float> GetPercentageModifiers(Stat stat)
        {
            if (!percentageBonusCache.ContainsKey(stat)) yield break;

            foreach (Trait trait in percentageBonusCache[stat].Keys)
            {
                float bonus = percentageBonusCache[stat][trait];
                yield return bonus * GetPoints(trait);
            }
        }

        public object CaptureState()
        {
            return assignedPoints;
        }

        public void RestoreState(object state)
        {
            assignedPoints = new Dictionary<Trait, int>((IDictionary<Trait, int>)state);
        }
    }
}


Thank you,

Eric

Copied from response to this question on Udemy:

Let’s add some Debug.Logs to the Awake() to make sure that we’re getting our setup initialized properly:

 private void Awake()
{
    additiveBonusCache = new Dictionary<Stat, Dictionary<Trait, float>>();
    percentageBonusCache = new Dictionary<Stat, Dictionary<Trait, float>>();
    foreach(var bonus in bonusConfig)
    {
        Debug.Log($"Processing Bonus {bonus.stat}/{bonus.trait} +{bonus.additiveBonusPerPoint} +{bonus.perrcentageBonusPerPoint}%");
        if (!additiveBonusCache.ContainsKey(bonus.stat))
        {
            Debug.Log($"Creating Additive lookup for {bonus.stat}");             
            additiveBonusCache[bonus.stat] = new Dictionary<Trait, float>();
        }
        if (!percentageBonusCache.ContainsKey(bonus.stat))
        {
             Debug.Log($"Creating Percentage lookup for {bonus.stat}");                
             percentageBonusCache[bonus.stat] = new Dictionary<Trait, float>();
        }
        additiveBonusCache[bonus.stat][bonus.trait] = bonus.additiveBonusPerPoint;
        percentageBonusCache[bonus.stat][bonus.trait] = bonus.percentageBonusPerPoint;
        Debug.Log($"Setting Additive [{bonus.stat}][{bonus.trait}] to {additiveBonusCache[bonus.stat][bonus.trait]}";
        Debug.Log($"Setting Percentage [{bonus.stat}][{bonus.trait}] to {percentageBonusCache[bonus.stat][bonus.trait]}";
    }
}

0 upvotes
Message

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

Privacy & Terms