Code:Dictionary<string, int> Awards = new Dictionary<string, int>();//LoadDefaultConfig() Config["Awards"] = new Dictionary<string, int> { { "rifle.ak", 1 }, { "rocket.launcher", 1 }, { "metal.refined", 500 } };//Loaded() // does not help foreach (var element in Config["Awards"] as Dictionary<object, object>) Awards.Add(element.Key.ToString(), Convert.ToInt32(element.Value));// does not help foreach (var element in Config["Awards"] as Dictionary<string, int>) Awards.Add(element.Key, element.Value));
Solved Converting config in dictionary?
Discussion in 'Rust Development' started by SwipoStyle, Nov 11, 2015.
-
IMHO use Config.WriteObject and Config.ReadObject
You can just make your own class with dictionaries, custom classes and just about anything and dump/read it with these. Look at Cornucopia for an example of this. That's how I do all my configs now, no more converting involved, just load and save and use it like nothing happened. -
You can leave the key as which it actually is but you need to set object as value.
Thats how I'd do it:
Code:Config["Dictionary"] = new Dictionary<string, int> { {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5} };Dictionary<string, object> dic = Config["Dictionary"] as Dictionary<string, object>;foreach (string key in dic.Keys) { Puts(key + ": " + Convert.ToInt32(dic[key])); } -
Thank you very much!
I did, and I really like it ,it's a great experience for me. -
There are limits though! Several object structures have issues serializing with this. DateTime is an example, it won't serialize as-is so I convert it to a long when I need to serialize these. Same with Vectors and several others. I read about a work around somewhere on here this week though.
-
Pretty much any MonoBehaviour
