Creating objects with user-defined variable names

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?

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?

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật