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. 
2 Answers
2
Your answer is Reflection 
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.
Are you adding to this dictionary during game play, or are you setting it up once at the start, then just reading from it for the rest of the game?
– HabitablabaJust to be safe, I'd say both. I'd like to allow for in game updating if needed. Mainly it's a one time load though at the beginning.
– jtsmith1287would the dictionary string entry always be the name of the class?
– AnxoAs of now I'm still debating that part. It could be the user facing string, or just used internally. It was initially class name because I was attempting to use reflection, but turns out I don't understand it all that well and am unsure of its limitations and performance costs. For the sake of this answer let's just say it'll always be the class name. Thanks.
– jtsmith1287@Landern The issue with that is I'm still manually updating the list/xml/json etc. What I'm looking for, if it exists, is a way to pick up all the classes in a file or within a namespace, then populate the dictionary with that. There's likely going to be a lot of iterations on these abilities, so needing to manually update at every change is totally not worth it. EDIT: Re-reading your comment I'm understanding what you're saying. You're talking about what to do with the dictionary once it's populated with data. I'm talking about actually populating with the data to begin with.
– jtsmith1287