Ok so been a long time since I've updated this as #1 I've been staying out of coding for the past few months due to exams/busyness/being burned out but I figured I'd add a few more BASIC things to these mini tutorials.
Disclaimer: As I think I've stated every time. I'm far from pro... please correct me if I make a mistake (which I probably will). I'm simply trying to explain some simple coding principles ideally in a way that people who are just starting can understand while relating them to things in conquer servers. (lets face it... most new coders here don't care to know what an int is or w/e... They want to learn how to do something to fix/modify their server.)
Mini Rant #1: Dictionaries
So I went over hashtables early on in the thread but you really shouldn't be using them often.. besides dictionaries are so much more useful in the long run.
Dictionaries have MUCH better performance than hashtables and will be much more powerful and useful for your server as it grows. For this example I'm going to create a sample program (not related to conquer in this case. Next time I'll try to focus on something for pservers though.)
In this sample program I'm making a new Console application called DictionaryTest.
Note: the static void Main(string[] args) function is what runs when the console application is loaded. Being empty if you try to debug the program, you will have a console application flash up for a second and go away. That's because all contained code has executed and is finished. You can solve this a few ways. #1 have a console.readline() line that basically has the server wait for the user to enter something in the window. This input can be assigned to a variable or anything else you like. #2: Have a bool assigned a value of true and then insert code into the function reading as
In that case the server will repeatedly run that code (as the bool Running is never = to false).
For this example I'll be using the first method. Console.ReadLine();
Now we want to create a dictionary. This is VERY simple to do. You need to first decide what values you want your dictionary to store. You can use ANY data type associated with C# OR YOUR SERVER! This means you can have it hold any structure you create (characters, items, profs, skills.. ANYTHING)
In this case we shall use a simple int, string structure (stores a number as key and a string as value). To do this you simply need to add a line saying
Code:
Dictionary<int, string> TestDictionary = new Dictionary<int, string>();
The section to the left of the equals sign imply says the dictionary named "TestDictionary" has the values int and string. We then need to define what it IS. So we type new dictionary<int, string>() to create it.
Adding to the created dictionary
We are going add strings to be stored through the use of the Console.ReadLine(); command.
To do this effectively we are going to create 2 variables.
Code:
int Key = 1;
string Value = Console.ReadLine();
This should be quite obvious but this creates the variable Key and gives it the value 1 and the variable "Value" and sets it to whatever is taken from the console.readline() command.
Now we need to add the values to the dictionary.
Again, very simple all we need to use is Dictionaryname.Add(key, value);
Code:
TestDictionary.Add(Key, Value);
Boom, we are done.
NOTE: If you try to add two values with the same key you will get an exception saying "An item with the same Key has already been added."
This means EXACTLY what it says. Keys are unique values that allow data to be retrieved from a dictionary. You CANNOT have two items with the same key in the same dictionary.
Retrieving data from a dictionary:
So lets bring back the data we just entered.
This can be done easiest by using DictionaryName[Key];
Code:
Console.WriteLine(TestDictionary[Key]);
This will write to console the value stored by TestDictionary at key "Key". Again, VERY simple.
Remember: foreach loops can be used to run through ALL stored values in a dictionary through the use of
Code:
foreach (KeyValuePair<int, string> ThisLoop in TestDictionary)
{
Console.WriteLine(ThisLoop.Value);
}
Not gonna explain it more than the fact that we are defining the structure of the dictionary we are referencing.. then assigning the value returned to a variable (ThisLoop) and saying where we are getting the information from (in TestDictionary)
For a full recap of Foreach loop go back to the first page I think... but w/e.
Wiping a Dictionary
Not used often but if you want to lets say... reload values into a dictionary you will need the database empty (if not you will run into the key already entered problem..)
Simply use DictionaryName.Clear();
Code:
TestDictionary.Clear();
Discovering if data has already been entered:
Ok so seeing as I already brought up the fact that Keys are UNIQUE values and you will get an exception if you try to add two values with the same key... we need to find a way to get around that, right? Simply using an if statement we can find everything we need to know about a database.
Code:
if (TestDictionary.ContainsKey(Key))
{
Console.WriteLine("Error: Dictionary already contains an entry with Key of: " + Key);
}
else
{
TestDictionary.Add(Key, Value);
}
Problem solved.
You can also check via the value stored but that shouldn't be needed usually.
Code:
if (TestDictionary.ContainsValue(Value))
{
Console.WriteLine("Error: Dictionary already contains an entry with Value of: " + Value);
}
else
{
TestDictionary.Add(Key, Value);
}
Ok... if I missed something or explained something wrong please lemme know and I'll correct it/give credit.
Also lemme know if you have any questions about dictionaries... I think I got most of it covered here but I probably missed some stuff...
I'll try to post a few more things over the next while. Lemme know what you guys need covered or think new coders would find useful. If needed I can apply things to a specific source or topic next time.