Is using sub-classes for ability unlocks a good idea?

I’m making my first game and I try to learn as I go, but i wish to get the best workflow so I don’t struggle too much on the future.
Right now i’m trying to write a script to purchase upgrades so the player can unlock different abilities or other stuff, and I thought about building a game manager script to encapsulate all of these possible upgrades in a single class called “unlockedUpgrade”. The code is bare bones, but it goes something like this:

public class unlockedUpgrade
{
    public string nameOfUnlock;
    public int levelToUnlock;
    public bool canBeUnlocked;
    public int neededPixels;
    public bool isUnlocked;

    public unlockedUpgrade(string name, int lvl, bool canBe, int pix, bool isUnlo)
    {
        // what is the name of the upgrade?
        name = nameOfUnlock; 
        //Which level does the player have to be to unlock this upgrade?
        levelToUnlock = lvl;
        // does the player meet the requirements to unlock this upgrade?
        canBeUnlocked = canBe;
        // how many currency (points, gold, coins, etc) does the player need to unlock this upgrade?
        neededPixels = pix;
        // is this upgrade already unlocked or purchased?
        isUnlocked = isUnlo;
    }
    // Example for one of these.
    // Upgrade is defined as 'firebullet' but the string names it as 'Bullet'
    // players need to be at level one to unlock
    // upgrade is available to purchase
    // player needs 10 points to unlock
    // upgrade is not yet unlocked.
    public unlockedUpgrade firebullet = new unlockedUpgrade("Bullet", 1, true, 10, false);
}

my plan is to define these sub classes to easily organize the conditions and parameters for a bunch of different abilities. is this a good way to do it?

Subclassing is definitely one of the more straight-forward ways to go about it (though there isn’t any going on in your example). This would probably be along the lines of the pattern subclass-sandbox: Subclass Sandbox · Behavioral Patterns · Game Programming Patterns

Should give you some food for thought.

That said don’t just have a vague ‘game manager’ script and hard code all these. Develop a method to author these in the inspector, such as with scriptable object. Either the scriptable objects are the upgrade themselves, or they can be used to factory an upgrade instance. Then you can define collections, groups, etc of them in other scriptable objects or components for use at runtime.

Also I wouldn’t necessarily call it an ‘unlocked upgrade’, when it’s possible for it to not be unlocked. It should just be Upgrade and its IsUnlocked property is used to determine whether it is or isn’t.

In any case, there’s never one ‘best’ workflow. It depends on the project, and depends on the programmer/developer. You’ll learn what works best as you work on the project, and if it doesn’t, you can refactor it, potentially making a forum post if you need some direction.

2 Likes