Context:
I am making a learning project in XNA. What I would like to be able to do is allow the game to be moddable by allowing the users to place arbitrary values into a JSON data file which is read into the game. The thing is that I want to avoid hardcoding if possible and have it automagically work for whatever the user throws at the engine.
Problem:
I have no idea how to design class(es) that can accommodate this sort of behavior, as my understanding is that whatever loads the JSON has to map it to existing variables on the object:
public class SomeGameEntity
{
public float Strength { get; set; }
public bool IsStrong { get; set; }
}
But what I want is for the client code to be able to do something like:
SomeGameEntity.Stats["Strength"] = 16;
SomeGameEntity.Stats["IsStrong"] = true;
My Solution:
Upon consideration, I would only define a few valid data types, likely float, bool, and string (other types, such as XNA.Color
, would probably have to be inferred from a combination of RGB floats on the object.)
Thus, what makes sense is to create a class such as the following:
public class EntityStatComponent
{
private readonly Dictionary<string, float> _floatValues;
private readonly Dictionary<string, string> _stringValues;
private readonly Dictionary<string, bool> _boolValues;
public float GetValue(string name) { }
public void SetValue(string name, float value) { }
public string GetString(string name) { }
public void SetString(string name, string value) { }
public bool GetFlag(string name) { }
public void SetFlag(string name, bool value) { }
}
But, this seems like a heavy-handed approach, especially with three dictionaries per active object. I would probably say there are less than 250 objects onscreen at a time (characters, projectiles, etc).
Unfortunately, I don't think generic typing is the way to go for this, since for T, it would have to accept a single data type into the dictionary. Alternatively, if I allowed any T value to be used via the get/set method, I would have to use the if(someArg is float)
stuff, which I understand to be frowned upon.
Am I overlooking something, or is there a better way to do it?