Saturday, February 11, 2012

All about the Pawn Programming Language

LOOPING 

Loops are basically what the word says they are, it is a piece of code that is created to run more than once. So lets waste no time and dive into what kind of loops there are and how they work.

Note: While this tutorial is for PAWN, loops apply to just about every programming language in existence, and the syntax is almost always the same!

For loop

The for loop is a loop which is to be run an exact amount of times. Lets make a simple example to explain it:
for(new var = 1; var <= 5; var++)
{
    printf("Value of var is now %d", var);
}
print("For loop finished");

So what we’re doing here, is creating a new variable that can only be used in the loop called “var”, then we’re checking if it’s less than or equal to 5 and if it is, run the code inside the curly brackets and increment var by 1. The output of this code is:
Value of var is now 1
Value of var is now 2
Value of var is now 3
Value of var is now 4
Value of var is now 5
For loop finished


Notice anything? Well it’s the exact same output as the while loop! So this is how you would do the same loop in a for loop, it’s a much better way of doing this particular example of code. The same applies for this type of loop too, it will delay any execution of other code until the loop is finished executing.
While loop

The while loop is simple, it checks if a condition is true and while it’s true, it runs a piece of code. It is used for a loop that you don’t know how many times you want it to run. We know how many times the loop will run in this example, but we’ll just use a while loop for the purpose of the example:
new var = 1;
while(var <= 5)
{
    printf("Value of var is now %d", var);
    var++;
}
print("While loop finished");




So what we’re doing here, is creating a new variable with a value of 0. Then we’re making a while loop that checks if var is less than or equal 5, then runs the piece of code in between the curly brackets. The specific output of this piece of code is:
Value of var is now 1
Value of var is now 2
Value of var is now 3
Value of var is now 4
Value of var is now 5
While loop finished


So what we see here is that the loop ran 5 times and the finished. It’s worth understanding that the loop will stop the execution of any other code until it’s finished, as this is one the main causes of unoptimized and poorly coded scripts.

ARRAY

Pawn features basic "arrays". An array is a simple type of aggregate data. This means you can store multiple values in one variable! An array follows the same rules as a regular variable, and it has the same types. It simply can contain multiple values. You define an array with brackets, and how many values it can hold. For example:
//This will declare a variable called "Players" which holds 32 numbers. 
new Players[32]
 
//You can now store values in any of the 32 "slots" this array has.  
// The slots are numbered from 0 to n-1, or in this case, 0 to 31.
//Every slot starts off as 0.
 
//Set slot 0 to 5
Players[0] = 5
 
//Set slot 1 to whatever is in slot 0, in this case, the number 5
Players[1] = Players[0]
 
//This is invalid! 
//Although there are 32 slots, they are numbered from 0 to 31.
//Doing this results in AMX Native Error 4 - AMX_ERR_BOUNDS
// or, it simply won't compile!
Players[32] = 15
 
//This is also totally invalid           
Players[-1] = 6
 
new a = 3
//This is also totally invalid.  
//a must be a constant number.
new BadArray[a]
 
//So this is valid:
const b = 3
new GoodArray[b]
 
//You can also use Compiler Directives (See last section)
 
#define ARRAY_SIZE 3
new Array[ARRAY_SIZE]
Arrays can also be declared with groups of data default, such as:
new Numbers[4] = {0,1,2,3}
//Note: it is important that you make sure the amount of numbers
// you pass and the size of the array match
You can also use any data type with arrays:
//Array of floating points:
new Float:Numbers[4] = {0.0, 1.2, 2.4, 3.8}
//Array of booleans.  Note this sets every slot to true.
new bool:playerHasGun[33] = {true, ...}


Conditionals

Conditionals allow you to test if an expression meets a standard, and to execute code based on that decision.

If Statements

The most important conditional is called "if ... then". If evaluates whether a given expression is true or false. It if is true, it executes a block of code. If not, it executes a different block of code. For example:

This is an example of the most basic if ... then statement. The first line checks to see if the expression is true. In this case, if the variable a is equal to 5, then the if statement will execute the block of code underneath it, which sets a to 6.

if (a == 5)
{
   a = 6
}
However, what happens if a does not equal 5? Then the code will not be executed. However, you can tell it to execute code if the conditions are not met. Now, if a is equal to 5, a will be set to 6. Otherwise, it will be set to 7.
if (a == 5)
{
   a = 6
} else {
   a = 7
}
There are many different operators you can use inside the if () statement. In fact, you can use any expression that evaluates to true (not zero) or false (zero).
//This will return true if a does not equal 5
if (a != 5) {}
//Returns true if a is greater than 5
if (a > 5) {}
//Returns true if a is less than 5
if (a < 5) {}
//Returns true if a is greater than or equal to 5
if (a >= 5) {}
//Returns true if a is less than or equal to 5
if (a <= 5) {}
//Returns true because 11 is true
if (5+6) {}
//Returns true of both a and b are true
if (a && b) {}
//Returns true if 7.5 is greater than c
if ( ((5*3)/2) > c) {}
//Always returns true no matter what
if (true) {}
//Never returns true
if (false) {}
Note that array comparisons have restrictions. This is invalid:
my arrayOne[3]
my arrayTwo[3]
if (arrayOne == arrayTwo) {
You must do:
if ((arrayOne[0] == arrayTwo[0]) && 
    (arrayOne[1] == arrayTwo[1]) && 
    (arrayOne[2] == arrayTwo[2])) {

Obviously, this would get very tedious with large arrays. You will see later on how to easily compare strings and arrays.
The if...then model of conditional switching can be brought up to another level. Pawn provides a way for you to provide multiple levels of true and false expressions.

//Example of "if...else if"
if (a == 5) {
   //This code will be run if a is 5.
} else if (a < 6) {
   //This code will be run if a is less than 6
} else if (a == 7) {
   //This code will be run if a is 7.
} else {
   //If none of the above conditions are met, this code will be run.
}

It is important to note that in the above example, each code block is not "fall through". That means each of the conditions will be checked in order, and if one is true, the code will be executed and the if statement is done. It will not execute multiple true conditions.

No comments:

Post a Comment