1

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?

4

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?

  • Do you happen to have a resource where I can learn about dynamic modules and embedded scripting languages? Both sound useful for different purposes. Is it unusual for a very sophisticated game to use both, or does convention usually call for one approach over the other? – Kyle Baran May 22 '14 at 1:22
  • Check this article for dynamic module loading and this article for using LUA as an embedded scripting language. Whenever it's worthwhile to use them both depends not on how sophisticated your game is, but on how sophisticated the mods will be. A very complex mod(one that can be treated as a new game - like Counter Strike) might benefit from using a module for the core parts and scripts for defining special behaviors of levels, characters and items. – Idan Arye May 22 '14 at 1:32
  • Alright, you have gone above and beyond in answering my question. Have a checkmark, with gusto! – Kyle Baran May 22 '14 at 1:47
  • It might be worth mentioning MEF for .NET dynamic module loading. msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx – Shelby115 Jun 18 '15 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.