13.12.2012, 05:19
(
Last edited by Frant1kz; 13/12/2012 at 05:55 AM.
)
Want to learn PAWN? Start here.
Introduction:Time and time again i come across useless, irrelevant, vital information lacking tutorials posted by members with barley any, if not zero knowledge on the actual topic other than the ability to explain how to paste it into another script. This is somewhat sad in reality as the common newbie is than "robbed" of there chance to learn something from a "proper tutorial".
In this tutorial i am going to explain what PAWN is, how its used with SA:MP and breifly explain basic pawn coding with a small pawn script tutorial(Without SA:MP).
So, what is PAWN exactly?
PAWN is a typeless, open source, C-like scripting language(Formally known as "Small") maintained by a Dutch company named CompuPhase.
Unlike other scripting languages PAWN does not ship native functions, instead PAWN's functions come from "include" files which can be used for development.
Did you know?: Although PAWN is famously used by SA:MP, it is also used for Half-life mods such as Counter-Strike 1.6 to make plugins.
Well, so how does it work?
Think of PAWN as a scripting language that hasn't been "told what to do" yet, this is where the "includes" come into play. Includes are files like filterscripts located in "pawno/includes" directory, mainly hold native functions(ie. SetPlayerHealth), forwards to callbacks(ie. OnPlayerDeath) and other definitions(ie. #DEFINE MAX_PLAYERS) which are called from the gamemode using "#include <include_name_here>" which then can be used for developing the gamemode.
This is why you will see "#include <a_samp>" in every script, as it includes SA:MP's functions, callbacks and so on to be able to make gamemodes, filterscripts and includes that work on SA:MP servers.
Any example scripts, explained?
We are going to code something simple in PAWN, although we are not going to include a_samp so you can understand PAWN more easily, without the confusion of all the latest includes and libraries as if your new to PAWN altogether, this will be the best way for your to grasp how a basic script works, then i will give you a few recommended tutorial links at the end to guide you in the right direction when learning PAWN for SA:MP servers.
Ok, we are going to create a very simple script that logs the exact hour, minute, day, month and year it first loaded.
So what do we need for this kind of script? File and date/time functions that is. Luckily the default pawno package comes with the "file" and "time" includes(Also default in SA:MP, don't worry you have these includes) which we can use for our new script.
Now, go ahead and open both "file.inc" and "time.inc" in the "pawno/includes" directory and study them.
As you can see in "file.inc" when you scroll to the bottom of the script, we can use native functions like "fopen" and "fwrite" so we can open a new file, write the time and date(Explained further on) and use "fclose" to close our file and finish the script process, resulting in a text file containing the hour, minute, day, month and year it loaded.
As you can see in "time.inc" we have native functions like "getdate" and "gettime" to get the current time and date from UTC time to write in the file.
There is on other thing we need, that were going to create ourselves. We need a function that formats the time and date into text to be able to write it into the file.
Lets begin by opening pawno and including the file and time includes and typing the following underneath it like so(Which will be explained below)
pawn Code:
#include <file>
#include <time>
native data(format[], length, const string[], {Float,_}:...);
Now were going to need to specify the name and location of the file were going to create, so for your benefit lets take advantage of "macros" to define the location like so:
pawn Code:
#include <file>
#include <time>
native data(format[], length, const string[], {Float,_}:...);
#define FILE "file_name.txt"
pawn Code:
#include <file>
#include <time>
native data(format[], length, const string[], {Float,_}:...);
#define FILE "file_name.txt"
main()//This function defines the main script(Gamemode), and is also called when the script loads
{
if(!fexist(FILE))//We use the "fexist" function to check if the FILE exists (Note: ! means "Not", so !fexist = file does not exist)
//If the file has not been created yet, it will pass the if statement, else if so, its not the first time the script loaded)
{
new File:file = fopen(FILE, io_write);//We make a new variable and use "fopen" to open and write to a new file
new hour, minute, sec, day, mon, year;//Data
new string[128];//Data
gettime(hour, minute, sec);//We get the hour, minute and sec and store them to the data variables
getdate(year, mon, day);//And dido
//We now use our "data" function to format the date and time into a string
data(string, sizeof(string), "This script first ran on the %d/%d/%d(D/M/Y) at %d:%d", day, mon, year, hour, minute);
fwrite(file, string);//We then write the string to the file using "fwrite"
fclose(file);//And close the file using "fclose"
}
return true;//And tell pawno the script has finished and to stop here
}
Before we finish let me add a few tricks you can do with macros, variables and booleans.
I will show you how you can define if the script is enabled or not using them, like so:
pawn Code:
#include <file>
#include <time>
native data(format[], length, const string[], {Float,_}:...);
#define FILE "file_name.txt"
#define SCRIPT_ENABLED//Use a macro to define if the script is enabled(ie. #if defined SCRIPT_ENABLED) ecplained below
//Note: Commenting this line out will disable the script
new bool:ScriptEnabled;//And a similar way with a boolean (ie. ScriptEnabled == true, ScriptEnabled == false)
//Note: Booleans only use "true" and "false" statements
new ScriptDisabled;//And again(ie. ScriptDisabled = 0, ScriptDisabled = 1)
//Variables use an integra(number) and can be used to count
main()
{
ScriptEnabled = true;//Setting this boolean to false will disable the script
ScriptDisabled = 0;//Setting this variable to 1 will disable the script
if(ScriptDisabled != 1)//Means: if > ScriptDisabled > does NOT equal > 1 =(pass)
{
if(ScriptEnabled == true)//means if > ScriptEnabled > equals > true (pass)
{
#if defined SCRIPT_ENABLED//Will pass onto processing script ONLY if defined SCRIPT_ENABLED
if(!fexist(FILE))
{
new File:file = fopen(FILE, io_write);
new hour, minute, sec, day, mon, year;
new string[128];
gettime(hour, minute, sec);
getdate(year, mon, day);
data(string, sizeof(string), "This script first ran on the %d/%d/%d(D/M/Y) at %d:%d", day, mon, year, hour, minute);
fwrite(file, string);
fclose(file);
}
#endif
}
}
return true;
}
So, how to script for SA:MP?
Well to be honest, that is a hard question, and i think the best answer would be: LEARN, LEARN AND LEARN SOME MORE.
Below are some useful(reliable) links to newbie friendly tutorials so you can ease your way into the basics and have a better foundation to learn off:
1. SA:MP Wiki Tutorial 1
2. SA:MP Wiki Tutorial 2
3. SA:MP Wiki Tutorial 3
4. Useful Tutorials List
5. SA:MP Forum Tutorial Section
If you have any questions, post them here and i will try my best to answer to all of you.