Inheritance best solution to handle *many* instances of a class with unique values?

This is related to https://answers.unity.com/questions/1608857/best-structure-for-a-perk-system.html - but since fundamentally it’s a separate question I’m making a separate post.

I have a game which makes use of “perks”, which are unique player bonuses that can be picked upon leveling up. Here’s a few examples:


  • Healthy - Increases your maximum life to 150.
  • Vigorous - Increases your maximum life to 200. (Requires “Healthy”)
  • Health Regeneration - While detected by an enemy, you regenerate 1 health per second.
  • Heavy Armor - You no longer take damage from enemy death explosions.
  • Health Specialist - Health pickups now give an additional 25% health, but armor pickups now give 50% less armor.

Thus I have a large amount of instances of a “Perk” class which currently looks like this:

public class Perk
 {
     public string name;
     public string description;
     public bool picked = false;
     public Perk requiredPerk;
     public List<int> values;
 }

A Perk always has a name, description, and picked status. Optionally it might also have a requiredPerk. Finally, each perk may or may not have a few unique values. As seen in the examples above, “Healthy” would have a unique int set to 50 (bonus health). “Health Specialist” would have two unique floats - 1.25 and 0.5 - which are the pickup multipliers, and so on. Currently my setup assumes it’s all ints, so I’d have to either store those values as percentages and convert, or have a separate List variable - both of which seems like clumsy solutions.

Now, at a first glance the obvious fix for this is to use inheritance. E.g.

public class Perk
{
    public string name;
    public string description;
    public bool active = false;
    public Perk requiredPerk;
}

public class PerkHealthSpecialist : Perk
{
    public float armorMultiplier;
    public float healthMultiplier;
}

But I’m not quite sure if this is the most logical approach. Most perks have some kind of value attached to them, so they would all pretty much be their own class with a single instance.

Is there a more recommended way to do this, or is inheritance the way to go?

A simple solution is just to create a generic class for perks with additional values which derives from Perk. Then instantiate it with desired type (or just use List<object> to store multiple types of values).

 public class PerkWithValues<T> : Perk{
      public List<T> values;
 }

But i must state that this question may not be completely suitible when it comes to unity. Because unity’s approach is more like component based. So it’s most likely to face some problems at some point which may look like violating OOP rules.

I could make further recommendations but it depends on whether you want to create predefined perks with predetermined values or just create random ones in runtime.