30.04.2010, 20:42
This tutorial is meant for mainly beginners. Some advanced might get some benefit from this tutorial aswell.
Indexing:
1.10 Math and Programming
Assuming you are new to programming, you kinda suck at this all. Scripting that is.I just want to note that there is a difference between scripting and programming. I don't remember what exactly it is but who cares. whatever you do, if you use PAWN, tell people you script, not programming.
In order to program, common sense tells you you need to be decent at mathematics. If your sense doesn't tell you that, please start walking towards a train because you are mentally ill.
A basic mathematical problem would be:
5*5
We know the answer is 25, and so does the computer. If not, buy a new one cuz your computer doesn't deserve to be used.
now imagine, that instead of numbers, we use values that are not rounded.
5,5 * 5,5
now for us it gets harder to calculate even though this still is way to easy. Computer is at it's usual speed and found the answer 30,25.
using a metric system, you can obtain the surface by multiplying 2 sides with eachother.
in this example, we gave 5 the unit of centimeters where we can obtain square centimeters from.
5cm*5cm=25cm2
1.20 Basic Variables in global understanding
Some of you might know or not know, mathematics is all about using variables and functions.
for example, you can use sin cos tan to calculate the angle of a corner of a triangle, or use it to find a wavelength for whatnot.
let's assume we do not wanna know the answer of a basic mathematical equation but yet we need to use the answer from it.
you can use a variable to store the answer in it.
for example, a = variable that holds the answer
5*5 = a
in this example, we know a = 25, because this is the equation. However, if we wish to assign the value of the equation into the variable a, it is the oposite.
a= 5*5
in this example, we assign the value of the equation 5*5 into a.
now I want to use it into another equation.
b=a*1
for obvious reasons, b = 25. If I would change the equation 5 * 5 = a into a = 4* 5, I haven't changed equation b=a*1. Yet, the answer is different.
which is 20 if you didn't knew that.
This is the concept of a variable. Having a certain equation using a variable, and get different results without changing the equation.
Also, it saves effort. You can store an answer from the beginning into one variable and later on have new input into the same equation and compare those outputs.
for those shitheads: output = results from a function where input = the information you put in.
in programming itself, you have different types of variables. Here is a god damn list:
1. Integer (over the common, a rounded value between I dunno, only if pawn could tell me how much bytes integer get signed with)
2. Float (an integer that supports unrounded values. Usefull for storing coordinates or speeds and distances)
3. Boolean (basicly, holds either yes/true/1 or no/false/0), takes up only little asigned memory)
4. String (keep it simple, this is text)
//These are the common variables. meh I am to lazy to post more variable types. you can look them up if you are interested.
variables are used so functions can take a certain routine without having the need to change itself (that much)(Or just pure lazyness from calculating out of their plain heads, lazy bastards).
1.30 Functions handling
You know this right? No, oh my god I thought I had seen it all but no, you keep surprising me.
how do you explain a function? I see it as a short replacement for a longer complicated code.
let's say our function is this: calc will be the function's name and input will be the input put in.
PS: in this example I use some common programming rules.
calc(input)
{
return 5*input;
}
I want to note, even though it might seem so, programmaticly this isn't the most efficient programming style. Further explanation will follow later on.
anyways:
a=calc(5)+5
calc(5) is a replacement for 5*input. We can make a equation here.
calc(5) = 25
a= 5*input+5
a=25+5
a=30
however, this answer is solid now. this is the reason why variables are used.
assume a = 10
b=calc(a)+a;
what answer would this equation give? You don't know? Really you should learn some more math.
b=60
calc(input) is a function that returns an unsolid value based upon the input.
1.40 Syntaxing
Yay wtf, roflmao pornstar we finally made it to the more pawn orientated stuff.
what about syntaxing. syntaxing is the most important part of programming. In the end of this section, you will see why.
brackets are fine, YES FINE AS HELL. Lol but this is still a so called newb code. This is nothing. Basicly this only states the existence of the public function OnPlayerUpdate. It will call for it, like a werewolf calls for the innocent sheep and then eats them to pieces but without realising those weren't sheep he ate but plain air that tasted like sheep? Wth anyways it's better not to have these things inserted into your code if not necessary. Prevent the calling of functions that do nothing!
But let me explain this syntax:
public << Defines that the following information is a symbol that acts as a function that is accessible globally.(Globally accessibility means that in the complete program, this function can be called)
OnPlayerUpdate << This is the name of the function, which is used as a reference to call the code.
(playerid) <<The argument that is expected to get called with.
//Note: public OnPlayerUpdate(playerid) is called a function header.
{ << Indicating that with the function's header, the following functions are a part of what should be done when calling this function.
return << Function that holds the ability to return a value when a function is called. In most Pawn(o) Call-Backs, it usually means nothing or success/fail.
1 << The value returned
; << Indicator that a function inside this callback has finished and proceed if possible.(In this case, it will not. Return ends the function OnPlayerUpdate and therefore will not continue to do the rest of the code.
} << Indicator for the function OnPlayerUpdate has ended when talking in called functions, and therefore close the memory assigned to it.
Plain simple right?
However, this gets harder when an error occured. Putting a bracket {,} wrong, might result in hundred errors, even though there is only 1 bracket wrong.
This is why that is:
In this example, only 4 brackets where placed wrong, and already caused over 7 errors. Now imagine you have your gamemode up and running. Suddenly, you get 100 errors at once, since the function you worked lately on had it's brackets placed wrong and it happens to be in top of your script. This will result in the complete disaster movie where your gamemode is the meteorite and your server a person who gets squashed under the meteorite.
This is why syntaxing is important!
1.50 Advanced Variables in Pawn environment (arrays)
I'd never thought you could be able to read this far. I am impressed for real. You dip shits are strong enough to keep on reading. So here we go!
first of, in order to understand more advanced variable in pawn, you should know what arrays are.
an array is a variable that can hold multiple dimensions. In other words, instead of assigning 1 value to a variable, you can assign more than one. Arrays are usually used to show the user or computer that certain information belongs together. Or in the computer's case, it gets assigned the same memory space but all under the same header. Which saves computer memory. Ok, now to give you an example:
normal non-array variables:
where g = gravity, and v = velocity in kilometres per hour
2 different variables that hold in a way related to each other.
However, for a variable it would go more like this:
where f = array forces, indicator (g)ravity = 0 and (v)elocity in kilometres per hour is 1
In this case, g and v are no variables, but indicators to show which array index to use.
basicly what is says is this:
But for your eyes and reasoning, using a mask sort of speak is much easier to understand what you made.
this is where defines can kick in for usefullness.
imagine we want to create a gravity detection and handling system.
PS: this is a example to explain definitions. Usually you should use define to create settings as they are permanent.
This is how it works, this is how it goes.
However, this can be done much easier aswell. The enum statement.
as you can see, it makes no difference for the computer itself. However as a programmer it is much better as you can see exactly what variables hold what information.
Now how do we create a string?
Pawn(o) is used to be a integer oriented language. Meaning this language wasn't meant for using strings at all. Nevertheless, there are strings available in pawn for you to use.
side note: You can use -1 array indexes of the value you entered. I like to believe that the first index is used to indicate the arrays information. How much space it takes etc.
in this case, the string may be at maximum 19 characters long. My assigned string is 17 characters long so it is OK to assign this.
to use a string, you do not need to indicate the array index like an integer.
Pretty simple right?
Let's move on, array strings. No not like just before, having arrays for strings this time.
The use of array strings might be usefull. However it depends on how you work.
please know that using multiple dimensions for arrays is very limited for Pawn(o). Pawn(o) only supports 3 different dimensions. In case of a string, you could only use 2 different dimensions.
Dimension 1 2 3
| | |
new array[20][20][20];
Dimension 3 is also used to indicate a string, therefore 2 dimensions for strings.
1.60 Conditions,if/else statement, Looping, switching and line jumping etc
Here are the conditions and operators:
Basic things, easy to use. if you don't understand this, you better stop reading cuz you should kiss some monkeys ass
if and else statements are very commonly used.
basicly, these statements allow you to variablize a function.
for example, you don't want a code to open a gate that is already open. you want to close it. therefore, we introduce you the if and else statements.
if(condition is true) do action function.
or
if(!condition is true) do action function.// ! means the oposite of the true condition, basicly a check if the condition is false.
very easy to say: if condition is true, to this or else do that:
Basic principle of using if and else statements
Question where I know the answer on: Why do we use while or for?
for looping through variables most of the time. Let's say you have an array carrying only 1 dimension, but all these numbers are secret keys that together can give you power to immortality.
Then a friend of yours suddenly told you the answer you where waiting for. He told you if you add all those values to together, you get the key of total damnation and you can get immortality. However, you are way to lazy to add all those values one by one.
therefore, you will loop through your array to have the computer get you the number using a for statement.
for(starting function; when condition; do action)
for is a special kind of function, that does not end till the condition is true. for every END of a loop, it does the action you entered.
now let's create our setup and function to have the computer calculate the key for immortality.
note: this function usually is used with variables. Don't mistake that otherwise isn't possible!
you should know about return and function header and such by now, so let's skip that and more on to the loop and it's functions.
for(new i=0;i<6;i++)
action= create new variable i and assign value zero.
condition is, if i is smaller than 6, THEN do action.
if the condition was true, on the end of the loop, add 1 to i.
Continue to next loop.
the brackets that come along are just like the ones within a function.
they assign functions between the brackets as a part of that code. In this case, the loops will do the following actions till condition is false or true.
store+=destructionkeys[i];
for every loop, use i as array index.
add the value of destructionkeys[i] to store
if you paid attention well, you should know or at least figured that the answer or value returned is store = 157404.
this was the looping part, and using array inside a loop. in short, you can use a loop to get all values of an array and manipulate them to your bidding.
Damn u manipulator.
Besides for, there is a while statement.
the while statement basicly is the same as for, but on the end of a loop there isn't any function done.
while basicly is for(starting action, condition)
while (starting action, condition)
see yourself how to use this.
The following part is copied right from the sa-mp wiki:
==
do-while
A do-while loop is a while loop where the condition comes after the code inside the loop instead of before. This means that the code inside will always be executed at least once because it is done before the check is done:
new
a = 10;
do
{
// Code inside the loop
a++;
}
while (a < 10); // Note the semi-colon
// Code after the loop
==
The use of do while, is that you can variablize the functions added using if and else statements.
IMPORTANT NOTE: You cannot use local/remote functions as the action as there will be no valid actions taking place for the statements for and while!
explaining switch/case.
example uses imaginative variable registered users as REG_USERS non-array
in theory, switch is the same as a long chain of else/if conditions.
However, if you compare the speed of both ways, switch case is much faster when dealing with large numbers and the amount of calls.
Note: switch can only compare values when switching a variable. It is only faster when larger amount of cases/if else conditions!
Line jumping and used isn't used much therefore I won't discuss it here.
Indexing:
- 1.10 Math and Programming
- 1.20 Basic Variables in global understanding
- 1.30 Functions handling
- 1.40 Syntaxing
- 1.50 Advanced Variables in Pawn environment (arrays)
- 1.60 Conditions,if/else statement, Looping, switching and line jumping
- 2.10 Writing your first sucky but fully working code
- 2.20 Effective coding
- 2.25 Explanation why the hell you should do that
- 3.10 Creating a concept
- 3.20 Extending the concept
- 3.30 Why the shit you need it
- 4.10 Seemly unnoticable powerz of pawn called classes/local-remote functions
- 5.10 Error Handling
- 1000 Being smart
1.10 Math and Programming
Assuming you are new to programming, you kinda suck at this all. Scripting that is.I just want to note that there is a difference between scripting and programming. I don't remember what exactly it is but who cares. whatever you do, if you use PAWN, tell people you script, not programming.
In order to program, common sense tells you you need to be decent at mathematics. If your sense doesn't tell you that, please start walking towards a train because you are mentally ill.
A basic mathematical problem would be:
5*5
We know the answer is 25, and so does the computer. If not, buy a new one cuz your computer doesn't deserve to be used.
now imagine, that instead of numbers, we use values that are not rounded.
5,5 * 5,5
now for us it gets harder to calculate even though this still is way to easy. Computer is at it's usual speed and found the answer 30,25.
using a metric system, you can obtain the surface by multiplying 2 sides with eachother.
in this example, we gave 5 the unit of centimeters where we can obtain square centimeters from.
5cm*5cm=25cm2
1.20 Basic Variables in global understanding
Some of you might know or not know, mathematics is all about using variables and functions.
for example, you can use sin cos tan to calculate the angle of a corner of a triangle, or use it to find a wavelength for whatnot.
let's assume we do not wanna know the answer of a basic mathematical equation but yet we need to use the answer from it.
you can use a variable to store the answer in it.
for example, a = variable that holds the answer
5*5 = a
in this example, we know a = 25, because this is the equation. However, if we wish to assign the value of the equation into the variable a, it is the oposite.
a= 5*5
in this example, we assign the value of the equation 5*5 into a.
now I want to use it into another equation.
b=a*1
for obvious reasons, b = 25. If I would change the equation 5 * 5 = a into a = 4* 5, I haven't changed equation b=a*1. Yet, the answer is different.
which is 20 if you didn't knew that.
This is the concept of a variable. Having a certain equation using a variable, and get different results without changing the equation.
Also, it saves effort. You can store an answer from the beginning into one variable and later on have new input into the same equation and compare those outputs.
for those shitheads: output = results from a function where input = the information you put in.
in programming itself, you have different types of variables. Here is a god damn list:
1. Integer (over the common, a rounded value between I dunno, only if pawn could tell me how much bytes integer get signed with)
2. Float (an integer that supports unrounded values. Usefull for storing coordinates or speeds and distances)
3. Boolean (basicly, holds either yes/true/1 or no/false/0), takes up only little asigned memory)
4. String (keep it simple, this is text)
//These are the common variables. meh I am to lazy to post more variable types. you can look them up if you are interested.
variables are used so functions can take a certain routine without having the need to change itself (that much)(Or just pure lazyness from calculating out of their plain heads, lazy bastards).
1.30 Functions handling
You know this right? No, oh my god I thought I had seen it all but no, you keep surprising me.
how do you explain a function? I see it as a short replacement for a longer complicated code.
let's say our function is this: calc will be the function's name and input will be the input put in.
PS: in this example I use some common programming rules.
calc(input)
{
return 5*input;
}
I want to note, even though it might seem so, programmaticly this isn't the most efficient programming style. Further explanation will follow later on.
anyways:
a=calc(5)+5
calc(5) is a replacement for 5*input. We can make a equation here.
calc(5) = 25
a= 5*input+5
a=25+5
a=30
however, this answer is solid now. this is the reason why variables are used.
assume a = 10
b=calc(a)+a;
what answer would this equation give? You don't know? Really you should learn some more math.
b=60
calc(input) is a function that returns an unsolid value based upon the input.
1.40 Syntaxing
Yay wtf, roflmao pornstar we finally made it to the more pawn orientated stuff.
what about syntaxing. syntaxing is the most important part of programming. In the end of this section, you will see why.
pawn Code:
public OnPlayerUpdate(playerid)
{
return 1;
}
But let me explain this syntax:
public << Defines that the following information is a symbol that acts as a function that is accessible globally.(Globally accessibility means that in the complete program, this function can be called)
OnPlayerUpdate << This is the name of the function, which is used as a reference to call the code.
(playerid) <<The argument that is expected to get called with.
//Note: public OnPlayerUpdate(playerid) is called a function header.
{ << Indicating that with the function's header, the following functions are a part of what should be done when calling this function.
return << Function that holds the ability to return a value when a function is called. In most Pawn(o) Call-Backs, it usually means nothing or success/fail.
1 << The value returned
; << Indicator that a function inside this callback has finished and proceed if possible.(In this case, it will not. Return ends the function OnPlayerUpdate and therefore will not continue to do the rest of the code.
} << Indicator for the function OnPlayerUpdate has ended when talking in called functions, and therefore close the memory assigned to it.
Plain simple right?
However, this gets harder when an error occured. Putting a bracket {,} wrong, might result in hundred errors, even though there is only 1 bracket wrong.
This is why that is:
pawn Code:
public OnPlayerUpdate(playerid)
{//No return, error 1;
}//we didn't opened this down properly
{//Seems we accidentally switched these brackets.
//No function header, error 2
return 1;//Unexpected function or declaration
}//Unexpected closure
public OnPlayerDisconnect(playerid)
}//Inverted bracket, invalid expression
return 1;
{//Inverted bracket, no error since this bracket indicates a group of actions, which is sometimes used in variables aswell.
//Error for line x to x+1
public OnPlayerConnect(playerid)
{
return 1;
}
//Waiting for the return 1, the function OnPLayerDisconnect hasn't closed. Making the compiler think OnPlayerConnect is a function inside OnPlayerDisconnect and causes not to be called as a independent function
This is why syntaxing is important!
1.50 Advanced Variables in Pawn environment (arrays)
I'd never thought you could be able to read this far. I am impressed for real. You dip shits are strong enough to keep on reading. So here we go!
first of, in order to understand more advanced variable in pawn, you should know what arrays are.
an array is a variable that can hold multiple dimensions. In other words, instead of assigning 1 value to a variable, you can assign more than one. Arrays are usually used to show the user or computer that certain information belongs together. Or in the computer's case, it gets assigned the same memory space but all under the same header. Which saves computer memory. Ok, now to give you an example:
normal non-array variables:
where g = gravity, and v = velocity in kilometres per hour
pawn Code:
g=10;//from now on, we will use pawn syntaxing.
v=20;
However, for a variable it would go more like this:
where f = array forces, indicator (g)ravity = 0 and (v)elocity in kilometres per hour is 1
pawn Code:
f[g]=10;
f[v]=20;
basicly what is says is this:
pawn Code:
f[0]=10;
f[1]=20;
this is where defines can kick in for usefullness.
imagine we want to create a gravity detection and handling system.
PS: this is a example to explain definitions. Usually you should use define to create settings as they are permanent.
pawn Code:
#define g 0 //This syntax is a syntax used by the compiler. # states that the following code is meant for the compiler. define << function define is called.
//g 10 just like a variable, assign g=0. However, when defining these things, you cannot change them later on, unless you undefine them first.
#define v 1
//Note, zero or null is the default value for newly created variables. This includes arrays.
new f[2];//new is a function that is able to define a new variable. We give f a max array index of 2, which equals to the posibility to use all array sizes (positive TILL 2, so not including 2).
f[g]=10;
f[v]=20;
However, this can be done much easier aswell. The enum statement.
pawn Code:
enum forces//indicate start of statement inside forces
{//open assigned definitions
g,//add new index, the first one equals index 0, using null/zero as index when using an enum is not supported.
v//add new index, equals 1
}//close assigned definitions
new f[forces];//assign the enum above to f.
f[g]=10;
f[v]=20;
Now how do we create a string?
Pawn(o) is used to be a integer oriented language. Meaning this language wasn't meant for using strings at all. Nevertheless, there are strings available in pawn for you to use.
pawn Code:
#define roflmao 20
new stringname[roflmao]="your sucky string";
in this case, the string may be at maximum 19 characters long. My assigned string is 17 characters long so it is OK to assign this.
to use a string, you do not need to indicate the array index like an integer.
pawn Code:
new anotherstring[roflmao];
anotherstring=stringname;//assigns the value of stringname to anotherstring
Let's move on, array strings. No not like just before, having arrays for strings this time.
pawn Code:
new stringname[20][roflmao];
stringname[0]="this is the first accessible array index for a string.";//PS: this string is out of arraysize, please do not mind it.
stringname[1]="this is the second accessible array index for a string.";//PS: this string is out of arraysize, please do not mind it.
stringname[19]="this is the last accessible array index for a string.";//PS: this string is out of arraysize, please do not mind it.
Assigning starting values can be done more efficient than this. Will be discussed later!
please know that using multiple dimensions for arrays is very limited for Pawn(o). Pawn(o) only supports 3 different dimensions. In case of a string, you could only use 2 different dimensions.
Dimension 1 2 3
| | |
new array[20][20][20];
Dimension 3 is also used to indicate a string, therefore 2 dimensions for strings.
1.60 Conditions,if/else statement, Looping, switching and line jumping etc
Here are the conditions and operators:
pawn Code:
== //equals to
!= //Does not equals to
<= // is smaller than or equals to
>= // is bigger than or equals to
> // is bigger than
< // is smaller than
&& //and use following condition
|| // Or use the following condition
++ // add (1) value
-- // substract (1) value
+ // add any following value to the starting value
- // substract any following value to the starting value
+= // add any following value //Variables only!
-= // substract any following value //Variables only!
* // Obvious, right? It means square root! (no seriously, you know what it means jackass)
/ // divide by any following value to the starting value
if and else statements are very commonly used.
basicly, these statements allow you to variablize a function.
for example, you don't want a code to open a gate that is already open. you want to close it. therefore, we introduce you the if and else statements.
if(condition is true) do action function.
or
if(!condition is true) do action function.// ! means the oposite of the true condition, basicly a check if the condition is false.
very easy to say: if condition is true, to this or else do that:
pawn Code:
new gateopened=1;//is true
public handlegate()
{
if(gateopened==1)//is true
{
CloseGate();
gateopened=0;//is false
}else
{
OpenGate();
gateopened=1;
}
return gateopened;
}
Question where I know the answer on: Why do we use while or for?
for looping through variables most of the time. Let's say you have an array carrying only 1 dimension, but all these numbers are secret keys that together can give you power to immortality.
Then a friend of yours suddenly told you the answer you where waiting for. He told you if you add all those values to together, you get the key of total damnation and you can get immortality. However, you are way to lazy to add all those values one by one.
therefore, you will loop through your array to have the computer get you the number using a for statement.
for(starting function; when condition; do action)
for is a special kind of function, that does not end till the condition is true. for every END of a loop, it does the action you entered.
now let's create our setup and function to have the computer calculate the key for immortality.
note: this function usually is used with variables. Don't mistake that otherwise isn't possible!
pawn Code:
new store;//the variable that will hold the complete key.
new destructionkeys[6];
destructionkeys[0]=12345;
destructionkeys[1]=21345;
destructionkeys[2]=31245;
destructionkeys[3]=41235;
destructionkeys[5]=51234;
a for statement cannot be called outside any function. Therefore, we create our own imaginative function. (without defining the origin, this will be done later)
public OnRequestImmortalityKey(playerid)
{
//store still is zero, it is never called yet.
for(new i=0;i<6;i++)
{
store+=destructionkeys[i];
}
return store;
}
for(new i=0;i<6;i++)
action= create new variable i and assign value zero.
condition is, if i is smaller than 6, THEN do action.
if the condition was true, on the end of the loop, add 1 to i.
Continue to next loop.
the brackets that come along are just like the ones within a function.
they assign functions between the brackets as a part of that code. In this case, the loops will do the following actions till condition is false or true.
store+=destructionkeys[i];
for every loop, use i as array index.
add the value of destructionkeys[i] to store
if you paid attention well, you should know or at least figured that the answer or value returned is store = 157404.
this was the looping part, and using array inside a loop. in short, you can use a loop to get all values of an array and manipulate them to your bidding.
Damn u manipulator.
Besides for, there is a while statement.
the while statement basicly is the same as for, but on the end of a loop there isn't any function done.
while basicly is for(starting action, condition)
while (starting action, condition)
see yourself how to use this.
The following part is copied right from the sa-mp wiki:
==
do-while
A do-while loop is a while loop where the condition comes after the code inside the loop instead of before. This means that the code inside will always be executed at least once because it is done before the check is done:
new
a = 10;
do
{
// Code inside the loop
a++;
}
while (a < 10); // Note the semi-colon
// Code after the loop
==
The use of do while, is that you can variablize the functions added using if and else statements.
IMPORTANT NOTE: You cannot use local/remote functions as the action as there will be no valid actions taking place for the statements for and while!
explaining switch/case.
example uses imaginative variable registered users as REG_USERS non-array
pawn Code:
switch(REG_USERS)
{
//using brackets, implying on that multiple actions are going to be used
case 25://if value is 25
{
//there are 25 people registered
}
case 50: //if value is 50
{
//there are 50 people registered
}
//no brackets, imply only 1 action upon case
case 100://there are 100 people registered //if value is 100
default: //If all above conditions are false, do this: //if value is not 25, 50 or 100
}
However, if you compare the speed of both ways, switch case is much faster when dealing with large numbers and the amount of calls.
Note: switch can only compare values when switching a variable. It is only faster when larger amount of cases/if else conditions!
Line jumping and used isn't used much therefore I won't discuss it here.