Different approach to an upgrade system

Ok, so I’ve recently given up trying to make this flexible and just hard coded what i needed. However some insight on how I could have done this would be great.

Basically i wanted to create an array of ‘upgrades’ that i could randomly choose from to show up in the upgrade menu.100859-capture.png

The issue was creating a way to store methods or at least references to methods.

I wanted an array of classses which held a:
string (name of the upgrade)
float (value/power of the upgrade)
method (a reference to the appropriate method to carry out the upgrade)

The idea was to choose randomly among the array, instantiate a button, change its text to the name and power of the button and set the buttons listener to the method in the class.

Looked around at unity events and delegates, but didn’t get anywhere.

insight on anything i’m missing or should be looking at would be appreciated, thanks.

Never really tried anything like this but you want to store the methods for a upgrade?

Why not just make a new class, doesn’t need monobehaviour?

Then just reference that script from your calling script to upgrade, e.g:

`public class Methods {

public string U_name = “”;
public float U_power = 0.0f;

//I think you can use the “override” for this
public void Upgrade(string name, float power)
{
U_name = name;
U_power = power;

//effects here
}

}`

That allows multiple upgrades and manipulation for indevidual ones. For effects simply just add it in - in your calling script for said upgrade but I think you’d have to make the void overrideable.

No idea if this’ll work or if this is what you needed but hope this helps in anyway. @Paricus

Ok, so first, you need to link your updates, how it affects the object.
It basically looks like:

public class BaseClass:
{
    public int baseValue;
    
    public virtual int Value
    {
    get{ return baseValue;}
    }
}

public class ModifiableClass:
{
    public int modifiableValue;
    
    public virtual int Value
    {
    get{ return base.Value + modifiableValue;}
    }
}

However, you need to know, yes, I want to upgrade the power, so, an enum is nice:

 public enum BaseTypes
    {
    Power,
    Health,
    Defense,
    }

public class BaseClass:
 {
     public int baseValue;
     public BaseTypes baseType;
     
     public virtual int Value
     {
     get{ return baseValue;}
     }
 }

But having problems with the modifier here, it seems to only give you one upgrade, and only plus.

public enum ModifierTypes
{
    Add,
    Percent,
}

 public class ModifierClass:
 {
     public int value;
     public ModifierTypes type;
 }

 public class ModifiableClass:
    {
        public int modifiableValue;
        public List<ModifierClass> modifiers;
        
        public virtual int Value
        {
        get{ return base.Value + modifiableValue;}
        }

    // Here, you can easily calculate modifiableValue based on modifiers
    // However, regardless of what you do, keep your order fixed (multiplication before or preceding).       
    }

Basically, it has allowed you to upgrade a stat. I hope you like it.

Let’s suppose your base calss is called “Player” and Powerup class is “Powerup” with one virtual method wich accept Player object. Player has a bunch of properties that can be modified. Now Powerup class has as many derived classes as you need for each individual powerup with ovverrided method to modify player object, you can even make Powerup class a scriptable object so you can store it as asssets (like hp boost with different ampunts)…

thanks for all the answers. They’ve essentially forced me to look up stuff I’ve been avoiding :slight_smile:

I rewrote my code to be some what more flexible:

    Upgrades u;
    public enum MethodEnum { bps, bpc, cost, pDur, pBoost };
    delegate void MethodDel(float val);

    [Header ("Upgrade (%)")]
    public string uName;
    public float uPower;
    public MethodEnum uMethod;
    MethodDel method;

    [Space(10)]
    public Text buttonText;

    private void Start()
    {
        u = Upgrades.upgrades;
        buttonText.text = uName + "

" + (uPower >= 0 ? “+” : “”) + uPower + “%”;
GetMethod();
}

    public void GetMethod()
    {
        switch (uMethod)
        {
            case MethodEnum.bps:
                method = BPS;
                break;
            case MethodEnum.bpc:
                method = BPC;
                break;
            case MethodEnum.cost:
                method = Cost;
                break;
            case MethodEnum.pDur:
                method = PanicDuration;
                break;
            case MethodEnum.pBoost:
                method = PanicBoost;
                break;
        }
    }

    public void Upgrade()
    {
        method(uPower / 100);
    }

    public void BPS(float val)
    {
        u.bpsM += val;
    }

This way I can choose which upgrade the button will trigger using an enum (thank you @haruna9x )
The only real draw back however is having to make a method for each upgrade and reference it in a switch statement.
Works for this scale, but i’d imagine it could get annoying the more values you want to upgrade.

Sorry if i’m completely missing something from your answer. Not that amazing of a programmer just yet. Thanks anyway.