1. 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.
     
  2. 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)
    {
    //
    }
    }
    or
    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
  3. That's no help as it doesn't explain how to save a dictionary.

    Okay thanks.
     
  4. I added one more example to the post that you replied by the way ;x
     
  5. Actually it does:
    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
  6. 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();
                }
            }
    but I'm getting the error
    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
    Any ideas?
     
    Last edited by a moderator: Oct 17, 2015
  7. Try the second example that i gave u
     
  8. This seems to work, for reading.
    Code:
    void Loaded()
            {
                Config["Kills"] = kills;
                Config["Deaths"] = deaths;
            }
     
  9. I'm trying to save the config. The 2nd one you posted was for reading it.
     
  10. SaveConfig(); ?
     
  11. I mean save the dictionary to the config
     
  12. I cant get you... you cant "save" a dictionary. You can simply save the config.
     
  13. 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();
            }
    With this code I'm getting the error
    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
     
  14. 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();
    Thats what i gave you.. simply add values to the dictionary then add the dictionary to the config
     
  15. 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'
     
  16. key.Value.ToString();
     
  17. 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
     
  18. could you show me your current state? At least your Config and Loaded method
     
  19. Config:
    Code:
    {
      "Deaths": {},
      "Kills": {}
    }
    Loaded method:
    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()));
                }
            }