C# Dynamic Data – Creating Objects with User-Defined Variable Names

cdynamic-datajsonxna

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?

Best Answer

Loading values from JSON is only useful if at some point something uses these values. Since you want to allow modders to use "arbitrary" structures(which I assume means "their own structures"), it's clear that it's not your own code that will use the data from that JSON - it'll be the modders code.

Now, there are two ways for letting modders add their code to your game - dynamic module loading and embedded scripting language.

If you use dynamic module loading - which means the modders write C# code with an API you supply, compile it to DLL, and your game loads that DLL - you should use hard coded classes to represent the data structure. However, you will not be the one who defines these classes - the modders will define them in their modules and send them as generic parameters to your JSON loading method(which should use generic reflection to delegate it to a JSON library). That way you won't be imposing the structure on the modders, but they'll still be able to use a well defined static-typed structure.

If you use an embedded scripting language(like LUA or JavaScript), you should just use a string->object map. Yes, I know that string->object maps are considered a code smell in static-typed languages, but scripting languages are usually dynamic-typed. You are going to loose all static structure anyways when you pass the data to the scripting language, and you are not going to access it from C#, so why not save yourself the trouble and ditch that structure(only for the values from the JSON, of course) from the beginning?