Part I
Concepts of Pawn
Welcome to the absolute beginner's guide to Pawn! Before you can get into the nooks and crannies of Pawn, first we have to learn about the concepts of Pawn and programming in General.
IntroductionPawn is an embeddable programming language and has a C-like syntax. Pawn was designed as an embeddable programming language, and is now used by projects like AMX Mod X, SA:MP, and, of course, VC:MP. Pawn scripts need to be
compiled, or translate source code into computer-readable instructions. Pawn scripts have a structured flow, which means instructions are run one after another, but control statements can change what instructions come next.
The CompilerThe compiler translates source code into computer-readable code which will get run by the server. To use the compiler, you can download Pawno from the
SA:MP server package, then go to Build > Compile. I recommend you use Pawno for this tutorial series so you can compile your test scripts and get compiler feedback if anything's wrong with the code.
Basic Program StructureA very basic "Hello World" Pawn script would look like this:
main()
{
print("Hello World!");
}
The
main() function is the entry-point for the script, or where the program starts. The
print() function writes a line of text in the console. The
main() function doesn't concern us much in VC:MP scripting, but is good for printing script copyrights.
Note how the end of the Hello World line ends with a semicolon (;), but the rest of the code does not. That is because semicolons define where
commands end, whereas control flow contains commands.
VariablesVariables are placeholders for information that a script can use, and can be changed at any time by the script. Variables are given names so we can not only get the variable's value, but so that we can also change it later if we need to. Variables can be assigned
types so that their values can only be of a given type. Some common types are:
- Integer: Sometimes represented as int, this is any integer or "whole" number.
- Float: Stores decimals and any real number
- Character: Or char, stores a letter, number, or punctuation. Arrays of characters create strings.
The only type that needs to be explicitly declared is a float. Otherwise, Pawn will assume that a variable is an integer, and that arrays are either strings or arrays of integers. However, variables still need to be declared at some point in the script. A variable declaration consists of the word
new, followed by the name of the variable, and a semicolon. For example:
- new myInt; defines a new integral variable named "myInt"
- new Float:myFloat; defines a new floating point number named "myFloat" -- note the Type: prefix.
BooleanBoolean algebra is when a mathematical expression is evaluated as either
true or
false. Therefore, a boolean value can only have one of two values: true or false. However, it evaluates entire expressions. Let's take a few expressions into consideration.
If I were to say 3 < (is less than) 6, obviously the statement is
true.
If I were to say I am 5' 9" tall, this statement is also true.
However, if I were to say I am 5' 9" tall
AND I am President of the United States, boolean logic states that the ENTIRE expression is false. While it may be true that I am 5' 9", I am not the President. Therefore, the entire expression is false.
A similar sentence: I am 5' 9" tall
OR I am President of the United States.
Since only one part of the sentence has to be true, the entire sentence is considered true.
Note0 is considered false, and 1 is considered true.
Boolean OperatorsThere are three boolean operators:
AND,
OR, and
NOT. In Pawn:
- AND is written as &&
- OR is written as ||
- NOT is written as !
Boolean operators are evaluated like so. For
AND, the combination of two "True" values results in "True" while two "False" values results in "True." Any other combinations returns False. For example:
True AND True is true.
True AND False is false.
False AND True is false.
False AND False is true.
For
OR, as long as one value is true, the entire statement is true.
True OR True is true.
True OR False is true.
False OR False is false.
False OR True is true.
The
AND operator negates the value of an expression.
NOT True is false.
NOT False is true.
Comparison OperatorsBoolean expressions most often involve comparison operators to see if they are true or false. These operators are equal to, greater than (or equal to), less than (or equal to), and not equal. In Pawn:
- Equal to is written as ==
- Less than is written as <
- Greater than is written as >
- Less than or equal to is written as <=
- Greater than or equal to is written as >=
- Inequality is written as !=
Comparison operators form expressions that can be evaluated as true or false. For example,
Number_of_Students > 30. IF there are more than 30 students in the class, the entire expression is true. Otherwise, it is false.
Combining Boolean and Comparison OperatorsWe've looked at how boolean and comparison operators work to form expressions. Now, we'll look at how we can combine them to form more complex expressions.
If we wanted to know if there were more than 30 students in a class and if there were more than 30 seats, we could express this as:
Number_of_Students > 30 && Number_of_Seats > 30Pawn has to determine the boolean (true/false) value of each expression, then use those values to evaluate the expression as a whole. Let's say we declare two new variables:
new Number_of_Students = 35;
new Number_of_Seats = 38;
Pawn will break this down into
35 > 30 && 38 > 30, which then evaluates to
TRUE && TRUE. Therefore, the entire expression is true. The following example uses the following assumptions:
- All employees work in a department.
- All employees in department 5 have salaries greater than $25,000
- Alice is an employee in department 5
Consider the truth of these few expressions:
Alice_salary < 25000 && Alice_department == 5Alice_salary < 25000 is False.
Alice_department == 5 is true.
False && True is false.
!Alice_salary < 25000Alice_salary < 25000 is false.
NOT FALSE is true.
Appendix I: Mathematical OperatorsPawn offers mathematical operators as well if you wish to do math with scripts, which will come in handy a lot.
Note that "var" is shorthand for variable.
var + var - Addition
var - var - Subtraction
var * var - Multiplication
var / var - Division
var ^ power - Exponentiation
dividend % divisor - Modulus
variable += number - Adds
number to
variable and saves in
variablevariable *= number - Multiplies
number to
variable and saves in
variablevariable -= number - Subtracts
number from
variable and saves in
variablevariable /= number - Divides
number into
variable and saves in
variableComments allow you to make notes in your code to explain to people who might view it, including yourself, what a piece of code does. Comments can be one line long: one-line comments start with
// and continue until the line ends. Multi-line comments begin with
/* and end with
*/ and last between the two markers. Anything that is commented is NOT run or compiled.
Examples:
// One-line long comment/* Multi
* line
* comment
*/