Selecting a variable to modify from a ScriptableObject?

Hey, I have a system that allows the player to collect items to improve their stats. Each item is a ScriptableObject with a couple of fields: name, description, and icon. I also want to be able to choose what the item does when creating the ScriptableObject.

So on the ScriptableObject, I have a couple of options. The first one is what formula it does. For this I just have an enum with add, subtract, multiply, and divide as options. Then I have a float for the modifier (so I would do 50 if I was going to add 50 health).

My issue is with the last one. I need to be able to select what attribute I want to modify while editing the ScriptableObject.
If I have like 15 variables I want to modify, I don’t want to do this:

switch (item.name)
            {
                case "Item 1":
                    maxHealth = maxHealth + 50;
                    break;
                case "Item 2":
                    movementSpeed = movementSpeed + 10;
                    break;
                case "Item 3":
                    jumpForce = jumpForce + 10;
                    break;

                //And so on
            }

Ideally, I’d like to be able to do something like this (where I would be able to define what variable I want to modify from the ScriptableObject)

switch (item.function)
            {
                case Item.Formulas.add:
                    variable = variable + item.modifier;
                    break;
                case Item.Formulas.subtract:
                   variable = variable - item.modifier
                    break;
                case Item.Formulas.multiply:
                   variable = variable * item.modifier
                    break;
                case Item.Formulas.divide:
                   variable = variable / item.modifier
                    break;
            }

This way whenever I make a new item, I don’t have to go into the script to add a couple of extra lines for it, it just works right away.

The issue is that I don’t know the best way about doing this. I could create a string on the ScriptableObject where I type the name of the variable I want to modify, then just refer to them by their name in the script. I’d rather not do this, it feels kinda messy referring to things by their name instead of another option.

Are there any other ways I could do this?

You can aways create a function, and call that function with the parameter you want. For example:

//Here you have your scriptable object base Item.
public class Item : ScriptableObject
{
    public string name;
    public Sprite icon;
    public string description;

    public virtual float Add(float value)
    {
        return value;
    }
    public virtual float Substract(float value)
    {
        return value;
    }
}

//You create child classes of item, so you can create custom behaviours.
public class HealthPotion : Item
{
    public float AddHp = 50f;

    public override float Add(float value)
    {
        value += AddHp;
        return value;
    }
    public override float Substract(float value)
    {
        value -= AddHp;
        return value;
    }
}

public class MaxDamage : Item
{
    public float AddDamage = 3f;

    public override float Add(float value)
    {
        value += AddDamage;
        return value;
    }
}

And then in your player class:
public class Player : MonoBehaviour
{
//I will assume you have an inventory.
public List inventory = new List();

    private float playerHp = 50f;
    private float maxDamage = 10f;
    private void Awake()
    {
        //I can add a health option type since its child of Item.
        inventory.Add(new HealthPotion());
        inventory.Add(new MaxDamage());
    }

    private void ConsumeItem()
    {
        //For example purposes
        int selectedSlot = 0;
        playerHp = inventory[selectedSlot].Add(playerHp);
        playerHp = inventory[selectedSlot].Substract(playerHp);
        selectedSlot = 1;
        maxDamage = inventory[selectedSlot].Add(maxDamage);
    }
}

Feel free to ask if you don’t understand something. :stuck_out_tongue: