Introduction to SourcePawn 1.7

From AlliedModders Wiki
Jump to: navigation, search

This guide is designed to give you a very basic overview to fundamentals of scripting in SourcePawn. Pawn is a "scripting" language used to embed functionality in other programs. That means it is not a standalone language, like C++ or Java, and its details will differ based on the application. SourcePawn is the version of Pawn used in SourceMod.

This guide does not tell you how to write SourceMod plugins; it is intended as an overview of the syntax and semantics of the language instead. Read the separate article, Introduction to SourceMod Plugins for SourceMod API specifics.

Non-Programmer Intro

This section is intended for non-programmers. If you're still confused, you may want to pick up a book on another language, such as PHP, Python, or Java, to get a better idea of what programming is like.

Symbols/Keywords

A symbol is a series of letters, numbers, and/or underscores, that uniquely represents something. Symbols are case-sensitive, and usually start with a letter.

There are a few reserved symbols that have special meaning. For example, if, for, and return are special constructs in the language that will explained later. They cannot be used as symbol names.

Variables

There a few important constructs you should know before you begin to script. The first is a variable. A variable is a symbol, or name, that holds data. For example, the variable "a" could hold the number "2", "16", "0", et cetera. Since a variable holds data, it also allocates the memory needed to store that data.

In addition to a name, variables have a type. A type tells the program how to interpret the data, and how much memory the data will use. Pawn has three types of data that are most commonly used:

  • Integers, using the int type. Integer types may store a whole number from -2147483648 to 2147483647.
  • Floats, using the float type. Float types may store fractional numbers in a huge range, though they are not as precise as integers.
  • Characters, using the char type. Character types store one byte of character information, typically an ASCII character.
  • Booleans, using the bool type. Booleans store either true or false.

Example of creating variables and assigning values:

int money = 5400;
float percent = 67.3;
bool enabled = false;

Functions

The next important concept is functions. Functions are symbols or names that perform an action. When you invoke, or call them, they carry out a specific sequence of code and then return a result. There are a few types of functions, but every function is activated the same way. Example:

show(56);     // Calls the "show" function, and gives it the number 56.
enable();     // Calls the "enable" function with no values.
bool visible = show(a);    //Calls the "show" function, stores its result in a variable.

Every piece of data passed to a function is called a parameter. A function can have any number of parameters (there is a "reasonable" limit of 32 in SourceMod). Parameters will be explained further in the article.

Comments

Note any text that appears after a "//" is considered a "comment" and is not actual code. There are two comment styles:

  • // - Double slash, everything following on that line is ignored.
  • /* */ - Multi-line comment, everything in between the asterisks is ignored. You cannot nest these.


Block Coding

The next concept is block coding. You can group code into "blocks" separated by { and }. This effectively makes one large block of code act as one statement. For example:

{
   here;
   is;
   some;
   code;
}

Block coding using braces is used everywhere in programming. Blocks of code can be nested within each other. It is a good idea to adapt a consistent and readable indentation style early on to prevent spaghetti-looking code.

Language Paradigms

Pawn may seem similar to other languages, like C, but it has fundamental differences. It is not important that you immediately understand these differences, but they may be helpful if you're familiar with another language already.

  • Pawn is sort of typed. Before SourceMod 1.7, Pawn did not have types. Older code and older natives will reflect this by using tags and the new keyword. As of SourceMod 1.7, we recommend that all code use types. For more information see SourcePawn Transitional Syntax.
  • Pawn is not garbage collected. Pawn, as a language, has no built-in memory allocation, and thus has no garbage. If a function allocates memory, you may be responsible for freeing it.
  • Pawn is not object oriented. Pawn does not have structs or objects. As of SourceMod 1.7, it has limited sugaring for treating some data types as objects, but users cannot create their own objects or classes.
  • Pawn is single-threaded. As of this writing, Pawn is not thread safe.
  • Pawn is compiled. Pawn is compiled to an intermediate, machine-independent code, which is stored in a ".smx" file. When loading .smx files, SourceMod translates this code to machine code for the platform and CPU it's running on.

Early language design decisions were made by ITB CompuPhase. It is designed for low-level embedded devices and is thus very small and very fast.

Variables

