Instantiating class from selected Enum

Hi

I’m working on a turn-based game where you can steal other people’s powers and use them yourself once you defeat them. So far I can add these powers to my “Inventory” and it displays the correct power, but I can’t use them yet.
I’m using enums from a base class to select what power is in this instance of the class, it’s also used to get the sprite (another script) for inventory display. What I want to do is get a specific class based on the power you selected and display it in the inspector (inspector part is optional), each class for the specific power will contain the 3 abilities, damage, and string for displaying the name of each ability in the UI.

Any help would be appreciated.

you should not use classes, but instances of classes, aka objects.
but regardless you can correlate your enum with the data in question by using a dictionary.

using System.Collections.Generic;

public class SomeClass {

  Dictionary<PowersEnum, Power> _map;

  public void InitializePowersMap() {
    _map = new Dictionary<PowersEnum, Power>();
    _map[PowersEnum.MegaPower] = new MegaPower();
    _map[PowersEnum.CoronaPower] = new CoronaPower();
    _map[PowersEnum.FlowerPower] = new FlowerPower();
  }

  public Power GetPowerObject(PowersEnum power) {
    return _map[power];
  }

}

public enum PowersEnum {
  MegaPower,
  CoronaPower,
  FlowerPower
}

public abstract class Power { }

public class MegaPower : Power {
  public int someValue;
}

public class CoronaPower : Power {
  public bool lockdown;
}

public class FlowerPower : Power {
  public int someValue;
  public int flowers;
}

Would I be able to call methods within the power’s classes using this?

Well yes and no.

  1. If it’s a method that every Power should have, you declare it in that abstract Power class.
  2. If it’s a method that only some powers should have, you need to know which power it is exactly (which you do, based on the enum alone), and cast the general Power into some other specific thing, i.e. FlowerPower.
  3. If it’s a method that only a category of powers should have, you can use interfaces to distinguish one group from another.

Example for #1

public abstract class Power {

  public abstract void PowerMethod(int strength); // no method body, but has to be implemented

}

// now all derived classes must implement that method
public FlowerPower : Power {

  public override void PowerMethod(int strength) {
    crazyExplosionBoom(strength);
  }

}

Usage

SomeClass.GetPowerObject(PowersEnum.FlowerPower).PowerMethod(5);

Example for #2

public class MegaPower : Power {
  public int someValue;

  public void PowerMethod(int strength) {
    someValue = strength;
    crazyExplosionBoom(strength);
  }
}

// each class having their own methods
public class FlowerPower : Power {
  public int someValue;
  public int flowers;

  public void FlowerBurst() { }
}

Usage

var powerObj = SomeClass.GetPowerObject(powerType);
switch(powerObj) {
  case FlowerPower flowerPower: flowerPower.FlowerBurst(); break; // but now you have to check for a type for the compiler to know which method is where
  default: System.NotSupportedException();
}

Example for #3

// here you declare interfaces (start their names with I)
public interface IStandardPower { // you can have an empty one
}

public interface ISpecialPower { // or an interface that demands certain stuff
  void SpecialMethod(); // interfaces contain no body, just signatures
}

public class MegaPower : Power, IStandardPower {
  public int someValue;
}

// you can have more than one interface, but only one parent class
public class FlowerPower : Power, ISpecialPower {
  public int someValue; // this is still something that is known only to FlowerPower
  public int flowers;  // this as well

  public void SpecialMethod() { // here we satisfy that interface
  }
}

Usage

var powerObj = SomeClass.GetPowerObject(powerType);
if(powerObj is ISpecialPower specialPower) specialPower.SpecialMethod();
// now you have to check an object against a particular interface to know what it does

All three approaches can be combined into a powerful whole.

Thanks for all the effort Orion, I’ll give this a try.
Enjoy your day!

I’ve added comments in the meanwhile if you need better understanding of what’s what.