Referencing data from Scriptable Objects

Hello everyone, I’ve been working on an equipment system that uses scriptable objects to apply different stats to the player and need help with simplifying it if possible. Here is the script:

public class PlayerStats : CharacterStats
{

    EquipmentSlot _slot;
    void Start()
    {
        EquipmentManager.instance.onEquipmentChanged += OnEquipmentChanged;
    }

    void OnEquipmentChanged(Equipment newItem, Equipment oldItem)
    {
        _slot = newItem.equipSlot;
        if (newItem != null && _slot == EquipmentSlot.LeftHand)
        {
            poorArmorL.AddModifier(newItem.poorArmorModHand);
            goodArmorL.AddModifier(newItem.goodArmorModHand);
            greatArmorL.AddModifier(newItem.greatArmorModHand);
            poorPowerL.AddModifier(newItem.poorPowerModHand);
            goodPowerL.AddModifier(newItem.goodPowerModHand);
            greatPowerL.AddModifier(newItem.greatPowerModHand);
        }

        if (newItem != null && _slot == EquipmentSlot.RightHand)
        {
            poorArmorR.AddModifier(newItem.poorArmorModHand);
            goodArmorR.AddModifier(newItem.goodArmorModHand);
            greatArmorR.AddModifier(newItem.greatArmorModHand);
            poorPowerR.AddModifier(newItem.poorPowerModHand);
            goodPowerR.AddModifier(newItem.goodPowerModHand);
            greatPowerR.AddModifier(newItem.greatPowerModHand);
        }

        if (newItem != null && _slot == EquipmentSlot.Chestplate)
        {
            poorArmorC.AddModifier(newItem.poorArmorModChest);
            goodArmorC.AddModifier(newItem.goodArmorModChest);
            greatArmorC.AddModifier(newItem.greatArmorModChest);
        }

        if (oldItem != null)
        {
            poorArmorL.RemoveModifier(newItem.poorArmorModHand);
            goodArmorL.RemoveModifier(newItem.goodArmorModHand);
            greatArmorL.RemoveModifier(newItem.greatArmorModHand);
            poorPowerL.RemoveModifier(newItem.poorPowerModHand);
            goodPowerL.RemoveModifier(newItem.goodPowerModHand);
            greatPowerL.RemoveModifier(newItem.greatPowerModHand);

            poorArmorR.RemoveModifier(newItem.poorArmorModHand);
            goodArmorR.RemoveModifier(newItem.goodArmorModHand);
            greatArmorR.RemoveModifier(newItem.greatArmorModHand);
            poorPowerR.RemoveModifier(newItem.poorPowerModHand);
            goodPowerR.RemoveModifier(newItem.goodPowerModHand);
            greatPowerR.RemoveModifier(newItem.greatPowerModHand);

            poorArmorC.RemoveModifier(newItem.poorArmorModChest);
            goodArmorC.RemoveModifier(newItem.goodArmorModChest);
            greatArmorC.RemoveModifier(newItem.greatArmorModChest);
        }
    }
}

Currently, “EquipmentSlot” is referencing the enums (RightHand, LeftHand, Chestplate, Belt) of my Scriptable Object equipment. Long story short, this is sorting where the attack and defense values go for my other script to randomize the outcome.

And as you can see, it’s a repetitive mess to navigate in the inspector and it feels like there is a simpler way of doing this anyway. Any suggestions are welcome, but understand I am still well within the learning stage of unity coding and may need some clarification from time to time.

I haven’t played around with this sort of stuff before, but I have thought about it a bit (as my own project will need this eventually), so I’ve pondered on it some.

In your particular case, what you could look to do is encapsulate a slot into it’s own, serializable class (so it shows in the inspector). This can then contain the functionality for changing the equipped item in that slot, and adjusting stats as necessary. From there, you then have a variable in your player stats corresponding to each slot you want. Eg: private EquipmentSlot handEquipmentSlot;

How you get an item into the right slot probably depends on your UI for doing so. If your UI is the kind where you drag and drop an item into the slot, then the script component for that UI slot probably would be given a reference to the matching slot on the player. Dropping an item on it calls it swap item method, then adjusts player stats as necessary.

If it’s the kind where you right or double-click on an item to equip it, then perhaps you’ll need an enum on the item (which you do already), which gets checked in a switch-case statement to tell the item where it goes. I think in either case you’ll need an enum to denote what slot an item should go in.

Hopefully this gives you an idea to work on.

Yeah, I see what you’re saying. I’ll have to write that down because I need to finish a lot of steps to get there. As it stands, my inventory only changes by clicking on the slot and I still don’t have a working equipment tab yet. Thank you!