Pawn currently supports the following basic variable types:

  • bool - true or false.
  • char - an 8-bit ASCII character.
  • int - a 32-bit signed integer.
  • float - a 32-bit IEEE-754 floating point number.
  • Handle - the base type of a SourceMod object

Other types may exist when defined in include files - for example, enums create new types for named integers, and many types derive from Handle.

Strings, currently, are 0-terminated arrays of chars. They're described a little further ahead.

Declaration

Below we include some examples of variable declarations, both valid and invalid. Keep in mind that SourcePawn has recently added new syntax, and that's what's documented below. Older code may use older declaration syntax, which is no longer supported.

int a = 5;
float b = 5.0;
bool c = true;
bool d = false;

Invalid variable usage:

int a = 5.0;         // Type mismatch. 5.0 is a float.
float b = 5;         // Type mismatch. 5 is an integer.

If a variable is not assigned upon declaration, it will be set to 0. For example:

int a;        // Set to 0
float b;      // Set to 0.0
bool c;       // Set to false

Assignment

Variables can be re-assigned data after they are created. For example:

int a;
float b;
bool c;
 
a = 5;
b = 5.0;
c = true;

Arrays

An array is a sequence of data in a list. Arrays are useful for storing multiple pieces of data in one variable, or mapping one type of data to another.

Declaration

An array is declared using brackets. Some examples of arrays:

int players[32];     // Stores 32 integers.
float origin[3];     // Stores 3 floating point numbers

By default, arrays are initialized to 0. You can assign them different default values, however:

int numbers[5] = {1, 2, 3, 4, 5};       // Stores 1, 2, 3, 4, 5.
float origin[3] = {1.0, 2.0, 3.0};      // Stores 1.0, 2.0, 3.0.

You can leave out the array size if you're going to pre-assign data to it. For example:

int numbers[] = {1, 3, 5, 7, 9};

The compiler will automatically deduce that you intended an array of size 5.

When array is declared with brackets after its name, Pawn considers that array to have a fixed size. The size of a fixed-size array is always known. Some arrays can be dynamically sized, by putting the brackets before the name. For example,

int[] numbers = new int[MaxClients]

This creates an array of size MaxClients, which could be anything, so the size of the array is not known until the array is allocated.

Usage

Using an array is just like using a normal variable. The only difference is the array must be indexed. Indexing an array means choosing the element which you wish to use.

For example, here is an example of the above code using indexes:

int numbers[5];
float origin[3];
 
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
origin[0] = 1.0;
origin[1] = 2.0;
origin[2] = 3.0;

Note that the index is what's in between the brackets. The index always starts from 0. That is, if an array has N elements, its valid indexes are from 0 to N-1. Accessing the data at these indexes works like a normal variable.

Using an incorrect index will cause an error. For example:

int numbers[5];
 
numbers[5] = 20;

This may look correct, but 5 is not a valid index. The highest valid index is 4.

You can use any expression as an index. For example:

int a, numbers[5];
 
a = 1;                   // Set a = 1
numbers[a] = 4;          // Set numbers[1] = 4
numbers[numbers[a]] = 2; // Set numbers[4] = 2

Expressions will be discussed in depth later in the article.

Strings

Strings are a construct for storing text (or even raw binary data). A string is just an array of characters, except that the final character must be 0 (called the null terminator). Without a null terminator, Pawn would not know where to stop reading the string. All strings are UTF-8 in SourcePawn.

In general, you must have an idea of how large a string will be before you store it. SourcePawn does not yet have the capability of pre-determining storage space for strings.

Usage

Strings are usually declared as arrays. For example:

char message[] = "Hello!";
char clams[6] = "Clams";

These are equivalent to doing:

char message[7];
char clams[6];
 
message[0] = 'H';
message[1] = 'e';
message[2] = 'l';
message[3] = 'l';
message[4] = 'o';
message[5] = '!';
message[6] = 0;
clams[0] = 'C';
clams[1] = 'l';
clams[2] = 'a';
clams[3] = 'm';
clams[4] = 's';
clams[5] = 0;

Although strings are rarely initialized in this manner, it is very important to remember the concept of the null terminator, which signals the end of a string. The compiler, and most SourceMod functions will automatically null-terminate for you, so it is mainly important when manipulating strings directly.

Note that a string is enclosed in double-quotes, but a character is enclosed in single quotes.

