Pawn (AMXX) Tutorial (Variables) [12/31/2016]
Nov 13, 2016 11:02:14 GMT
zmd94, Obada D., and 4 more like this
Post by DoNii on Nov 13, 2016 11:02:14 GMT
Pawn is the language that AMXX codes are written on. Pawn routes AMXX to connect with Half Life's Engine. Pawn is written on C and AMXX ( Metamod Plugin ) is written on C++.
Variables :
Variables are data types for holding data. (int, string, float..) . You can't use spaces when naming them, also they're case sensitive, that means that one capital character can change everything
They can be local and global :
Local Variables :
They're created inside a function and can be used only in that function.
Global Variables :
They're outside of functions and can be accessed in any function, a good example is a variable created for holding item price.
Example Local Variable :
Example Global Variable :
Floats :
Float can store numbers with decimal values (0.1, 0.2535, 1.53, 96.12 etc..).
Booleans :
Booleans or Bools, are variables which can only be assigned the value "true" or "false". Example
Arrays :
Arrays are data types, that you can store in numbers and strings. Note that Arrays are zero-based. There are 1D and 2D arrays.
2D Arrays :
Strings :
A string is an array of numbers that translate to ASCII (character) symbols.
Comments :
Comments ( // ) are ignored by the compiler, they are used to make the code more understandable , i've used comments on this tutorial. Example :
Where can variables be helpful ?
Integers :
For holding prices, CVARs, count limits, offsets etc..
Bools :
For checking if an item has been used, for stocks.
Arrays :
Holding CVARs, random integers, origins etc..
Variables :
Variables are data types for holding data. (int, string, float..) . You can't use spaces when naming them, also they're case sensitive, that means that one capital character can change everything
new x // this is the variable x , it is empty as we haven't assigned any value
new x = 1 // now the x variable has the value "1"
new x = 0.5 // this is WRONG, you can't assign floats as variables.
new e,f,g,h // you can also declare multiple variables in just one line
new x=7, y=3 // and this
new var_name // correct naming
new var name // incorrect
new var_name
// this isn't the same as this
new Var_name
They can be local and global :
Local Variables :
They're created inside a function and can be used only in that function.
Global Variables :
They're outside of functions and can be accessed in any function, a good example is a variable created for holding item price.
Example Local Variable :
#include <amxmodx>
public plugin_init( ) {
register_plugin( "Test" , "1.0" , "DoNii" ) ;
register_clcmd( "say /showmyname" , "my_name" ) ;
}
public my_name( id ) {
new iName[ 32 ]; // our variable
get_user_name( id , name , charsmax( iName ) ) ;
client_print( id , print_chat , "Your Name Is %s", iName ) ;
}
Example Global Variable :
#include <amxmodx>
new g_PriceM4A1 = 3000 // it's better to name it with the prefix g_ if it's global.
public plugin_init( ) {
register_plugin( "Test" , "1.0" , "DoNii" ) ;
register_clcmd( "say /priceofm4a1" , "M4A1PriceShow" ) ;
}
public M4A1PriceShow( id ) {
client_print( id , print_chat , "Price Of M4A1 Is $%d", g_PriceM4A1 ) ;
}
Floats :
Float can store numbers with decimal values (0.1, 0.2535, 1.53, 96.12 etc..).
new Float:var = 0.5
new Float:var2 = 1.2
new Float:var3 = 0.8
Booleans :
Booleans or Bools, are variables which can only be assigned the value "true" or "false". Example
new bool:var //Declares a new variable "var" which is automatically false
new bool:var1 = true //Declares a new variable "var1" set to true
Arrays :
Arrays are data types, that you can store in numbers and strings. Note that Arrays are zero-based. There are 1D and 2D arrays.
new Players[32] //This will declare a variable called "Players" which holds 32 numbers.
Players[0] = 5 //Set slot 0 to 5
new BadArray[a] // this is invalid because the slot must be a constant number ( 1,2,3,4,5.. )
new array[][] = // this is a 2D array, it's used for more readable and easy assign to bigger values, this is int method
{
"5, 6, 7, 8"
"2, 3, 4, 8"
"5, 2, 0, 1"
}
new array[] = { "5,1,4" } // this is a 1D array, used for 1 line assignment.
2D Arrays :
In Pawn it is possible to have arrays where each slot is another array. This is very useful for storing a table of data, where the first section of slots is a row and the second section of slots is a column. Two dimensional arrays are declared like so:
//This declares an array with 50 rows and 50 columns.
new BigArray[50][50]
//this declares a floating point array with 25 rows and 10 columns.
new Float:BigArray[25][10]
Strings :
A string is an array of numbers that translate to ASCII (character) symbols.
//This will declare a number array "myString" that contains the data "Hello".
//It will have 6 slots, because there are 5 characters.
//The last slot is reserved for the number 0, which tells the Pawn engine that it is a string.
new myString[] = "Hello" // in game it will output "Hello"
new myString[5] = "Hello" // will also output "Hello"
new myString[4] = "Hello" // will output "Hell" because we've made the slot 4, last slot is taken by the engine, so you need to count +1, if the word has 4 letters, you need to specify the slot 5, or just leave it blank, it will calculate itself.
new myString[6] // you can even do like this
myString[0] = 'H'
myString[1] = 'e'
myString[2] = 'l'
myString[3] = 'l'
myString[4] = 'o'
myString[5] = 0 // notice the null (0), it tells the engine where the string ends.
Comments :
Comments ( // ) are ignored by the compiler, they are used to make the code more understandable , i've used comments on this tutorial. Example :
#include <amxmodx>
public plugin_init() {
register_plugin("Plugin", "1.0", "DoNii"); // this is a comment, compiler will not compile it
}
public client_putinserver(id) {
new szName[ 32 ] ;
get_user_name(id, szName, charsmax( szName ) ) ;
client_print( id, print_chat, "Your Name is : %s", szName ) ;
}
Where can variables be helpful ?
Integers :
For holding prices, CVARs, count limits, offsets etc..
Bools :
For checking if an item has been used, for stocks.
Arrays :
Holding CVARs, random integers, origins etc..