Professional Documents
Culture Documents
From A Little Clucker To A Spam Machine
From A Little Clucker To A Spam Machine
SIMP
From a Little-Clucker to a
Spam-Machine
Copyright © 2020 by Mr. Simp
First edition
Some parts of this book are written assuming that the reader is familiar with
SA-MP and if you want to follow along practically, you should know the basic
concepts like setting up a local SA-MP server for loading up and running
scripts. There are countless resources, suggestively the official SA-MP Wiki
which can teach you the process. If you want to only give a quick read to the
book, it will still be beneficial for you since it will cover most of the concepts
that are common in computer languages.
1
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
2
INTRODUCTION
A coding beginner does not necessarily need to know the details of program
compilation, but having a rough idea of this concept does help on the
long run. The PAWN compiler takes in the source file, combines it with
other dependencies and creates a file with (.amx) extension which contains
‘bytecode’ also called pseudo-code (P-Code). When the program or script
is required to be executed, the P-Code (.amx) file is passed into an abstract
machine and executed. An abstract machine in simplest terms a virtual model
of a computer system.
One of the applications in which the Pawn language has been most extensively
used is the multiplayer modification of Gta San Andreas called San Andreas
Multiplayer or SA-MP in short. Therefore, I decided to use the SA-MP
development environment for writing the example code of this book and I
expect you to follow along in the same way. The Pawn Compiler and an IDE
known as ‘Pawno’ is provided along with the SA-MP server resources from
the official site. If you open up the IDE which can be found in the ‘pawno’
folder in the server package, the window should look something like this :
3
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
One neat thing about this IDE is that it’s very lightweight and almost perfect
for writing PAWN code since it was specially made for this purpose. All
the code in this book can be easily written and compiled using this piece of
software.
Hello World
The process of writing, compiling and running the scripts is very quick and
simple which makes SA-MP development more fun. It is explained briefly in
this topic, while there are countless other sources online which explain the
same process in much more detail.
Navigate to the top-left corner of the IDE window and click the ‘New’ icon
which will create a new script and in-turn some code will appear in the
window. Delete all the code and write the follow instead :
#include <a_samp>
4
INTRODUCTION
public OnFilterScriptInit ()
{
print ("Hello World");
return 1;
}
Save the file as a filterscript and compile it using the F5 key or the ‘Compile’
icon from the toolbar. Once compiled, run the server and load the filterscript.
It should show the following output in the server console :
Hello World
This was a very simple and the traditional ‘Hello World’ program every
programmer writes while learning to code. If you are following along
practically, you can experiment by trying to change the output text which
will make you more familiar with the code.
5
The Pawn Language | Fundamentals
Comments
The comments in a programming language are lines which are completely
ignored by the compiler and hence have no effect on the program. Oddly
enough, comments are one of the most useful tools in any programming
language. Their importance will become more apparent as you progress in
your programming career. The code from the first chapter can be reused for
this example.
6
THE PAWN LANGUAGE | FUNDAMENTALS
#include <a_samp>
public OnFilterScriptInit ()
{
// print ("Hello World");
return 1;
}
If you copy-paste the above code in the IDE, compile and run it. This time
it will not show anything in the console. If you have not already noticed
the change in the code, it is the addition of double-slash symbols before
the ‘print’ statement which basically commented out all the letters following
the slashes, in that specific line. The ‘print’ statement was responsible for
‘printing’ or showing that “Hello World” text in the server console and since it is
now commented out, the compiler simply ignores that line and consequently
nothing happens as the script is executed.
The preceding was an example of a single-line comment. The code in
programming often exceeds hundreds and even thousands of lines and if
you want to comment out a specific part of such a script, it can get quite
troublesome and not-so-efficient if the single-line comments are used for
that purpose. For such purposes, there is another type known as multi-line
comments. The syntax for a multi-line comment is the following :
#include <a_samp>
7
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
public OnFilterScriptInit ()
{
print ("This");
print ("is");
print ("a");
/*
print ("commented");
print ("out");
*/
print ("statement.");
return 1;
}
Output
This
is
a
statement.
If you look at the code, there are six ‘print’ statements but four of them
are executed while two are skipped, which is because those two statements
are enclosed in the multi-line comment operators (/*) and (*/) and hence,
commented out of the program. Multi-line comments can also be used for
commenting out single lines but it won’t make much sense unless you want to
comment out a specific part of that line which can be useful in some situations.
Following is an arbitrary example of commenting out part of a statement
(which you don’t necessarily need to understand or practice at this level) :
8
THE PAWN LANGUAGE | FUNDAMENTALS
Output
Printed
Other than disabling parts of code, comments can be very useful for leaving
out messages in the source code for other people. Often comments are used
for explaining what is the function of a specific part of the code which can
help fellow programmers working on the same script, or it can be used to
leave some notes or reminders for yourself in the script. Generally, it is up to
you how you use them.
Basic Symbols
Before studying more about the Operators, you should know the meaning of
some important symbols that do not fall in the category of Operators. These
include the braces, parentheses, single quotes, double quotes and semi-colon.
This will give you more familiarity with the code and in turn help you make
more sense of it. Eventually you will develop the intuition and will be able to
read the code easily, just like a natural language and understand the meaning.
9
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
Braces
The basic ‘hello world’ code will be used in the following example to keep the
things simple.
#include <a_samp>
public OnFilterScriptInit ()
{
print ("Hello World");
return 1;
}
If you observe the code above, you will notice there are two (curly) braces
used in the script. The print and the return statements appear to be ‘enclosed’
between the two braces. The first bracket is called opening-brace ({) , and the
second bracket is called closing-brace (}). Everything between each such pair
of braces is considered a ‘block of code’. At this point, all you need to know is
that the block of code (containing ‘print’ and ‘return’) belongs to the preceding
expression or statement and in this case :
public OnFilterScriptInit ()
This relation will become more apparent as you study the properties of
functions in later topics.
Parentheses
The syntactic use of parentheses is similar to the braces but they both imply
different meanings. While braces are used for enclosing a collection of
statements, the parentheses are used to enclose data or others requirements
that are needed by that statement. For example, the print function takes in
some text input so we use the brackets to enclose that input data (the double
10
THE PAWN LANGUAGE | FUNDAMENTALS
A brief definition of a function is given in the preceding example but they will be covered in detail later in this chapter.
public OnFilterScriptInit ()
Semi-colon
One of the single most used operators in Pawn language is the semi-colon
operator and a lot of other languages share the same property. You can think
of the semi-colon (;) as a dot symbol which indicates the end of a sentence.
The semi-colon operator simply indicates to the compiler about the end of
a statement. For-example if you notice, in the code presented in preceding
examples, the print statement(s) have a semi-colon in the end which means
the it’s the end of that statement.
11
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
// Wrong
print (Hello World);
// Correct
print ("Hello World");
12
THE PAWN LANGUAGE | FUNDAMENTALS
Revision
Now I would like you to take a look at the following code in the context of
the previous explanations.
#include <a_samp>
public OnFilterScriptInit ()
{
print ("Hello World");
return 1;
}
Operators
• Assignment Operators ( = , += , -= , *= , /= )
• Arithmetic Operators ( + , - , * , / , ^ )
• Relational Operators ( < , > , == , <= , >= )
• Logical Operators ( ! , || , && )
• Ternary Operator(s) ( condition ? True : False ; )
•
13
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
It’s not a small topic and since the explanations of each operator’s functioning
is dependent upon the programming concepts which you presumably have
not acquired at the moment, hence I will be explaining parts of it at relevant
places in later topics throughout the book.
String Formatting
The method of inserting changeable or variable data in a string is called string
formatting. Formatting a string means adding ‘format specifiers’ which are
symbols that are replaced by some data which may be text or values, therefore
there are different format specifiers for each datatype. Till now we have been
using the print function to output a string (text) in the console but there is an
advanced version of it called ‘printf’. The general usage of printf function is
similar to that of print but it adds the ability of formatting the input string.
The syntactical difference between print and printf is the following :
print (main_string);
The printf function takes in the first argument as the main string which may
or may not contain format specifiers, while the following arguments to that
(if any) contain the data that is to replace the respective format specifiers in
the main string. Here is a practical example of the difference between print
and printf function :
14
THE PAWN LANGUAGE | FUNDAMENTALS
If the above two statements are used under the OnFilterScriptInit function,
both will print the same text in console when the script is loaded. In the
printf statement, the main string contains a format specifier for integer data
(%d). The data in first optional argument replaces the first appearing format
specifier in the main string which is %d when the statement is executed during
the runtime. Following is a list of format specifiers in Pawn :
From the above table, we know that string data uses the specifier %s. We can
use that in an example :
15
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
The first formatting argument “Mouiz” replaces the first format specifier %s
while the second one 10 replaces the second specifier %i. It is important to
note that the format specifiers are replaced according the to order in which
the formatting arguments are included, which means that if we switch the
positions of ‘Mouiz’ and 10, it will show some unexpected results :
Which implies that the string formatting won’t work properly unless you use
the correct format specifiers according to the respective arguments’ datatypes
and vice versa.
Since the compiler treats certain characters in the string (like format
specifiers) differently than normal letters and numbers, there are a few
exceptions to the string data. The most common mistake alot of beginners
make is, adding double quotes inside a string.
You might have concluded that printf just has a fancy syntax since the output
is the same but there is more to it. Formatted strings are in simple words are
16
THE PAWN LANGUAGE | FUNDAMENTALS
Variables
One of the most important concepts in programming is the concept of
‘variables’. In programming, a variable is an entity that is changeable, but in
terms of what ? In Pawn language, a variable holds a value at any time and
that value-as the name suggests-is ‘variable’ or ‘changeable’.
The reason why variables are so important is because they are basically small
units of computer memory which can hold or ‘remember’ different values
while the program is under execution (running), and that property turns out
to be very useful in programming. For example, you want to keep track of
the scores of 100 players in a game, you can do it easily by programming the
computer to store (remember) and keep the values updated (there is a more
efficient method for storing scores called ‘Arrays’, a later topic in the book).
Later if you want to find the mean score of those players or want to create a
leader board, those values from the variables can be easily accessed and used.
For using a variable, first we need to declare it using a keyword plus a name
for it. Following is the method of syntax of declaring a variable :
new my_variable ;
17
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
statement.
new var;
new ammo;
new score;
new vehicles;
new top_score;
Each of the above defined variable has a value by default, which is zero. There
are different ways of assigning values to a variable. One method is directly
assigning a value to the variable as it’s declared :
In the above example, a variable named ‘letters’ is being declared, with a value
of 25. You will notice an equal sign which is a simple Assignment Operator
that can be used for assigning values to variables. It evaluates the expression
on its right and assigns the resultant value to the variable referenced on its
left side. Other than assigning values directly at the declaration, you can also
do it in later parts of the code :
new letters;
letters = 25;
But this can be done only if the part of the code where you’re referencing the
variable is within the scope of that variable. Scope of a variable depends upon
the code block or position where that variable was declared. For example a
variable being declared outside any block of code, usually in the beginning of
18
THE PAWN LANGUAGE | FUNDAMENTALS
the script, has a ‘Global’ scope and can be accessed from anywhere within the
script:
#include <a_samp>
new example_var = 5;
public OnFilterScriptInit ()
{
example_var = 10;
return 1;
}
return 1;
}
// Output :
// The value is 10
// The value is 100
In the above example, a public function (callback) has been introduced which
executes the code in it’s code block only when a player connects to the server.
Public Functions or more appropriately ‘Callbacks’ are functions or parts
of codes that are executed by the server on specific occasions line when a
player connects, shoots, moves, disconnects etc. Hence, they can be treated
as ‘events’.
19
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
Other than ‘Global’ (scoped) variables, there are ‘local’ or ‘private’ variables
that can be accessed only from inside the block of code where they were
declared. For-example :
#include <a_samp>
public OnFilterScriptInit ()
{
new local_var;
local_var = 5;
return 1;
}
return 1;
}
If you try to compile the code above, the compiler will show an error which
is reasonable as a local variable is being references in a completely different
block of code.
Note: If it is a nested code block then the variable can be accessed from
there.
One important thing to note is that you cannot declare variables with the
same names if their scopes intercede. For example if you already have a
variable named ‘score’ on a global scope, you cannot create another variable
named ‘score’ on the global scope as well as a local one, and this is true for
other way around as well (if you already have a local variable, avoid declaring
a global variable with the same name).
20
THE PAWN LANGUAGE | FUNDAMENTALS
#include <a_samp>
new score;
public OnFilterScriptInit ()
{
new score = 5; // This line will show an error.
return 1;
}
Now that you know how to declare variables, you need to know the naming
rules for declaring variable which are listed below :
Examples:
After that, now lets look at some examples of what types of data can be stored
21
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
22
THE PAWN LANGUAGE | FUNDAMENTALS
Using correct tags is important to avoid any bugs or errors during program
execution.
This this chapter, the Assignment Operator, as explained before is used for
assigning a value to a variable, but there are a few more operators that can be
useful while dealing with variables. The ‘Arithmetic Operators’ are used for
performing arithmetic operations like addition, subtraction, multiplication,
division, finding the remainder and raising a value to a power.
• Addition ( + )
• Subtraction ( - )
• Multiplication ( * )
• Division ( / )
• Remainder ( % )
• Power ( ^ )
We can apply these operators in many ways, some of the examples are given
below :
// Example 1
new number = 5 + 5; // Value of 'number' is 10
// Example 2
23
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
// Example 3
new a = 10; // Declaring 'a' with a value of 10
new b = 10; // Declaring 'b' with a value of 10 aswell
new c = a + b; // Assigning the sum of 'a' and 'b' to 'c'
// Example 4
new score = 100;
score = score + 100; // Now the value of score is 200 as it adds
100 to itself, but there is a shorter method which will be
explained below.
In the above examples, a few ways in which the arithmetic operators can be
used has been demonstrated. There are some Assignment Operators which
are specifically called ‘Compound Assignment Operators’, in which the base
assignment operator is combined with an arithmetic symbol to form a new
operator which can be useful in assigning values to the variables. These
include += , -= , *=, /= and a few others which are not very frequently used.
24
THE PAWN LANGUAGE | FUNDAMENTALS
Constants
Similar to variables, constants are entities which can hold values, but the
main difference is that once a constant is declared, it’s value stored in it will
not be changeable. Constants are declared using the ‘const’ keyword :
const my_constant = 5;
my_constant = 1; // Will show an error
25
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
Conditionals
Conditionals or Conditional Statements are used to evaluate conditions and
execute a block of code based on the result.
Code
#include <a_samp>
public OnFilterScriptInit ()
{
new value = 100;
return 1;
}
Output
The code above first declares a variable named ‘value’ and assigns it a value
of 100. It then checks if the value stored in the ‘value’ variable is greater
than 99, if it is then it prints the output indicating that, otherwise it prints
the second print statement from the code. There are two new keywords in
the code above namely ‘if’ and ‘else’. The ‘if’ keyword is used for writing
an if-statement’ of ‘conditional’ which is followed by a pair of brackets that
26
THE PAWN LANGUAGE | FUNDAMENTALS
contain the condition of the statement since that’s the requirement of the if-
statement. The condition in the brackets contains a reference to the variable
‘value’ and the number ‘99’ separated by a greater-than symbol (>) which is a
‘Conditional Operator’. The combination of the variable reference, greater-
than conditional operator and the value 99 forms a condition which simply
checks if the value stored in the variable ‘value’ is greater than 99. One thing
to remember is that, wherever a variable is referenced, it returns the variable’s
value at that point.
As explained before, if that condition is true the computer will execute all the
code in the block under this if-statement (code between the enclosing pair
of braces that follow). If it is false, then the code block belonging to the ‘else’
statement is executed.
If the code block contains a single line, then the enclosing curly braces are
not necessary, therefore the above program can also be written as :
#include <a_samp>
public OnFilterScriptInit ()
{
new value = 100;
return 1;
27
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
The conditionals simply compare different values but this turns out to be
extremely useful in programming. There are more conditional operators
which allow more flexible operations, a list of the basic conditional operators
is given below :
Note: The equal-to operator contains double equal signs, unlike the assign-
ment operator. This example below makes use of the equal-to operator.
if (score == 100)
print ("The score is equal to 100.");
else
print ("The score less or greater than 100.");
If you combine the ‘else’ and ‘if’ keywords, its form a new combined keyword
called ‘else if’ which basically works as an extension to the if-statement by
adding more conditions you can check for more possibilities. This can be
better understood by an example :
if (score == 100)
print ("The score is equal to 100.");
else if (score < 100)
28
THE PAWN LANGUAGE | FUNDAMENTALS
In the above example, you will notice there’s an addition condition along
with the ‘else if’ keywords. The code first checks if the value of score is equal
to 100, if it is then the code block under ‘if’ will be executed, otherwise it
will more on to the next operation, that is, another condition which checks if
the value of score is less than 100, if it’s true then it executes the code below
it. The code under ‘else’ is executed if you provided condition is satisfied,
and if there is no ‘else’ in your code (since it’s optional) then nothing will be
executed. You can experiment with the code by changing the value of ‘score’
and observing the change in output, in order to understand it more clearly.
You can chain the conditionals adding as many conditions as you want using
‘else if’.
There is another set of operators called ‘Logical Operators’. They are used
in conditional statements to combine and create more elaborate conditions
which comes in handy in many situations. These operators include logical
NOT, OR and AND operators.
• NOT ( ! )
• OR ( || )
• AND ( && )
The logical ‘OR’ operator checks if the condition on either one of its sides is
true.
new score = 1;
29
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
The logical ‘AND’ operator checks if the conditions on both of its sides are
true.
The logical ‘NOT’ operator basically checks if the condition is not true.
if (!(val == 25))
print ("val is not equal to 25");
else
print ("val is equal to 25");
The condition first evaluates the brackets (val == 25) and then turns its result
opposite (from true to false) because of the ‘!’ operator. So it is basically
checking if the value is not equal to 25. Rest of the code is self explanatory.
This operator can also be combined with the equal sign to form the ‘Not
30
THE PAWN LANGUAGE | FUNDAMENTALS
Equal-To’ operator, using which we can write the same code given above but
in a comparatively neater way :
#include <a_samp>
public OnFilterScriptInit ()
{
new val = 25;
if (val != 25)
print ("val is not equal to 25");
else
print ("val is equal to 25");
return 1;
}
Loops
Loops are used for repeating a block of code over and over again for a certain
amount of time or till a certain condition is satisfied. There are three types
of loops namely :
• for loop
• while loop
• do…while loop
‘For’ Loop
One of the most commonly used loops is called the for loop, which is used
for repeating a block of code for a specific number of times. The syntax for
writing a for loop is the following :
31
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
// condition - used for writing the condition under which the loop
with proceed (optional)
#include <a_samp>
public OnFilterScriptInit ()
{
for (new i = 0 ; i < 100 ; i++)
printf ("%i", i);
return 1;
}
// Output:
// 0
// 1
// 2
// ...
// 99
In the loop’s brackets, we first create a local variable (new i) which only by
32
THE PAWN LANGUAGE | FUNDAMENTALS
usable and assessable inside the loop’s code block. As a second expression,
we provide a condition for the loop (i < 100 ) , so that before every iteration
the loop checks the condition, in case if it is true it proceeds, otherwise
the loop stops. The third expression (i++) is for specifying an arithmetic
operation on any accessible variable, which is then performed on every
iteration. Conventionally we increment or decrement the value of the loop’s
variable so that it can eventually meet the condition provided. Notice how
the expressions are separated by semi-colons, this is how the compiler can
distinguish between different expressions, statements etc.
There is a new operator in the code (++) which is called the ‘increment’
operator. It is used for incrementing the value of a variable. Similar to that,
there is a decrement operator (- -) which is used to decrement the value of a
variable. Examples of both are given below :
new a = 10;
a++;
printf ("%d", a);
// Output: 11
new b = 1;
b--;
printf ("%d", b);
// Output: 0
Note that if the three parameters are left empty-which is possible as they are
optional-it forms an infinite (never ending) loop.
33
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
While Loop
A while loop takes only one expression, that is a condition under which it
will iterate till it’s turns false. Following is a simple program which uses a
while loop, functioning similar to a for-loop.
#include <a_samp>
public OnFilterScriptInit ()
{
new a = 10;
while (a >= 5)
{
printf ("%d", a);
a--;
}
return 1;
}
// Output:
10
9
8
7
6
5
Do…While Loop
The do…while loop similar to the while loop also repeats a set of instructions
(code) till the provided condition results false, but there is a difference in
its functionality. In the while loop the condition check is performed before
executing the code block, therefore if the condition is already false, it will
not execute any code. In contrast to that, the do…while loop first executes
34
THE PAWN LANGUAGE | FUNDAMENTALS
the code then checks the condition, every iteration. Consequently, even if
the condition provided is already false, the code will execute at least once.
Following is an example :
new i = -1;
do
{
print ("This will print once.");
}
while (i > 0);
Functions
A function is a keyword in a programming language, which when called
(referred to) executes a block of code. We have used a some functions in
the prior examples like print, printf, format, etc. A function may or may
not take in arguments. Some functions are provided by the base libraries
(includes), and we can use those along with other features of the language
to create custom functions. One of the reasons you would write a custom
function is to reduce the repetition of same lines of code in the program.
Following are some functions provided by ‘a_samp’ library that will be used
in the upcoming example :
GetPlayerAmmo (playerid);
// Returns the ammo of equipped weapon of the player
35
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
We can use functions creatively for the making of various features. Following
is the code for a ‘One-shot Deagle Deathmatch’ where the player upon
spawning will be given a Desert Eagle gun with 3 bullets and 10.0 health
:
#include <a_samp>
const DM_INTERIOR = 1;
36
THE PAWN LANGUAGE | FUNDAMENTALS
return 1;
}
return 1;
}
PutPlayerInDeathmatch (playerid)
{
SetPlayerHealth (playerid, 10.0);
GivePlayerWeapon (playerid, DM_WEAPON, DM_WEAPON_AMMO);
return 1;
}
37
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
PutPlayerInDeathmatch (playerid)
{
SetPlayerHealth (playerid, 10.0);
GivePlayerWeapon (playerid, DM_WEAPON, DM_WEAPON_AMMO);
return 1;
}
38
THE PAWN LANGUAGE | FUNDAMENTALS
Enumerators
Enumerators are in simple terms, ‘group of constants’. They are frequently
used for creating object-like structures (which will be covered in later
chapters) as there are no objects in the Pawn language. An enumerator can
be declared using the ‘enum’ keyword :
enum my_enumerator
{
CONSTANT_1,
CONSTANT_2
}
// Output : 0, 1
enum
{
CONST_A,
CONST_B
}
39
FROM A LITTLE-CLUCKER TO A SPAM-MACHINE
new CONST_A;
new CONST_B;
Preprocessor Directives
As the name suggests, ‘preprocessor directives’ are lines of code that instruct
the compiler to perform tasks like include lines of code from another script,
define some constants or some keywords along with their function, these
can also modify some properties of the compiler and disable warnings etc.
Preprocessor Directive start with a hash (#) symbol.
In most of the above examples we have used the ‘include’ preprocessor
directive to import functions from the ‘a_samp’ library.
#include <a_samp>
#define PI 3.14
#include <a_samp>
public OnFilterScriptInit ()
{
printf ("%d", SUM(1,1));
return 1;
}
// Output : 2
40
THE PAWN LANGUAGE | FUNDAMENTALS
41