[Guide] Coding for CoEmu/Cofuture

11/16/2009 00:49 Zion~#16
How thoughtful. :)

EDIT:
Ofcourse python is my favorite language next to vb:p.

BTW Yashi there's an easier way to start processes..
11/16/2009 05:18 ImmortalYashi#17
Quote:
Originally Posted by Zion~ View Post
How thoughtful. :)

EDIT:
Ofcourse python is my favorite language next to vb:p.

BTW Yashi there's an easier way to start processes..
Heya,

I know, Process.Start("http://google.com/search?q="");

EDIT:
'Last edited by ImmortalYashi; Today at 00:46. ' Should explain why i didn't thought of this.

Cyaa,
Yashi.
11/28/2009 01:30 GRASSHOPPA#18
heres a little list thats pretty helpful(at least to me)
Code:
Type  	 Size (in bits)  	 Range

sbyte 		8 		-128 to 127
byte 		8 		0 to 255
short 		16		-32768 to 32767
ushort 		16		0 to 65535
int 		32		-2147483648 to 2147483647
uint 		32		0 to 4294967295
long 		64		-9223372036854775808 to 9223372036854775807
ulong 		64		0 to 18446744073709551615
char 		16		0 to 65535
kinda self explanatory

@pro4never
nice guides...
i like how u write them..keep it up =]
12/17/2009 07:54 sawickas#19
Ok i hawe quęstion how know all opcion wat yuo can add to npc like (teleport 2010 555 888) it is exmpl to teleport char to some place,wat abaut add efekt like top archer and how to save it for like 1 mont? Or any another opcions
12/21/2009 19:04 pro4never#20
So a super short rant on if statements...

I've seen a couple people confused by them on here and I remember having some people I was helping not understand how to lay them out either.

WHEN USING IF STATEMENTS, THINK OF THEM LIKE BRACKETS IN ALGEBRA! IT IS A LOGICAL ORDER IN WHICH DATA/CALCULATIONS ARE EXECUTED.


if
else if
else if
else if
else

You do not NEED else if/else. They are saying exactly what they say, IF the first statement doesn't get executed, check the else if and try to execute it, if NONE of the conditions are met (for if/else if's) then execute the else statement.

Not hard to understand but I get all sorts of people doing strings of if statements and then wondering why odd things are happening.

Eg:

if x > 0
do x
if x > 1
do y
if x > 2
do z

in this case if x = 1 x happens
x = 2 x AND y happen
x = 3 or more X Y AND Z happen. If that's the way you want your commands to be executed then that's great... just think about it before you write it though.


Eg 2:

if x > 0
do x
else if x > 1
do y
else
do z

In this case if x = 1 x happens
if x = 2 or more only x happens because the first if statement happened thus the else if/else statements are never even checked!
if x < 1 then z happens.

In this situation you will NEVER have y happen. You would be surprised how many people I've had ask for help because they cant get a npc/quest/event working and it's simply because they are doing if statements that simply can never be called.



Anyways: super super basic but hope this will help some people.
12/21/2009 21:13 Fleaman#21
Nice guide man!!!
01/07/2010 18:27 adoptado111#22
So can someone tell me were can i learn how to code?
01/07/2010 22:12 xScott#23
haha nice, im learning vb in school :/ trying to learn C# also :)
01/08/2010 03:08 pro4never#24
Quote:
Originally Posted by adoptado111 View Post
So can someone tell me were can i learn how to code?
What are you trying to code?

There are a HUGE number of guides already posted here and there are quite a few people willing to answer questions.

One of the best ways to learn to code is simply TRY. Look at some examples and guides and then get into a source and mess around. if you break it you can always go back and start from the base source again. See what you can accomplish and if you put enough time into it you will be able to accomplish alot. Start with the basics and work your way up and things are alot easier.
02/12/2010 13:27 renetjuuh#25
yooooooooooooooo i love your work pro never
03/23/2010 06:11 pro4never#26
Ok so just a bit of a rant/explanation of functions and organization in your source (or any coding you do)

Again: please keep in mind I'm far from 'pro' and I don't know everything. I never really had any formal education so my terminology or approach may be a tad off but the basic concepts should still be quite valid. I welcome any constructive criticism and will obviously credit you with any corrections made.


What is a function
(Aka Method... I always have referred to them as functions though)

A function is basically a calculation that does something, it can either return a value or not (void).

When should I use a function?
Any time you are coding something that you will want to use more than once

How long should I make my functions?
There is no real answer for how long or short a function should be but here's a tidbit that you may wanna keep in mind that my proff mentioned once...

A function should be doing ONE thing...

If you are writing your function and it is getting very long, chances are you could break it down into more functions which will make your coding in the future much simpler... although there are obvious exceptions to that.

I still don't understand! What is a function?! What would I use one for?

Anything under the sun! You could write a function to spawn monsters into the game, teleport all players in the entire server to a certain map, give exp/cp/money to all connected players, send them all customized text.. literally anything you want to do.



Types of functions (basic)
Basically you will have either a function that returns a value or one does not... the difference between them will be determined right as you are declaring it.

Basic types are
-void (no return value)
-int (int return value)
-bool (bool return value, durrr)
-double
-long
-string
really any of the data types associated with C#. The one to use is up to you but basically... 2 values use bool, small use byte, text use string, larger numbers use w/e one fits it best (dependent on size/decimals/negative)

Example of voids!

void DivideXbyY (double x, double y)
{
double z = x / y;
}

would assign the variable z to whatever x/y is... there are a few problems with this.

