C# -- Populate dictionary automatically (not manually, yuck)

The title is vague cause I’m not 100% I’m doing this in a decent way. So I’m creating modular ability system in my game where the player can swap out any of their 4 skills/abilities from a list of … a lot. Here’s roughly what we’re doing:

// One of many small classes in ability file

class SpecificAbility : IAbility{
    
    // ability stuff like an Execute() method for starting specific ability functions.
}    

// Different file

class FooStuff {
    
    public Dictionary<string, IAbility> AbilityDict = new Dictionary<string, IAbility>();
}

    void UpdateAbilityDict(){
    
        AbilityDict.Add("SpecificAbility", new SpecificAbility());
    }

So, this works, and then we can easily toss abilities around allowing for the player to modify what they’re using. Cool, great. However, we currently have 16 abilities, and are planning for up to 80. Now, I do NOT want to type AbilityDict.Add() 80 times, and I’m sure I don’t need to explain why this is just bad practice. I come from Python, so dynamically loading each ability into this dictionary is childsplay where I come from. C#? I’ve got no idea, lol. I’m not even sure what the tactic is even called to do this so google as been less than helpful. Halp! Thanks guys. :slight_smile:

Your answer is Reflection :wink:

This is one of the cases where it’s ok to use it since there’s no other way around that. If you’re happy with using the classname as dictionary key that’s quite easy. If you want to specify a seperate name you would have to add a custom Attribute to your class to give it a custom name.

Those two helper methods will give you either a list of all types in your current AppDomain or all classes which are assignable to a specified type which could be a base class or an interface.

public static class ClassUtils
{
    public static IEnumerable<System.Type> AllTypes()
    {
        var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
        foreach(var assembly in assemblies)
        {
            var types = assembly.GetTypes();
            foreach(var type in types)
            {
                yield return type;
            }
        }
    }
    public static IEnumerable<System.Type> AllTypesDerivedFrom(System.Type aBaseType)
    {
        foreach(var T in AllTypes())
        {
            if(aBaseType.IsAssignableFrom(T) && T != aBaseType)
                yield return T;
        }
    }
    public static T GetFirstAttribute<T>(this System.Type aType) where T : System.Attribute
    {
        var attributes = aType.GetCustomAttributes(typeof(T),false);
        if(attributes.Length == 0)
            return null;
        return (T)attributes[0];
    }
}

Now you just need to define a custom attribute like this:

public class CustomAbilityName : System.Attribute
{
    public string customName;
    public CustomAbilityName(string aCustomName)
    {
        customName = aCustomName;
    }
}

And this is how one of your child classes could look like:

[CustomAbilityName("Fireball")]
class SpecificAbility : IAbility{
    // ...
} 

Your initialization of your dict would look like:

var classes = ClassUtils.AllTypesDerivedFrom(typeof(IAbility));
foreach(var T in classes)
{
    IAbility inst = (IAbility)System.Activator.CreateInstance(T);
    string name = T.Name;
    var att = T.GetFirstAttribute<CustomAbilityName>();
    if (att != null)
        name = att.customName;
    AbilityDict.Add(name, inst);
}

Note: using that attribute is pure optional. If no attribute specified it would use the classname.

Your answer here is XML serilization.

What you can do (once you understand how XML serialization works) is create your XML file that lists all your abilities and all the properties pertaining to them. Modify your ability class to have abilities include a ID (which can just come from the class name if need be, but I’m assuming in some cases you can have the same class serve the purpose of a fireball and an icebolt but the dmg type property will be different along with other properties).

class SpecificAbility : IAbility
{
  [XMLElement("AbilityID")]
  public string ID;
// ability stuff like an Execute() method for starting specific ability functions.
} 

Then read in (deserialize in this case)the XML as a container like the following:

public class AbilityContainer
    {
        [XMLElement("Ability")]
        List<IAbility> Abilities = new List<IAbility>();
    }

Then you can just iterate through the list of abilities and add them to a dictionary with the ID as their key.