I get this message as soon as the OnServerCommand hook is triggered:This is the only code that's in the hook function:Code:Failed to call hook 'OnServerCommand' on plugin 'CustomHelp v1.0.0' (InvalidCastException: Value is not a convertible object: System.Collections.Generic.List`1[System.Object] to System.String[])
And this is the code I use for the config for "Commands":Code:Dictionary<string, string[]> dict = Config.Get<Dictionary<string, string[]>>("Commands");
Code:Config["Commands"] = new Dictionary<string, string[]>() { {"wipe", new string[]{ "The next wipe is gonna be on 2/2" } }, {"kit", new string[] {"There's not gonna be kits or tp on this server.", "We want the server to feel vanilla!"} } };
Any help is appreciated!
Thanks!
~K
InvalidCastException when trying to retrieve my dictionary from the config
Discussion in 'Rust Development' started by Kvixen, Feb 1, 2017.
-
Bump..
-
Try casting as a Dictionary<string, object>() instead.
-
How would I go about using that Object as an string[] later? I'm trying it out right now but don't seem like I get any progress.
-
Code:var dict = new Dictionary<string, object>(); var dict2 = new Dictionary<string, string[]>(); foreach(var kvp in dict) { dict2[kvp.Key] = (string[])kvp.Value; }
(and of course if the object inside the first dictionary isn't a string[], it will fail to cast it again) -
Code:
private Dictionary<string, string[]> dict;
Code:void Loaded() { foreach(var obj in (Dictionary<string, object>)Config["Commands"]) { dict[obj.Key] = (string[])obj.Value; } }
This generates a:
Code:Failed to initialize plugin 'CustomHelp v1.0.0' (InvalidCastException: Cannot cast from source type to destination type.)
-
Code:private Dictionary<string, string[]> dict = new Dictionary <string, string[]>();
EDIT: the init would fail without defining a length for the array, like string[9] for example.
Cause of that you dont know it before, using this instead of object type is mainly not useful here or very hard to handle. -