Want to learn PAWN? Start here. -
Frant1kz - 13.12.2012
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,_}:...);
As you can see under the includes we have created a new native function named "data"(Which works very much like the "format" function in "a_samp.inc") to format the date and time into a string so it can be formatted into the file(Shown further on)
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"
Now its time for the fun part, the script. Copy/paste the script under the includes and defines like so(I will explain it below):
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
}
And there you have it, a script that logs its starting date and time
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;
}
Note: Note how i use 2 spaces in between brackets and lines as pawno shows a warning for anything else than 2 spaces(SA:MP is 4 spaces as its made to be)
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.
Re: Wan't to learn PAWN? Start here. -
RajatPawar - 13.12.2012
EDIT:
It's 'want' by the way, not 'wan't' lol.
Anyways, this is a good tutorial, however if you want, really want newbies to start with scripting here, I would go for something basic, like printing messages or kicking people when they join or have miniguns. Your tone suggests me you have not taken my 'constructive criticism (lol)' well. Try to!
Re: Wan't to learn PAWN? Start here. -
Frant1kz - 13.12.2012
Quote:
Originally Posted by Rajat_Pawar
It's 'want' by the way, not 'wan't' lol.
Anyways, this is a good tutorial, however if you want, really want newbies to start with scripting here, I would go for something basic, like printing messages or kicking people when they join or have miniguns.
|
Learn English, the proper Grammar is "wan't".
Re: Wan't to learn PAWN? Start here. -
Kreatyve - 13.12.2012
No it isn't.
It's want, good sir.
Not 'Wa not'
Wa isn't even a word. It's an abbreviation for one of the U.S. states.
It's - It is
Isn't - Is not
Don't - Do not
Wasn't - Was not
Get it now?
Re: Wan't to learn PAWN? Start here. -
Frant1kz - 13.12.2012
Updated, for the Grammar sensitive clowns.
Re: Wan't to learn PAWN? Start here. -
Kreatyve - 13.12.2012
No, you've made an issue about it by trying to tell someone to "Learn English" when you weren't even doing it the proper way yourself.
Calling us "Grammar sensitive clowns", for proving you wrong and helping you understand the proper way, just shows your immaturity.
Re: Wan't to learn PAWN? Start here. -
Jonathan19 - 14.12.2012
nice little tutorial for beginners like me
Re: Wan't to learn PAWN? Start here. -
Inverse - 13.06.2013
A nice tutorial for beginners!
I used the a_samp.inc instead of the data and time includes. Used 4 space indentation and used the format function of a_samp.inc instead of your data function and it went well
No errors.
Re: Wan't to learn PAWN? Start here. -
Scenario - 13.06.2013
Quote:
Originally Posted by Kreatyve
No it isn't.
It's want, good sir.
Not 'Wa not'
Wa isn't even a word. It's an abbreviation for one of the U.S. states.
It's - It is
Isn't - Is not
Don't - Do not
Wasn't - Was not
Get it now?
|
Actually, it's WA, not Wa. There's a big difference.
Re: Wan't to learn PAWN? Start here. -
[ABK]Antonio - 13.06.2013
Since this is for beginners...Why use data instead of just format? That's way less work for a newbie