Jump to content
Hostul a fost schimbat. Daca vedeti serverul offline readaugati rpg.b-zone.ro sau 141.95.124.78:7777 in clientul de sa-mp ×

[PAWN] General Informations about Pawn


Dion ibe
 Share

Recommended Posts

Note: This guide is rather hardcoded to AMX Mod X. It needs to be mode generic

This guide is designed to give you a more in-depth overview of the basics of programming in Pawn.

Introduction
Pawn is an embeddable, (almost) typeless, easy to use scripting language that is compiled for a virtual machine. AMX Mod X uses Pawn to route scripting functions to the Half-Life engine, using the Pawn Virtual Machine and Metamod (Pawn is written in C, Metamod is written in C++). While you write Pawn scripts in a text editor, the scripts must be compiled with a "Compiler", which produces a binary for AMX Mod X. The AMX Mod X team distributes a specially modified Pawn compiler.
 
Programming scripts in Pawn is relatively easy, and does not have concepts in other languages that are unnecessary for general use, such as pointers, vectors, structs, classes, allocation, et cetera.
 
Language Paradigms
Pawn was originally named "Small" to emphasize the size of the language specification. The language sacrifices many features found in modern languages to achieve simplicity and speed, which are required for embedded uses.
 
No typing
Pawn only has one data type -- the "cell". It is the size of the processor's integral pointer (4 bytes for 32bit processor, 8 bytes for 64bit processors). This has two major implications - Pawn bytecode is processor specific, and pointers can fit inside a cell.
Tagging - Pawn lets you create weakly statically typed "tags", which can be associated with variables for primitive operator overloading. For example, Pawn has no concept of floating point numbers (only integers). Instead, operators are overloaded with the Float: tag to redirect computation to new functions. Tag-checking is only enforced as a warning. If you're using SourcePawn (for SourceMod), there is actually a second type: String. In a String, letters are stored as separate bytes... essentially having 4 characters stored in every cell.
Since Pawn only has one datatype, it does not support structs, records, objects, or anything else.
Pawn does support arrays of cells, which leads to C-style arrays for strings.
No garbage collection
Pawn has no "heap" allocation built-in. All variables are stored on the stack or in the data section. Therefore, no garbage collection is necessary and memory leaks are not possible from the language specification alone.
Procedural
Pawn is entirely comprised of single, non-nested subroutines. There are no lambda functions, member functions, constructors, et cetera. Functions can either be internal (within the script) or public (exposed to the VM by name, like C's "extern").
No thread-safety
Pawn is targetted toward single-thread instances.
Implementation Features
Cross-platform compatible compiler, which outputs bytecode and debug browsing information.
Cross-platform compatible Virtual Machine (VM), with support for debug browsing, halting/stopping execution, and interacting with scripts from C/C++ libraries.
IA32 JIT Compiler for vastly increasing script execution time.
Because the footprints of the VM and JIT are so small, Pawn is ideal inside games which need a simple and highly fast event system, embedded devices or applications, and realtime systems.
 
License
Pawn is licensed under the ZLib/libpng_License license.
 
Variables
Variables are simple structures for holding data throughout a period of time in your script.
 
Types
Small has just three data types for declaring variables. The default variable type is a regular whole number, or integer. A variable name, for backwards compatibility, should be 19 characters or less, and MUST start with a letter. It can contain the symbols A-Z, a-z, 0-9, and the underscore ("_"). It is important to note that variable names are case sensitive - "myvar", "MyVaR", and "MYVAR" are three separate symbols.
 
Integers
The simplest data type in Pawn is an "integer". Integers are whole numbers. To declare a new integer variable, use the "new" operator like so:
new a            //Declare empty variable "a"
new b=5          //Declare variable "b" and set it to 5.
new c=5.0        //This is invalid, technically not a whole number!
new d="hello"    //"hello" is not a number either, this is invalid.


//You can also declare multiple variables on one line:
new e,f,g,h
new x=7, y=3
new z = 1_000_000 // Pawn supports numbers like this. So big values are easier to read.
Floats
You can also declare a variable as a "Float", which means it can store numbers with decimal places. These are called "floating point" numbers:
new Float:a            //Declare empty floating point variable "a"
new Float:b=5.3        //This will declare a new variable "b" and assign 5.3 to it.
new Float:c=5          //This is valid, but the compiler will give you a warning.
new Float:d="hello"    //This is invalid, "hello" is not a decimal number.

>>You can also do the following:

//float(n) is a function that takes a number n and makes it a// floating point number.
new Float:var = float(5)
new Float:var2 = 5.0     
new Float:var3 = 1.0*5
new var4 = floatround(5.0)     
//Note: floatround(n) is a function that takes a number n and rounds it to a whole number.
//  this makes the assignment to a regular integer variable valid.
Arrays
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]
If an expression is not zero or it is not false, it not only returns a value, it also returns "true". Otherwise, it will return 0, which is also "false".
 
//Here are more mathematical expressions.  The mathematical operators are
// + for addition
// - for subtraction
// * for multiplication
// / for division
// % for modulus (finding the remainder of one number divided by another (5%2 is 1)
(5+6)                       //returns 11
((5*6)+3)                   //returns 33
((((5+3)/2)*4)-9)           //returns 7
((5*6) % 7)                 //returns 2
//Here are other expressions:
(true)                      //returns true
(5.0 + 2.3)                 //returns 7.3 as a floating point
Each slot in the first subset of the array becomes its own array.
 
new BigArray[3][3]
BigArray[0][0] = 10
BigArray[0][1] = 20
BigArray[0][2] = 30
BigArray[1][0] = 40
BigArray[1][1] = 50
BigArray[1][2] = 60
BigArray[2][0] = 70
BigArray[2][1] = 80
BigArray[2][2] = 90
Will result in BigArray looking like this:
 
BigArray 0 1 2
0 10 20 30
1 40 50 60
2 70 80 90
 
Conclusion
This guide should have given you a VERY brief introduction to basic Pawn programming. It is by no means comprehensive and it should not constitute the entirety of one's knowledge of Pawn. To read the official Pawn documentation and language guide, go this website: http://www.compuphase.com/pawn/Pawn_Language_Guide.pdf(Note, this guide is very long and should be used as a reference. You may want to try the Small forums or the AMX Mod X forums). Continue to the next Section to see how to apply Small programming to the Half-Life and AMX Mod X engine!
Thanks for your time.Good Luck!! :*:):P:(
Edited by Cdorsu
Link to comment
Share on other sites

Te pricepi man, bravo

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.