Heyy.
So how would you save a dictionary to a config? (eg Dictionary<BasePlayer, int>)
And how would you read from it? As in, get the values from the config and add them to a Dictionary.
Thanks, PsychoTea.
Save and Read Dictionary To/From Config
Discussion in 'Rust Development' started by PsychoTea, Oct 17, 2015.
-
Read Oxide API page.
-
Code:
public Dictionary<BasePlayer, int> something = new Dictionary<BasePlayer, int>(); something.Add(target, 1); somtehing.Add(player, 2); Config["Lala", "Lalaaa"] = something;
Not sure about the below .. but you can test.Code:[ChatCommand("read")] void cmdTest(BasePlayer player, string cmd, string[] args) { foreach(KeyValuePair<BasePlayer, int> pair in something) { // } }
Code:[ChatCommand("read")] void cmdTest(BasePlayer player, string cmd, string[] args) { foreach(var dunno in Config) { Puts($"{dunno.Key} and {dunno.Value}"); } }
Last edited by a moderator: Oct 17, 2015 -
-
-
Code:protected override void LoadDefaultConfig() { PrintWarning("Creating a new configuration file."); Config.Clear(); Config["ShowJoinMessage"] = true; Config["ShowLeaveMessage"] = true; Config["JoinMessage"] = "Welcome to this server"; Config["LeaveMessage"] = "Goodbye"; SaveConfig(); }[ChatCommand("Test")] private void Test(BasePlayer player, string command, string[] args) { if ((bool) Config["ShowJoinMessage"]) Config["ShowJoinMessage"] = false; else Config["ShowJoinMessage"] = true; SaveConfig(); }
Last edited by a moderator: Oct 17, 2015 -
Hm, this is what I have for saving the dict(s)
Code:void Unloaded() { foreach (KeyValuePair<BasePlayer, int> kvp in kills) { Config["Kills", kvp.Key.ToString()] = kvp.Value.ToString(); } foreach (KeyValuePair<BasePlayer, int> kvp in deaths) { Config["Deaths", kvp.Key.ToString()] = kvp.Value.ToString(); } }
Code:[Oxide] 2:30 PM [Error] Failed to call hook 'Unloaded' on plugin 'KillStats v1.0.0' (InvalidCastException: Cannot cast from source type to destination type.) [Oxide] 2:30 PM [Debug] at Oxide.Core.Configuration.DynamicConfigFile.Set (System.Object[] pathAndTrailingValue) [0x00000] in <filename unknown>:0 at Oxide.Core.Configuration.DynamicConfigFile.set_Item (System.String keyLevel1, System.String keyLevel2, System.Object value) [0x00000] in <filename unknown>:0 at Oxide.Plugins.KillStats.Unloaded () [0x00000] in <filename unknown>:0 at Oxide.Plugins.KillStats.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0
Last edited by a moderator: Oct 17, 2015 -
-
Code:void Loaded() { Config["Kills"] = kills; Config["Deaths"] = deaths; }
-
-
SaveConfig(); ?
-
I mean save the dictionary to the config
-
-
I want to write the KeyValuePairs from the dictionaries to the config file.
For example (not working-code):
Code:void Unloaded() { foreach (KeyValuePair<BasePlayer, int> kvp in kills) { Config["Kills", (String)kvp.Key.ToString()] = (String)kvp.Value.ToString(); } foreach (KeyValuePair<BasePlayer, int> kvp in deaths) { Config["Deaths", (String)kvp.Key.ToString()] = (String)kvp.Value.ToString(); } SaveConfig(); }
Code:[Oxide] 2:50 PM [Error] Failed to call hook 'Unloaded' on plugin 'KillStats v1.0.0' (InvalidCastException: Cannot cast from source type to destination type.) [Oxide] 2:50 PM [Debug] at Oxide.Core.Configuration.DynamicConfigFile.Set (System.Object[] pathAndTrailingValue) [0x00000] in <filename unknown>:0 at Oxide.Core.Configuration.DynamicConfigFile.set_Item (System.String keyLevel1, System.String keyLevel2, System.Object value) [0x00000] in <filename unknown>:0 at Oxide.Plugins.KillStats.Unloaded () [0x00000] in <filename unknown>:0 at Oxide.Plugins.KillStats.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0
-
Code:
public Dictionary<BasePlayer, int> something =[URL='http://www.google.com/search?q=new+msdn.microsoft.com']new[/URL] Dictionary<BasePlayer, int>(); something.Add(target, 1); somtehing.Add(player, 2); Config["Lala", "Lalaaa"]= something; SaveConfig();
-
Ah okay, I misunderstood what you'd said in the first place :3
Anyway,
I'm using Int32.Parse(key.Value) for reading the config, and getting this error
Code:[Oxide] 3:12 PM [Error] KillStats.cs(42,74): error CS1503: Argument `#1' cannot convert `object' expression to type `string'
-
-
Now I'm getting this error :/
Code:[Oxide] 3:18 PM [Error] Failed to initialize plugin 'KillStats v1.0.0' (FormatException: Input string was not in the correct format) [Oxide] 3:18 PM [Debug] at System.Int32.Parse (System.String s) [0x00000] in <filename unknown>:0 at Oxide.Plugins.KillStats.Loaded () [0x00000] in <filename unknown>:0 at Oxide.Plugins.KillStats.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.HandleAddedToManager (Oxide.Core.Plugins.PluginManager manager) [0x00000] in <filename unknown>:0
-
-
Config:
Code:{ "Deaths": {}, "Kills": {} }
Code:void Loaded() { foreach (var kill in Config) { if (kill.Value == null) return; kills.Add(BasePlayer.Find(kill.Key), Int32.Parse(kill.Value.ToString())); } foreach (var death in Config) { if (death.Value == null) return; deaths.Add(BasePlayer.Find(death.Key), Int32.Parse(death.Value.ToString())); } }