Problem 1:: We did not return the value anywhere... or in this case do ANYTHING with it...
- This could be less of a problem if lets say you only wanted to print the number (Console.WriteLine(z);

z CANNOT be used outside the function... it is a local variable!

you CANNOT assign z to something through calling the function (int a = DivideXbyY(20, 10)). That will give you an error such as "cannot implicitly convert type 'void' to 'int'". That's because a requires an INT value (full number) and our function has a value of null (or nothing).

you CANNOT call this function from anywhere else in the source. It is not declared in a public scope.

Lets re-write it to be more 'useful'

if we declare it in a global scope we can use it from anywhere else in the scope

public void DivideXbyY (double x, double y)
{
double z = x / y;
}

if we make its type double it will be able to return a value for use making it FAR more useful though!

public double DivideXbyY (double x, double y)
{
return (x / y);
}


NOTE: using int for x and y means it will be returning an int... you wouldn't want that but hey, this is an example!

We can now assign the return value by doing something along the lines of...

double a = DivideXbyY(10,20);

you could always use a variable for 10,20 but again, example!)

To Static or not?
In most functions in your source you will see the word "static" when it is being declared.

Simply because I don't know the terminology perfectly and haven't really delved into it tooo much... static means the function can be called from anywhere but non static methods/functions are limited in what they can access... basically almost all your stuff in conquer is going to be static functions (if you want them to be accessible without any hassle throughout the source.)

Simply add the word static when you are declaring them (aka public static void Blehhh(); )


CANNOT BE STATED ENOUGH

If you have a section of code you can foresee yourself using in the future... WRITE IT AS A FUNCTION! It will save you a lot of time and it will save a lot of lines of code.

ALSO CANNOT BE STATED ENOUGH

If you have a large section of code... break it down into smaller functions! You can call functions from within others... you can make some damn simple functions that will save you a TON of time

Eg: write a function to display an effect @ your characters position instead of having to refer to the messy packet code like you normally would

Eg: long sections of code such as a lottery/gambling system. Build a function to do the entire process then divide it into simpler functions such as one to build a text string that reports what was won, another one to do a percent success calculation on how often each quality is awarded (using lets say a constant value in the server so your can maintain uniform rates across all sections of play including drop rates)

Sky is the limit really...





Anyways, I know this has no concrete examples and almost no one will read it... I just hope some people can learn a bit from it. Functions really did confuse me a bit when I first started coding (mostly cause I was trying to translate my limited C# exp with scripting over to python, blehh) so thought I'd try to make it a bit clearer for ppl just starting.

Enjoy,
P4N
03/24/2010 20:27 ~Yuki~#27
Nice one. i´ve red it, your doing a good job
05/31/2010 20:00 pro4never#28
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

Code:
while(Running)
{
}
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.
06/21/2010 23:02 renetjuuh#29
very usefull never told that dictionary was stronger then hastable
10/06/2010 03:51 pro4never#30
Not posted anything in this for a while. I'm switching these away from source specific guides and focusing on features in C# instead.

If a moderator wouldn't mind, could you change thread title to "[Guide] C# and you, writing your server with common sense" or something equally lame ^^


This will be a short multi rant on a few different things. They will be short and to the point. As always if you have questions about my explanations or I make a mistake please tell me or add onto them.

Arrays:

Arrays in C# are used to hold more than one of a certain value (eg byte[] holds an array of bytes which can then be recalled by using the entry number)

NOTE: in arrays, the first element is 0 and goes up from there.


COMMON TYPES OF ARRAYS

byte[]: used for packets generally. These will be the most common arrays you use. Each 'section' of a packet generally consists of 1, 2, 4, 8 or counted bytes. In co packets these are little endian structured (meaning reversed byte order. a uint with a value of 1 would be 1 0 0 0 one with a value of 17 would be 0 1 0 0, etc)

string[] generally used for your commands and similar things. You will want to use the .split function to split a string based on a certain character (usually spaces but a number of things could apply). This is when you will see things in commands such as Cmd[1] or w/e.


Arrays Of Arrays/Jagged Array
I've never really seen this used in C# often but it's when you do something like

ushort[][].

This I've only seen used once and that was for getting fb coords (returning dual/paired values from a function. Personally I'd just create a structure/structure array but that's just me)




Queue:
I had NEVER heard of this before or seen it used in a pserver example until I was looking for options for handling my server floor (item removal mostly) but it's a very simple way for organizing flow of data and actions.

Queue's are a FIFO (first in, first out) system of organizing data. Unlike dictionaries where you have a .add/.remove command and can recall items at will, queue's have .enqueue and .dequeue which adds an item to the END of the queue or removes from the BEGINING.

This is only useful in situations where you want to handle things in order.

Eg: Handling sending of packets to a client, you may wish to send them in a specific order as resources are freed up. As such you may have a queue of packets waiting to be sent which will then check conditions of the next item in the queue (using peek function) and if met, dequeue and handle the packet. When adding you simply .enqueue and you're good to go.

It's a very simple solution and works wonders for very specific situations. Definitely adds something new to your arsenal of tools.



goto:

Another thing that's used in some sources but most people I've never seen use often is 'goto' it allows you to jump from one section of code to another at will dependent on conditions. You simply place a label where you want to go (anything:) and then use goto label; when you wish to go there in your code.

Lets use an example of where I've seen it used in sources before. Lets say you are pulling weapon types, running all sorts of switch and check statements to check your weapon type, check if you have the passive skill, check if it activates, etc. You may be getting into some nasty nested loops or checks. Once you've found the information you want, instead of sorting out all your break; statements and ensuring your code gets where it needs to go you can simply use a goto statement.

Note: don't over use this, it's just something to keep in mind for SPECIFIC situations. Always try to use the best tool for the job. The purpose of this thread is to introduce people to some tools so that as they get more comfortable with coding they will find better options and ways of coding things.



Suggestions for other posts or would you guys like to see more source related stuff vs theory?