Characters

A character of text can be used in either a String or a cell. For example:

char text[] = "Crab";
char clam;
 
clam = 'D';         //Set clam to 'D'
text[0] = 'A';      //Change the 'C' to 'A', it is now 'Arab'
clam = text[0];     //Set clam to 'A'
text[1] = clam;     //Change the 'r' to 'A', is is now 'AAab'

What you can't do is mix character arrays with strings. The internal storage is different. For example:

int clams[] = "Clams";                       // Invalid.
int clams[] = {'C', 'l', 'a', 'm', 's', 0};  // Valid, but NOT A STRING.

Concatenation

String literals can be concatenated with the ... operator:

char text[] = "This is a really long string of text should be "
     ... "split over multiple lines for the sake of readability."; // Valid.
 
char prefix[] = "What you can't do, however, ";
char moreText[] = prefix ... "is concatenate using a non-literal string."; // Invalid, not a constant expression.

Functions

Functions, as stated before, are isolated blocks of code that perform an action. They can be invoked, or called, with parameters that give specific options.

There are two types of ways functions are called:

  • direct call - You specifically call a function in your code.
  • callback - The application calls a function in your code, as if it were an event trigger.

There are six types of functions:

  • native: A direct, internal function provided by the application.
  • public: A callback function that is visible to the application and other scripts.
  • normal: A normal function that only you can call.
  • static: The scope of this function is restricted to the current file, can be used in combination with stock.
  • stock: A normal function provided by an include file. If unused, it won't be compiled.
  • forward: This function is a global event provided by the application. If you implement it, it will be a callback.

All code in Pawn must exist in functions. This is in contrast to languages like PHP, Perl, and Python which let you write global code. That is because Pawn is a callback-based language: it responds to actions from a parent application, and functions must be written to handle those actions. Although our examples often contain free-floating code, this is purely for demonstration purposes. Free-floating code in our examples implies the code is part of some function.

Declaration

Unlike variables, functions do not need to be declared before you use them. Functions have two pieces, the signature and the body. The signature contains the name of your function and the parameters it will accept. The body is the contents of its code.

Example of a function:

int AddTwoNumbers(int first, int second)
{
  int sum = first + second;
  return sum;
}

This is a simple function. The prototype is this line:

int AddTwoNumbers(int first, int second)

Broken down, it means:

  • int - Return value type (integer).
  • AddTwoNumbers - Name of the function.
  • int first - First parameter, an integer.
  • int second - Second parameter, an integer.

The body is a block of code. It creates a new variable, called sum, and assigns it the value of the two parameters added together (more on expressions later). The important thing to notice is the return statement, which tells the function to end and return a value to the caller of the function. All functions return something on completion, unless they return a special type called void.

A function can accept any type of input, and it can return any non-array type.

You can, of course, pass variables to functions:

int numbers[3] = {1, 2, 0};
 
numbers[2] = AddTwoNumbers(numbers[0], numbers[1]);

Note that cells are passed by value. That is, their value cannot be changed by the function. For example:

int a = 5;
 
ChangeValue(a);
 
ChangeValue(b)
{
   b = 5;
}

This code would not change the value of a. That is because a copy of the value in a is passed instead of a itself.

More examples of functions will be provided throughout the article.

Publics

Public functions are used to implement callbacks. You should not create a public function unless it is specifically implementing a callback. For example, here are two callbacks from sourcemod.inc:

forward void OnPluginStart();
forward void OnClientDisconnected(int client);

To implement and receive these two events, you would write functions as such:

public void OnPluginStart()
{
   /* Code here */
}
 
public void OnClientDisconnected(int client)
{
   /* Code here */
}

The public keyword signals to the parent application that it should attach the function to the appropriate forwarded event.

Natives

Natives are builtin functions provided by SourceMod. You can call them as if they were a normal function. For example, SourceMod has the following function:

native float FloatRound(float num);

It can be called like so:

int rounded = FloatRound(5.2);     // rounded will be 5

Array Parameters

You can pass arrays or Strings as parameters. It is important to note that these are passed by reference. That is, rather than making a copy of the data, the data is referenced directly. There is a simple way of explaining this more concretely.

int example[] = {1, 2, 3, 4, 5};
 
ChangeArray(example, 2, 29);
 
