I am making a system in my game where a modder will be to add a “Action” to the game with a Add method. I haven’t added the Add method in yet thought, because I have ran into a problem. Every “Action” is inside of an XML file with configurable settings. This works fine if I wasn’t allow modding, but since I have to do [XmlInclude(typeof(TYPEHERE))], it doesn’t work with modding. The reason I need these lines is because the XmlSerializer class doesn’t work without them because of the inheritance going on.
So basically, how would I do [XmlInclude(typeof(EveryChildOfActionClass))]
Here is my code, so you know what I mean:
[XmlInclude(typeof(Throw))]
public class Action {
[XmlAttribute("name")]
public string name;
public Action() { } //Blank constructor
public Action(string name) {
//Set up the action's settings
this.name = name;
}
public string GetName() { //Returns name of the action
return name;
}
}
This is what I mean. At the very top, you see XmlInclude, and then a specific type. Well, if I am allowing modder to create their own classes, I wont know every single type that is in the game. So how do I add multiple XmlInclude lines, and at the same time have them not be specifically typed. If I need to explain, please let me know.
Here is some more code:
public class Throw : Action {
[XmlElement("throw_power")]
public float throwPower;
public Throw() { } //Blank constructor
public Throw(string name, float throwPower) : base(name) {
//Set up the action's settings
this.name = name;
this.throwPower = throwPower;
}
public float GetThrowPower() { //Returns name of the action
return throwPower;
}
}
Notice that this is how an “Action” gets created. A class just inherits from the Action class. If modders create their own class, it will be impossible to know what it is called for the XmlInclude line.
[XmlRoot("action_pool")]
public class ActionPool {
[XmlArray("actions")]
[XmlArrayItem("action")]
public List<Action> actions = new List<Action>();
public void AddDefaultActions() {
actions.Add(new Throw("Throw", PlayerManager.Instance.settings.interactSettings.throwPower));
}
}
And this is what the XML file looks like:
<action_pool xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<actions>
<action xsi:type="Throw" name="Throw">
<throw_power>20</throw_power>
</action>
</actions>
</action_pool>