I would like to know how to create an array in C#
EG.
List of some thing
{
Example 1,
Example 2,
etc
}
[DOUBLEPOST=1477259854][/DOUBLEPOST]so Far i have
Config["Help Command Content"] = new List<object> {
"--HELP--",
"Clans Help -",
"Some other plugin help - "
Using arrays in Config C#
Discussion in 'Rust Development' started by Serenity 3, Oct 23, 2016.
-
Well. Theres a few different types of arrays really:
We have a list:
Code:List<string> shoppingList = new List<string>(){{"apples"}, {"tomatoes"}, {"oranges"} };
This will simply make a list where you can select those items.
If you want to get those items you can use a system like:
Code:var itemone = shoppingList[0];
This will grab apples, as the index starts at 0 not 1.
Then we have a dictionary:
Code:Dictionary<int, string> shoppingList = new Dictionary<int, string>(){ {0, "apples"}, {1, "oranges"} };
This will create a dictionary, which can store a class if so you desire.
You can retrieve the values using the first item(so in our case it will be the int as it was specified first.
Code:var itemselected = shoppingList[1];
this will output oranges, as you selected 1 instead of 0 which would get you apples(this is a bit different then a list.
You can retrieve all the items from a list using:
Code:for(var i = 0; i < shoppingList.Count(); i++){return shoppingList[i]; }
This will output:
apples:
tomatoes:
oranges:
and for a dictionary:
Code:foreach(var item in shoppingList){ return item; }
[DOUBLEPOST=1477262270][/DOUBLEPOST]You can see some examples in any of my plugins, or someone like @k1lly0u -
thanks

[DOUBLEPOST=1477263642][/DOUBLEPOST]Currently im developing a Sort Of Rust Essentials Plugin. a take off of the popular Essentials for minecraft. -
I get System.Collections.Generic.List'1[System.Object] in game when i execute my command "Help"
Code:using System.Collections.Generic; using System; using Rust;namespace Oxide.Plugins { [Info("Rust Essentials", "NoSharp", "0.1.1", ResourceId = 2011)] class Rsentials : RustPlugin { #region Config protected override void LoadDefaultConfig() { PrintWarning("Creating a new configuration file For Rust Essentials"); Config.Clear(); Config["Help Command"] = "Help"; Config["Help Command Content"] = new List<string> { "--HELP--", "Clans Help -", "Some other plugin help - " }; SaveConfig(); } #endregion #region Commands [ChatCommand("Help")] void cmdChathelp(BasePlayer player, string command, string[] args) { SendReply(player, Config["Help Command Content"].ToString()); } #endregion } } -
Config doesn't support string list too well from what I can tell. You would need to do something like
then to send it to chat you need something likeCode:new List<object>();
[DOUBLEPOST=1477269640][/DOUBLEPOST]Thats a simple way of doing it. though thats off the top of my head. But that would be the basic function of it.Code:var HelpTextt = Config["Help Command Content"].ToArray();foreach (var msg in HelpTextt) { SendReply(player, msg.ToString()); } -
Because a list can't be directly 'converted' into a string, that's just not how they work.
Here's an example of *a* proper way to use it:
Code:var cfgSB = new StringBuilder(); var listCfg = Config["Help Command Content"]; for(int i = 0; i < listCfg.Count; i++) { var str = listCfg[i]; cfgSB.AppendLine(str); } SendReply(player, cfgSB.ToString()); -
Well aren't you fancy
lol -
Wulf Community Admin
It would probably be done in a single line.
-
Hence the "*a* proper way". Not everyone likes one lines, either, lol. My brother gets upset any time I paste some of my code to him where there's a bunch of stuff on one line, he says it's hard to read and looks worse to him.
-
Wulf Community Admin
More isn't always "proper", if that was the case there wouldn't be as many of the shortcuts and advancements that we have in C# today.
A lot of it comes down to style though and opinions of the developer. -
I like shortcuts and clean code. Makes life simpler
-
Well, yes, but in this case, it's almost undoubtedly easier to read what I had pasted than it would have been on a one-liner. Which is especially important considering (and please take no offense) the OP is fairly new to this, so I figured a little less compact doesn't hurt.
Personally I try to make things as compact as possible in most cases. Anyway, I think we've derailed the thread enough by now, lol.
EDIT: By the way, the quote wasn't saying it was THE proper way, it was saying it was A, which is why I had used asterisks, in case you had misunderstood. The point of that was for me to say there are many ways you can do this, this is just one of the ways, the only reason I had even used the word 'proper' is because .ToString() on a list is not one of them. -
Wulf Community Admin
Keep in mind that Oxide is made to be modular, so there isn't really a point in making an all-in-one plugin. Many of the plugins you'd try to squash into one already exist and would allow more flexibility than you'd have in a single mashed up plugin. -
ok
i might try to do something else.
[DOUBLEPOST=1477291923][/DOUBLEPOST]After this is there an actual way of doing it?
[DOUBLEPOST=1477292061][/DOUBLEPOST](I still need to know how lists/arrays work in Configuration files.) -
Configuration files can be seen as a Dictionary<string, object>
Configuration files are always saved as a dictionary<string, object>. It is perfectly possible to save a list to a configuration file but when you read the value from the config it will have the type `Object` (in the case of a list, List<object>) so you will be required to cast it back to a List<string>.
Casting a List<object> to a List<string> is not possible and you would have to create a new list for this.
There is however another way to handle configs and this is by creating your own class.
Example config, using `Config`:
Using a custom object:Code:using System.Collections.Generic; using System.Linq;namespace Oxide.Plugins { [Info("Config Object Example", "Mughisi", 1.0)] class Tests : RustPlugin { protected override void LoadDefaultConfig() { Config["Content"] = new List<string> { "--HELP--", "Clans Help -", "Some other plugin help" }; } private void Loaded() { Puts("[DEBUG] Printing all config values 1 by 1:"); for (var i = 0; i < (Config["Content"] as List<object>).Count; i++) Puts($"[DEBUG] Entry {i}: {(Config["Content"] as List<object>)[i]}"); Puts("[DEBUG] All values as a single string from the DynamicConfigFile object using a newline as separator:"); Puts($"[DEBUG] {string.Join("\n", (from o in Config["Content"] as List<object> select o.ToString()).ToArray())}"); } } }
Code:using System.Collections.Generic;namespace Oxide.Plugins { [Info("Config Object Example", "Mughisi", 1.0)] class Tests : RustPlugin { private MyConfigObject config; public class MyConfigObject { public List<string> Content { get; set; } } protected override void LoadConfig() { base.LoadConfig(); config = Config.ReadObject<MyConfigObject>(); } protected override void LoadDefaultConfig() { config = new MyConfigObject { Content = new List<string> { "--HELP--", "Clans Help -", "Some other plugin help" } }; } protected override void SaveConfig() { Config.WriteObject(config, true); } private void Loaded() { Puts("[DEBUG] Printing all config values 1 by 1:"); for (var i = 0; i < config.Content.Count; i++) Puts($"[DEBUG] Line {i}: {config.Content[i]}"); Puts("[DEBUG] All values as a single string from the MyConfigObject object using a newline as separator:"); Puts($"[DEBUG] {string.Join("\n", config.Content.ToArray())}"); } } } -
thanks