void ChangeArray(int[] array, int index, int value)
{
   array[index] = value;
}

The function sets the given index in the array to a given value. When it is run on our example array, it changes index 2 to from the value 3 to 29. I.e.:

example[2] = 29;

Note two important things here. First, arrays are not copied when they are passed to functions - they are passed by reference, so the view of the array is consistent at all times. Second, the brackets changed position in our function signature. This is because our function accepts an array of any size, and since we don't know the size, we must use the dynamic array syntax.

To prevent an array from being modified in a function, you can mark it as const. This will raise an error on code that attempts to modify it. For example:

void CantChangeArray(const array[], int index, int value)
{
   array[index] = value;    //Won't compile
}

It is a good idea to use const in array parameters if you know the array won't be modified; this can prevent coding mistakes.

Expressions

Expressions are exactly the same as they are in mathematics. They are groups of operators/symbols which evaluate to one piece of data. They are often parenthetical (comprised of parenthesis). They contain a strict "order of operations." They can contain variables, functions, numbers, and expressions themselves can be nested inside other expressions, or even passed as parameters.

The simplest expression is a single number. For example:

0;   //Returns the number 0
(0); //Returns the number 0 as well

Although expressions can return any value, they are also said to either return zero or non-zero. In that sense, zero is false, and non-zero is true. For example, -1 is true in Pawn, since it is non-zero. Do not assume negative numbers are false.

The order of operations for expressions is similar to C. PMDAS: Parenthesis, Multiplication, Division, Addition, Subtraction. Here are some example expressions:

5 + 6;                   //Evaluates to 11
5 * 6 + 3;               //Evaluates to 33
5 * (6 + 3);             //Evaluates to 45
5.0 + 2.3;               //Evaluates to 7.3
(5 * 6) % 7;             //Modulo operator, evaluates to 2
(5 + 3) / 2 * 4 - 9;     //Evaluates to -8

As noted, expressions can contain variables, or even functions:

int a = 5 * 6;
int b = a * 3;      //Evaluates to 90
int c = AddTwoNumbers(a, b) + (a * b);

Note: String manipulation routines may be found in the string.inc file located in the include subdirectory. They may be browsed through the API Reference as well.

Operators

There are a few extra helpful operators in Pawn. The first set simplifies self-aggregation expressions. For example:

int a = 5;
 
a = a + 5;

Can be rewritten as:

int a = 5;
a += 5;

This is true of the following operators in Pawn:

  • Four-function: *, /, -, +
  • Bit-wise: |, &, ^, ~, <<, >>

Additionally, there are increment/decrement operators:

a = a + 1;
a = a - 1;

Can be simplified as:

a++;
a--;

As an advanced note, the ++ or -- can come before the variable (pre-increment, pre-decrement) or after the variable (post-increment, post-decrement). The difference is in how the rest of the expression containing them sees their result.

  • Pre: The variable is incremented before evaluation, and the rest of the expression sees the new value.
  • Post: The variable is incremented after evaluation, and the rest of the expression sees the old value.

In other words, a++ evaluates to the value of a while ++a evaluates to the value of a + 1. In both cases a is incremented by 1.

For example:

int a = 5;
int b = a++;   // b = 5, a = 6  (1)
int c = ++a;   // a = 7, c = 7  (2)

In (1) b is assigned a's old value before it is incremented to 6, but in (2) c is assigned a's int value after it is incremented to 7.

Comparison Operators

There are six operators for comparing two values numerically, and the result is either true (non-zero) or false (zero):

  • a == b - True if a and b have the same value.
  • a != b - True if a and b have different values.
  • a > b - True if a is greater than b
  • a >= b - True if a is greater than or equal to b
  • a < b - True if a is less than b
  • a <= b - True if a is less than or equal to b

For example:

(1 != 3);         //Evaluates to true because 1 is not equal to 3.
(3 + 3 == 6);     //Evaluates to true because 3+3 is 6.
(5 - 2 >= 4);     //Evaluates to false because 3 is less than 4.

Note that these operators do not work on arrays or strings. That is, you cannot compare either using ==.

Truth Operators

These truth values can be combined using three boolean operators:

  • a && b - True if both a and b are true. False if a or b (or both) is false.
