-3

README first

  • I have multiple abilities, and each ability is of an AbilityCategory type.

  • Examples of AbilityCategory types are AbilityCategory_Magic, AbilityCategory_Physical, AbilityCategory_Ranged, AbilityCategory_Biological, and the list goes on...

  • AbilityCategories are classes derived from Ability.

  • Ability is a class that can only be instantiated an AbilityBaseData object is passed into its constructor.

  • I want to find way to link each ability to one AbilityBaseData in a Dictionary called defaultAbilityBaseDatabase.

  • Each ability may only be linked up to a maximum of 1 instance of AbilityBaseData in my defaultAbilityBaseDatabase.


-- AbilityBaseData.cs

[System.Serializable]
public class AbilityBaseData{
    [SerializableField] Sprite abilityImage;
}

My current code structure:

-- AbilityDatabase.cs

enum EAbilityCategory_Magic{
    Fireball,
    IceBolt,
    BlackMagic
};
enum EAbilityCategory_Physical{
    SwordLance,
    FearlessBlow,
    RagnarokRage
};
enum EAbilityCategory_Ranged{
    Snipe,
    RainOfArrows,
    CrossbowShot
};
enum EAbilityCategory_Biological{
    VirusUnleash,
    SuperBug
};
enum Eblabla{};
enum Eevenmore{};
... There are many more abilities ...
...

Dictionary<EAbilityCategory_Magic,AbilityBaseData> defaultMagicAbilityBaseDataDatabase;
Dictionary<EAbilityCategory_Physical,AbilityBaseData> defaultPhysicalAbilityBaseDataDatabase;
Dictionary<EAbilityCategory_Ranged,AbilityBaseData> defaultRangedAbilityBaseDataDatabase;
Dictionary<EAbilityCategory_Biological,AbilityBaseData> defaultBiologicalAbilityBaseDataDatabase;
... list keeps going on...

Code structure looking for:

-- AbilityDatabase.cs

enum class EAbility{}
enum EAbilityCategory_Magic : EAbility{
    Fireball,
    IceBolt,
    BlackMagic
};
enum EAbilityCategory_Physical : EAbility{
    SwordLance,
    FearlessBlow,
    RagnarokRage
};
enum EAbilityCategory_Ranged : EAbility{
    Snipe,
    RainOfArrows,
    CrossbowShot
};
enum EAbilityCategory_Biological : EAbility{
    VirusUnleash,
    SuperBug
};
... There are many more abilities ...
...

Dictionary<EAbility,AbilityBaseData> defaultAbilityBaseDataDatabase;

This is the workaround I tried to use after encountering this problem

What variable type should I use to store types of a non sealed class or interface?

In which could have been like this:

//I can remove all enumerations have such a clean, one-line code
Dictionary<typeof(Ability),AbilityBaseData> defaultAbilityBaseDataDatabase;

I cannot combine all abilities like this:

enum EAbility{
    Fireball,IceBolt,BlackMagic,SwordLance,FearlessBlow,RagnarokRage,Snipe,.....
};

because it will prevent me from doing this:

-- MagicAbilityFactory.cs

AbilityCategory_Magic Create(EAbilityCategory_Magic magicAbilityCategory, AbilityBaseData defaultAbilityBaseData){
    switch(magicAbilityCategory){
    case Fireball:
        return new AbilityCategory_Magic_Fireball(abilityBaseData,other parameters);
    case blabla:
    }
    return null;
}

This might be an option https://stackoverflow.com/posts/3460047/revisions but the problem is that I would rather find a clean solution which doesn't need me to add so many lines of code since I have thousands of abilities..

Community
  • 1
  • 1
Ryan A WE
  • 59
  • 1
  • 10
  • 4
    It is frowned upon to beg for rep like you continue to do. There are ways to get rep and including requests for upvotes in your question is not it. – ethorn10 Feb 07 '16 at 03:37

1 Answers1

1

This is not possible in C#. The beneficial trade-off from this decision is that enums are value types, and thus lightweight structs instead of classes. This reduces memory pressure on the GC for these common, very small objects. If you have an issue with this design decision on the part of the C# team then you should raise that with Microsoft.

Where you need additional functionality beyond that available through the static methods of the Enum class and Extension methods, simply declare your own custom class/struct and inheritance hierarchy from scratch.

From MSDN on Enumeration Types:

All enums are instances of the System.Enum type. You cannot derive new classes from System.Enum, but you can use its methods to discover information about and manipulate values in an enum instance.

From MSDN on the class System.Enum

enter image description here

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
  • Hi, I have clarified my previous post regarding an extremely similar issue. Could you kindly take a look here? http://stackoverflow.com/questions/35191235/what-variable-type-should-i-use-to-store-types-of-a-non-sealed-class-or-interfac – Ryan A WE Feb 07 '16 at 10:34