Let's Learn PAWN!

Tuesday November 3rd, 2011, 12:56 AM CDT|Tuesday November 3, 2011, 12:56 AM GMT-6

This is the first installment of my tutorial of PAWN, specifically once we learn the basics we'll be learning SourcePawn for you to use with SourceMod. If you don't know what pawn is, I'll let Compuphase explain it for you.

pawn is a simple, typeless, 32-bit extension language with a C-like syntax. A pawn "source" program is compiled to a binary file for optimal execution speed. The pawn compiler outputs P-code (or bytecode) that subsequently runs on an abstract machine. Execution speed, stability, simplicity and a small footprint were essential design criteria for both the language and the abstract machine.
-Compuphase on PAWN

Before we jump right into this, we'll need a good text editor for our scripts. Here are a few programs for each OS:

These are not your only options, you are free to use any text editor you want, a minimum recommendation is that the text editor you pick has line numbering.
You are free to take notes on what we're doing with your brand new text editor if you want. The first thing we're going to learn about are variables and tags. There are minute differences between PAWN and SourcePawn, I'll let you know when a distinction between the languages are needed. Now let's jump straight into this.

new a; //This is a comment
new b = 1; /* This is a multi-line comment */

Let's look at what we've just done. new a; has created a new integer variable with the name of a. It's important to note here that a variable created outside of functions or forwards is a global, a global variable can be read at any time from any forward and fuction. A little note about pawn, you are free to use semicolons at the end of every line of code. This is a decision that you, the coder, will make for yourself later on. You can make semicolons in your code mandantory instead of optional once we start learning about pragmas. You also notice I have placed comments in the code. Everything past // will be ignored by PAWN compilers. A multi-line comment is begun with a /* and is ended with a */ everything between the beginning and the ending are ignored by compilers like single line comments, these comments are perfect when you decide to document your code for tutorial purposes. Now that I've explained comments to you let's get back on track. You might be saying, "What is a equal to!" I'll tell you. a is set to 0 since we did not define a value for it like we did for b, that is what the pawn compiler does to a once you compile the code. So you can see why new a = 0; is redundant. Now let's look at invalid and incorrect ways of creating variables.

new a = 0; //redundant as you are creating a variable who will be set to 0 anyway.
new a = 1; //invalid, if a was defined above, you are trying to create a variable which already exists this would throw an error when compiling your code.
decl b = a; //invalid you can't set a variable which is being declared

Basically the above states that you can't create a variable with the same name twice, it's also redundant to set a variable to 0. Also a very important note, decl does not exist in Pawn, but does exist in SourcePawn. This is one of the differences between the two versions of the same scripting language. decl creates a variable that is filled with random garbage data. You are not allowed to set a variable at the same time you declare it. declaring a variable is useful in instances where you're using strings and you're going to end up formatting the string. Before we move on to modifying variables with operators, I have to warn you to never start a variable name with a number, use a letter as the first character of a variable. Now that that's out of the way we're moving on to operators and modifying variables.

new c = 5;
c = c*2;

Modifying a value is that easy, * is the multiplication symbol in pawn. This also demonstrates that you can use variables in math equations in pawn. You are free to use other math symbols such as + - /. One other thing to note is the use of parantheses can be used to do proper order of operations. Keep in mind you can only do simple math with pawn operators, natives are available in both pawn and SourcePawn to do operations such as absolute and square root. Now I'll show you a shorthand.

new d = 2;
d *= d;
d = 2;
d = d*d;

Both d = d*d and d *= d are the same. in the second bit of code d is multiplied by d and then set equal to the output. so if d is 2, then d is squared and equaled to 4. You can replace * with any other math operator that you saw before. Now I'll test your knowledge.

new f = 10;
f *= f(4)/f;

What is f now equal to? Scroll over this big box to see the answer.
Did you guess right?

Before we move on to our next subject I'm going to teach you about arrays. Arrays are metaphorical boxes; they're variables all stored within one variable each being indexed starting from zero and ending at one less than the array size. Let's look at an example of an array.

new box[5];

In the above example we created a new variable, box, with a size of 5. That means we metaphorically a box(variable box) with 5 indexed values inside of it that are all 0 right now. Now when I say that this has a size of 5, I mean that indexes 0 through 4 can hold values. 0 through n-1 are the accessible values. n is the size of the array in our case 5. Multiple ways of modifying what values an array holds.

//assume box[5] from above is in existance.
box = { 1, 2, 3, 4, 5}; //we defined indexes 0-4's values.
box = { 1, ...}; //entire array filled with 1. "..." copies the previous array value to all proceding indexes.
box[0] = 4;
box[1] *= 2; //simple math from before works the same on integer arrays.
box[2] += 4; //more simple math.
box[3] = box[1]; //you can set them with array variables as well.
//below is an example of invalid code.
new bad = 5;
new badbox[bad]; //this is invalid, bad would have to be a constant value. the below is correct.
const good = 5;
new goodbox[good]; //this is valid and correct.

Now that we're done with simple math operations, declaring, and arrays, it's time to move on to tags. Tags are ways of marking what type of new variable to create. Before we were only able to create integer variables. Now we're going to discuss creating new variable types such as Strings and Floats.

new Float:a = 1.0 //a floating point value, must have an integer before the decimal point. ".5" is invalid in pawn.
new bool:steak = false; // a boolean value. they use true and false as possible values which are really 1 for true and 0 for false. from what we've learned before a new bool value will default to false.
new Bool:bad = true; //tags are case sensetive, "Bool" is incorrect, so would "float"

Now you know how to define and declare boolean and floating point values. Now we're moving on to strings. Strings are arrays that hold ASCII characters to form text. There are a few rules about how to use them.

Note! Strings in pawn are arrays, in SourceMod we use the String tag.
new String:text[64]; //remember that you can not access index 64 but instead 63 is the last readable value.
new String:hello[] = "Hello"; //you can directly set a string's content by setting the array size as undefined like this.
//The above only works if you are modifying the value as you create it.
new String:hi[10];
hi = "Hi"; //Invalid
new String:list[5][] =
	"index 1",
	"index 2",
	"index 3",
	"index 4",
	"index 5"
}; //[] is the string size which is automatically set when we set the strings to a value.
//5 is the number of indexes in the array.

Well it seems I've covered the basics of variables and tags for now. I'll keep my blog updated with new stuff when I can.

To Top

My Blog

Monday October 24th, 2011, 2:35 PM CDT|Monday October 24, 2011, 2:35 PM GMT-6

Welcome to my blog. This is where I will be posting some developer-aimed style blog posts. Tips and tricks will definitely be a must and I will most likely tackle the task of creating a PAWN tutorial for people who want to learn.

To Top

A website by Gustavo Vargas
Register a .tk
Valid XHTML 1.0 Valid CSS3