&& 0 1
0 0 0
1 0 1
  • a || b - True if a or b (or both) is true. False if both a and b are false.
|| 0 1
0 0 1
1 1 1
  • !a - True if a is false. False if a is true.
! 0 1
1 0

For example:

(1 || 0);         //Evaluates to true because the expression 1 is true
(1 && 0);         //Evaluates to false because the expression 0 is false
(!1 || 0);        //Evaluates to false because !1 is false.

Left/Right Values

Two important concepts are left-hand and right-hand values, or l-values and r-values. An l-value is what appears on the left-hand side of a variable assignment, and an r-value is what appears on the right side of a variable assignment.

For example:

int a = 5;

In this example a is an l-value and 5 is an r-value.

The rules:

  • Expressions are never l-values.
  • Variables are both l-values and r-values.

Conditionals

Conditional statements let you only run code if a certain condition is matched.

If Statements

If statements test one or more conditions. For example:

if (a == 5)
{
   /* Code that will run if the expression was true */
}

They can be extended to handle more cases as well:

if (a == 5)
{
   /* Code */
}
else if (a == 6)
{
   /* Code  */
}
else if (a == 7)
{
   /* Code */
}

You can also handle the case of no expression being matched. For example:

if (a == 5)
{
   /* Code */
}
else
{
   /* Code that will run if no expressions were true */
}

Switch Statements

Switch statements are restricted if statements. They test one expression for a series of possible values. For example:

switch (a)
{
   case 5:
   {
      /* code */
   }
   case 6:
   {
      /* code */
   }
   case 7:
   {
      /* code */
   }
   case 8, 9, 10:
   {
      /* Code */
   }
   default:
   {
      /* will run if no case matched */
   }
}

Unlike some other languages, switches are not fall-through. That is, multiple cases will never be run. When a case matches its code is executed, and the switch is then immediately terminated.

Loops

Loops allow you to conveniently repeat a block of code while a given condition remains true.

For Loops

For loops are loops which have four parts:

  • The initialization statement - run once before the first loop.
  • The condition statement - checks whether the next loop should run, including the first one. The loop terminates when this expression evaluates to false.
  • The iteration statement - run after each loop.
  • The body block - run each time the condition statement evaluates to true.
for ( /* initialization */ ; /* condition */ ; /* iteration */ )
{
   /* body */
}

A simple example is a function to sum an array:

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = SumArray(array, 10);
 
int SumArray(const int array[], int count)
{
   int total;
 
   for (int i = 0; i < count; i++)
   {
      total += array[i];
   }
 
   return total;
}

Broken down:

  • int i = 0 - Creates a new variable for the loop, sets it to 0.
  • i < count - Only runs the loop if i is less than count. This ensures that the loop stops reading at a certain point. In this case, we don't want to read invalid indexes in the array.
  • i++ - Increments i by one after each loop. This ensures that the loop doesn't run forever; eventually i will become too big and the loop will end.

Thus, the SumArray function will loop through each valid index of the array, each time adding that value of the array into a sum. For loops are very common for processing arrays like this.

While Loops

While loops are less common than for loops but are actually the simplest possible loop. They have only two parts:

  • The condition statement - checked before each loop. The loop terminates when it evaluates to false.
  • The body block - run each time through the loop.
while ( /* condition */ )
{
   /* body */
}

As long as the condition expression remains true, the loop will continue. Every for loop can be rewritten as a while loop:

/* initialization */
while ( /* condition */ )
{
   /* body */
   /* iteration */
}

Here is the previous for loop rewritten as a while loop:

int SumArray(const int array[], int count)
{
   int total, i;
 
   while (i < count)
   {
      total += array[i];
      i++;
   }
 
   return total;
}

There are also do...while loops which are even less common. These are the same as while loops except the condition check is AFTER each loop, rather than before. This means the loop is always run at least once. For example:

do
{
   /* body */
}
while ( /* condition */ );

Loop Control

There are two cases in which you want to selectively control a loop:

  • skipping one iteration of the loop but continuing as normal, or;
  • breaking the loop entirely before it's finished.

Let's say you have a function which takes in an array and searches for a matching number. You want it to stop once the number is found:

/**
 * Returns the array index where the value is, or -1 if not found.
 */
