So i feel like I’m going crazy (but most likely just missing a syntax i dont see)
I created a hat of health (similar to Sam) but when i equip it nothing happens. My health stays at 100. I have the " Should use modifiers" box checked on my player’s base stats in the the scene and in the prefab and also removed the old equipment.cs from my player and added the stats equipment. My scripts look basically identical at this point so not sure where im missing something.
Stats Equipment
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Inventories;
using RPG.Stats;
namespace RPG.Inventories
{
public class StatsEquipment : Equipment, IModifierProvider
{
IEnumerable<float> IModifierProvider.GetAdditiveModifier(Stat stat)
{
foreach (var slot in GetAllPopulatedSlots())
{
var item = GetItemInSlot(slot) as IModifierProvider;
if (item == null) continue;
foreach (float modifier in item.GetAdditiveModifier(stat))
{
yield return modifier;
}
}
}
IEnumerable<float> IModifierProvider.GetPercentageModifiers(Stat stat)
{
foreach (var slot in GetAllPopulatedSlots())
{
var item = GetItemInSlot(slot) as IModifierProvider;
if (item == null) continue;
foreach (float modifier in item.GetPercentageModifiers(stat))
{
yield return modifier;
}
}
}
}
}
StatsEquipableItem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Inventories;
using RPG.Stats;
namespace RPG.Inventories
{
[CreateAssetMenu(menuName = ("RPG/Inventory/Equipable Item"))]
public class StatsEquipableItem : EquipableItem
{
[SerializeField] Modifier[] additiveModifiers;
[SerializeField] Modifier[] percentageModifiers;
[System.Serializable]
struct Modifier
{
public Stat stat;
public float value;
}
public IEnumerable<float> GetAdditiveModifier(Stat stat)
{
foreach (var modifier in additiveModifiers)
{
if (modifier.stat == stat)
{
yield return modifier.value;
}
}
}
public IEnumerable<float> GetPercentageModifiers(Stat stat)
{
foreach (var modifier in percentageModifiers)
{
if (modifier.stat == stat)
{
yield return modifier.value;
}
}
}
}
}
Base Stats
using System;
using System.Collections;
using System.Collections.Generic;
using GameDevTV.Utils;
using UnityEngine;
namespace RPG.Stats
{
public class BaseStats : MonoBehaviour
{
[Range(1, 99)]
[SerializeField] int startingLevel = 1;
[SerializeField] CharacterClass characterClass;
[SerializeField] Progression progression = null;
[SerializeField] GameObject levelUpFX = null;
[SerializeField] bool shouldUseModifiers = false;
public event Action onLevelUp;
LazyValue<int> currentLevel;
Experience experience;
private void Awake()
{
experience = GetComponent<Experience>();
currentLevel = new LazyValue<int>(CalculateLevel);
}
private void Start()
{
currentLevel.ForceInit();
}
private void OnEnable()
{
if (experience != null)
{
//adding method to list onExperienceGained
experience.onExperienceGained += UpdateLevel;
}
}
private void OnDisable()
{
if (experience != null)
{
//adding method to list onExperienceGained
experience.onExperienceGained -= UpdateLevel;
}
}
private void UpdateLevel()
{
//if (currentLevel >= progression.MaxLevel(characterClass)) return;
int newLevel = CalculateLevel();
if(newLevel > currentLevel.value)
{
currentLevel.value = newLevel;
LevelUpEffect();
onLevelUp();
}
}
private void LevelUpEffect()
{
Instantiate(levelUpFX, transform);
}
private float GetPercentageModifier(Stat stat)
{
if (!shouldUseModifiers) return 0;
float total = 0;
foreach (IModifierProvider provider in GetComponents<IModifierProvider>())
{
foreach (float modifier in provider.GetPercentageModifiers(stat))
{
total += modifier;
}
}
return total;
}
private float GetBaseStat(Stat stat)
{
return progression.GetStat(stat, characterClass, GetLevel());
}
private float GetAdditiveModifier(Stat stat)
{
if (!shouldUseModifiers) return 0;
float total = 0;
foreach(IModifierProvider provider in GetComponents<IModifierProvider>())
{
foreach(float modifier in provider.GetAdditiveModifier(stat))
{
total += modifier;
}
}
return total;
}
public float GetStat(Stat stat)
{
return (GetBaseStat(stat) + GetAdditiveModifier(stat)) * (1 + GetPercentageModifier(stat) / 100);
}
public int GetLevel()
{
return currentLevel.value;
}
public int CalculateLevel()
{
Experience experience = GetComponent<Experience>();
if (experience == null) return startingLevel;
float currentXP = experience.GetPoints();
int penultimateLevel = progression.GetLevels(Stat.ExperienceToLevelUp, characterClass);
for (int level = 1; level < penultimateLevel; level ++)
{
float XPToLevelUp = progression.GetStat(Stat.ExperienceToLevelUp, characterClass, level);
if(XPToLevelUp > currentXP)
{
return level;
}
}
return penultimateLevel + 1;
}
}
}