SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 13/12/2012, 05:19 AM   #1
Frant1kz
Little Clucker
 
Frant1kz's Avatar
 
Join Date: Nov 2012
Posts: 25
Reputation: 4
Default Want to learn PAWN? Start here.

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.

Last edited by Frant1kz; 13/12/2012 at 05:55 AM.
Frant1kz is offline   Reply With Quote
Old 13/12/2012, 05:26 AM   #2
RajatPawar
High-roller
 
RajatPawar's Avatar
 
Join Date: Aug 2011
Location: ヽ༼ຈل͜ຈ༽ノ
Posts: 2,177
Reputation: 311
Default Re: Wan't to learn PAWN? Start here.

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!
RajatPawar is offline   Reply With Quote
Old 13/12/2012, 05:32 AM   #3
Frant1kz
Little Clucker
 
Frant1kz's Avatar
 
Join Date: Nov 2012
Posts: 25
Reputation: 4
Default Re: Wan't to learn PAWN? Start here.

Quote:
Originally Posted by Rajat_Pawar View Post
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".
Frant1kz is offline   Reply With Quote
Old 13/12/2012, 05:47 AM   #4
Kreatyve
Big Clucker
 
Kreatyve's Avatar
 
Join Date: Oct 2012
Posts: 103
Reputation: 26
Default Re: Wan't to learn PAWN? Start here.

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?
Kreatyve is offline   Reply With Quote
Old 13/12/2012, 05:56 AM   #5
Frant1kz
Little Clucker
 
Frant1kz's Avatar
 
Join Date: Nov 2012
Posts: 25
Reputation: 4
Default Re: Wan't to learn PAWN? Start here.

Updated, for the Grammar sensitive clowns.
Frant1kz is offline   Reply With Quote
Old 13/12/2012, 06:01 AM   #6
Kreatyve
Big Clucker
 
Kreatyve's Avatar
 
Join Date: Oct 2012
Posts: 103
Reputation: 26
Default Re: Wan't to learn PAWN? Start here.

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.
Kreatyve is offline   Reply With Quote
Old 14/12/2012, 09:49 PM   #7
Jonathan19
Little Clucker
 
Jonathan19's Avatar
 
Join Date: Dec 2012
Location: here and there
Posts: 29
Reputation: 0
Default Re: Wan't to learn PAWN? Start here.

nice little tutorial for beginners like me
Jonathan19 is offline   Reply With Quote
Old 13/06/2013, 04:04 AM   #8
Inverse
Huge Clucker
 
Inverse's Avatar
 
Join Date: Jun 2011
Location: Braavos
Posts: 251
Reputation: 19
Default Re: Wan't to learn PAWN? Start here.

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.
__________________
[FS] xPlosions - NEW!
Inverse is offline   Reply With Quote
Old 13/06/2013, 05:35 AM   #9
Scenario
Godfather
 
Scenario's Avatar
 
Join Date: Feb 2010
Location: Heaven
Posts: 7,804
Reputation: 701
Default Re: Wan't to learn PAWN? Start here.

Quote:
Originally Posted by Kreatyve View Post
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.
Scenario is offline   Reply With Quote
Old 13/06/2013, 05:40 AM   #10
[ABK]Antonio
High-roller
 
[ABK]Antonio's Avatar
 
Join Date: Sep 2011
Location: ID, USA
Posts: 1,223
Reputation: 185
Default Re: Wan't to learn PAWN? Start here.

Since this is for beginners...Why use data instead of just format? That's way less work for a newbie
__________________

ABKClan.com
[ABK]Antonio is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The best way to learn PAWN scripting sajk Scripting Help 14 04/08/2015 09:02 AM
Book to learn Pawn? davve95 General 9 20/10/2011 11:27 AM
PAWN Learn. airsoftglock Everything and Nothing 11 07/12/2010 11:46 AM
Best way to learn pawn Saurik Everything and Nothing 4 11/08/2010 01:14 PM
Learn pawn.. universe Everything and Nothing 6 01/12/2009 07:59 PM


All times are GMT. The time now is 01:15 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.