int SearchInArray(const int array[], int count, int value)
{
   int index = -1;
 
   for (int i = 0; i < count; i++)
   {
      if (array[i] == value)
      {
         index = i;
         break;
      }
   }
 
   return index;
}

Certainly, this function could simply return i instead, but the example shows how break will terminate the loop.

Similarly, the continue keyword skips an iteration of a loop. For example, let's say we wanted to sum all even numbers:

int SumEvenNumbers(const int array[], int count)
{
   int sum;
 
   for (int i = 0; i < count; i++)
   {
      /* If divisibility by 2 is 1, we know it's odd */
      if (array[i] % 2 == 1)
      {
         /* Skip the rest of this loop iteration */
         continue;
      }
      sum += array[i];
   }
 
   return sum;
}

Scope

Scope refers to the visibility of code. That is, code at one level may not be "visible" to code at another level. For example:

int A, B, C;
 
void Function1()
{
   int B;
 
   Function2();
}
 
void Function2()
{
   int C;
}

In this example, A, B, and C exist at global scope. They can be seen by any function. However, the B in Function1 is not the same variable as the B at the global level. Instead, it is at local scope, and is thus a local variable.

Similarly, Function1 and Function2 know nothing about each other's variables.

Not only is the variable private to Function1, but it is re-created each time the function is invoked. Imagine this:

void Function1()
{
   int B;
 
   Function1();
}

In the above example, Function1 calls itself. Of course, this is infinite recursion (a bad thing), but the idea is that each time the function runs, there is a new copy of B. When the function ends, B is destroyed, and the value is lost.

This property can be simplified by saying that a variable's scope is equal to the nesting level it is in. That is, a variable at global scope is visible globally to all functions. A variable at local scope is visible to all code blocks "beneath" its nesting level. For example:

void Function1()
{
   int A;
 
   if (A)
   {
      A = 5;
   }
}

The above code is valid since A's scope extends throughout the function. The following code, however, is not valid:

void Function1()
{
   int A;
 
   if (A)
   {
      int B = 5;
   }
 
   B = 5;
}

Notice that B is declared in a new code block. That means B is only accessible to that code block (and all sub-blocks nested within). As soon as the code block terminates, B is no longer valid.

Dynamic Arrays

Dynamic arrays are arrays which don't have a hardcoded size. For example:

void Function1(int size)
{
   int array[size];
 
   /* Code */
}

Dynamic arrays can have any expression as their size as long as the expression evaluates to a number larger than 0. Like normal arrays, SourcePawn does not know the array size after it is created; you have to save it if you want it later.

Dynamic arrays are only valid at the local scope level, since code cannot exist globally.

Extended Variable Declarations

Variables can be declared in more ways than simply int float or char.

static

The static keyword is available at global and local scope. It has different meanings in each.

Global static

A global static variable can only be accessed from within the same file. For example:

//file1.inc
static float g_value1 = 0.15f;
 
//file2.inc
static float g_value2 = 0.15f;

If a plugin includes both of these files, it will not be able to use either g_value1 or g_value2. This is a simple information hiding mechanism, and is similar to declaring member variables as private in languages like C++, Java, or C#.

Local static

A local static variable is a global variable that is only visible from its local lexical scope. For example:

int MyFunction(int inc)
{
   static int counter = -1;
 
   counter += inc;
 
   return counter;
}

In this example, counter is technically a global variable -- it is initialized once to -1 and is never initialized again. It does not exist on the stack. That means each time MyFunction runs, the counter variable and its storage in memory is the same.

Take this example:

MyFunction(5);
MyFunction(6);
MyFunction(10);

In this example, counter will be -1 + 5 + 6 + 10, or 20, because it persists beyond the frame of the function. Note this may pose problems for recursive functions: if your function may be recursive, then static is usually not a good idea unless your code is re-entrant.

The benefit of a local static variable is that you don't have to clutter your script with global variables. As long as the variable doesn't need to be read by another function, you can squirrel it inside the function and its persistence will be guaranteed.

Note that statics can exist in any local scope:

int MyFunction(int inc)
{
   if (inc > 0)
   {
      static int counter;
      return (counter += inc);
   }
   return -1;
}
Warning: This template (and by extension, language format) should not be used, any pages using it should be switched to Template:Languages

View this page in:  English  Russian  简体中文(Simplified Chinese)