Hi everyone,
My name is Dan-Levi, i have a lot of experience in several programming languages, but not c#.
My mission: I am making a very simple plugin that is going to save and load player userID and a bool to/form config file.
This is what i got so far:
When i have a clean config file it works, the data is saved as json into the file. But if i reload the plugin it seems like it have problems fetching the config and casting it to an dictionary.Code:[ChatCommand("god")] void cmdGod(BasePlayer player) { if(!player.IsAdmin()) { NotAllowedAction(player); } else { godToggle = !godToggle; string state = (godToggle == true) ? "on" : "off" ; var data = new Dictionary<string,bool>(); if(bulkitConfig["gods"] != null) { data = bulkitConfig["gods"] as Dictionary<string, bool>; } data[player.userID.ToString()] = godToggle; bulkitConfig["gods"] = data; SendReply(player,"Godmode toggled "+state); SaveDataConfig(); } }
What happens:
I clear the config file and alt tabs to the game. Typing /god in chat says godmode is toggled.
I then reload the plugin and try to type /god again. Im getting this error:
Any idea why this happens? Thanks in advance. And thanks for making Oxide happen.Code:[Oxide] 7:19 AM [Error] Failed to call hook 'cmdGod' on plugin 'Bulkit' (NullReferenceException: Object reference not set to an instance of an object) [Oxide] 7:19 AM [Debug] at Oxide.Plugins.Bulkit.cmdGod (.BasePlayer player) [0x00000] in <filename unknown>:0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
[DOUBLEPOST=1428384255][/DOUBLEPOST]Further debugging shows thatreturns null.Code:var gods = bulkitConfig["gods"] as Dictionary<string,bool>;
What am i missing out?
[DOUBLEPOST=1428391541,1428383930][/DOUBLEPOST]It turns out i had missunderstood what config actually returns. It returns Dictionary<string,object>.
I made a simple method that grabs it
And use it like thisCode:Dictionary<string,object> GetBulkitConfig(string configStr) { var config = Interface.GetMod().DataFileSystem.GetDatafile("Bulkit_Config"); return config[configStr] as Dictionary<string,object>; }
Code:[ChatCommand("test")] void cmdTest(BasePlayer player) { LoadConfig(); var gods = GetBulkitConfig("gods"); Debug.Log(gods[player.userID.ToString()]); }
Fetching config as dictionary
Discussion in 'Rust Development' started by Murky, Apr 7, 2015.
-
Code:
[ChatCommand("god")] void cmdGod(BasePlayer player) { if(!player.IsAdmin()) { NotAllowedAction(player); } else { godToggle = !godToggle; string state = (godToggle == true) ? "on" : "off" ; Dictionary<string,object>() data; if(bulkitConfig["gods"] != null) { data = bulkitConfig["gods"] as Dictionary<string, object>; } data[player.userID.ToString()] = godToggle; bulkitConfig["gods"] = data; SendReply(player,"Godmode toggled "+state); SaveDataConfig(); } }
-
Thanks Reneb, yes i figured it out but thank you anyway.
I have an idea of making some admin tools for Rust Experimental that is somewhat different to all the others. -
as long as you don't make an all in one plugin ^^