mercredi 30 décembre 2020

How can I maintain a list of objects that all inherit from base class, but access child methods after looking one up?

I am trying to figure out a way to maintain a list of objects that all inherit from a PlayerUpgradeData class and be able to access methods in their child classes.

In my game, given a string that corresponds with the property Name of one of these objects, I want to be able to look up the corresponding object either with LINQ or by dictionary key, and call a method Upgrade() in the child object. So, again, given a corresponding string I want to:

  • Lookup the object in a list, or dictionary.
  • Access the properties from PlayerUpgradeData.cs
  • Access the upgrade-specific Upgrade() method for the child class.

Here's a snippet of PlayerUpgradeData.cs:

public class PlayerUpgradeData : ScriptableObject
{
    public string Name;

    public string Description;
    
    public int Stacks;

    //More properties truncated...

    public virtual void Up()
    {
        Stacks++;
        UpdatePanel();
    }

    public virtual void Down()
    {
        Stacks--;
        UpdatePanel();
    }

    public void UpdatePanel()
    {
        //UI Update code that needs to be run each time this is upgraded.
    }

}

Here's a snippet of a given upgrade, PlayerUpgradeFanControl.cs:

public class PlayerUpgradeFanControl : PlayerUpgradeData
{
    //Data related to this upgrade

    public override void Up()
    {
        base.Up();

        //Logic for upgrading this item.
    }

    public override void Down()
    {
        base.Down();

        //Logic for downgrading this item
    }


}

And finally, an example of how I want to work this with data structure:

//Having a list of objects allows me to serialize it in my Unity Inspector
List<PlayerUpgradeData> UpgradeList = new List<PlayerUpgradeData>();

//Given a string, look up the object and run its upgrade logic.
UpgradeList.FirstOrDefault(x => x.Name == myString).SomehowRunChildUpgradeFunction();

A solution I've come up with is basically to create an interface property that all upgrades implement. Then, keep a list/dictionary of IPlayerUpgrade. Then when I look up, I'll certainly have access to the methods, but I no longer have access to the data within PlayerUpgradeData.

Hopefully, you see my issue... I'm storing a list of objects that all have their base class in common. But when I look up the corresponding object, I obviously can't cast it to a child object, so I have no way of accessing its upgrade logic.

I realize my data structure is inherently flawed, so I am hoping someone can explain to me a better way to structure this.

enter image description here

Aucun commentaire:

Enregistrer un commentaire