SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 17/07/2013, 10:00 AM   #1
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default PAWN For Beginners : 1 - Understanding PAWN

INTRODUCTION


I created this tutorial to help people who doesn't know how to create a server and who doesn't know how to make basic scripting. I will make this topic a serie. Going to probably 6 or 7. Or maybe more. I have got lots to explain. But they will come soon. We will go through several things, they are :

  1. - What is PAWN ?
  2. What is Pawno ?
  3. What is function ?
  4. What is callback ?
  5. Defining colors.
  6. Includes
  7. Let's write hello world !
  8. Well-known compiler errors


So let's start, shall we ?



1) What is PAWN ?


PAWN is a programming language which is typeless. It is a mix of already known programming languages. PAWN is used by many games including SA-MP. It has got functions and callbacks just like any other programming language !


2) What is Pawno ?


[B][I][CENTER]Pawno is a programming language application which helps us creating PAWN scripts. It has got a pawncc.exe which works as a compiler and a compiler helps you finding errors and warnings which prevents your script working good. If it compiles right, it will create an .amx file which will be used if you start samp-server.exe. Servers use .amx files, they do NOT use .pwn file to open the script.

3) Where can I get 'Pawno' ?


Well, it is in standard SA-MP server package. You can get SA-MP server package for 0.3x here : Click me !. After you download SA-MP server package's RAR, extract them to desktop. Open the folder and here we go. Pawno is in Pawno folder inside your server folder. Just click on Pawno.exe

4) But it is blank page and I can't write something !


Of course you can not ! Just hit 'New' at the left-top corner of your pawno. It must be a blank page. When you scroll your mouse to there it already says 'New'. Hit there and you will see a empty script with lots of callbacks !

We will make a gamemode so we should delete these lines :

pawn Code:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

Why ? Because we aren't going to make a filterscript ! If we were going to make a filterscript we should have done it like this(I will talk about comments in the second article) :

pawn Code:
#define FILTERSCRIPT

So what is different ? I deleted 2 comments on top. Which refeers to 'This is a comment' and 'uncomment the line below if you want to write a filterscript' and i deleted the // for removing it from comment.

What else will we do ?


Well, delete these lines as we are not making filterscript again.
pawn Code:
#if defined FILTERSCRIPT // Well, if you are making a filterscript, then don't delete here. This tells the script if filterscript's defined at the top of our script.

public OnFilterScriptInit() // This is a callback. Here starts as soon as your filterscript gets started. Even though before your server starts. So you can use here to connect your SQL to your server ! And you can add your vehicles, classes, objects here ! And lots of more of course ! :)
{
    print("\n--------------------------------------");
    print(" Blank Filterscript by your name here"); // These prints are the ones which prints to your console. Such as 'Rufio's Test gamemode has been started !' !
    print("--------------------------------------\n");
    return 1;
}

public OnFilterScriptExit() // This is the callback used in filterscript's exiting. That can be done manually by RCON-logging in your server and typing /rcon unloadfs 'fsname' or automatically by closing your server. You can print again of course ! Such as 'Rufio's test filterscript has been shut down !' ! ;)
{
    return 1; // Return 1 is a function though. It means return this true. 1 is true, 0 is false.
}

#else // This tells the script if this script is a gamemode.

and also do NOT forget to delete this :

pawn Code:
#endif

Sorry for the mistake here. You should NOT delete this if it is a filterscript. I was sleepy. Lol. Why not delete this ? Well, because we have stated an #if statement at top of our script as said #if defined FILTERSCRIPT. So we should add #endif at the end of our filterscript.

So, final-looking of our gamemode shall be :

pawn Code:
#include <a_samp>


main()
{
    print("\n----------------------------------");
    print(" Blank Gamemode by your name here");
    print("----------------------------------\n");
}

public OnGameModeInit()
{
    // Don't use these lines if it's a filterscript
    SetGameModeText("Blank Script");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
    return 1;
}

public OnPlayerConnect(playerid)
{
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    return 1;
}

public OnPlayerSpawn(playerid)
{
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    return 1;
}

public OnVehicleSpawn(vehicleid)
{
    return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
    return 1;
}

public OnPlayerText(playerid, text[])
{
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/mycommand", cmdtext, true, 10) == 0)
    {
        // Do something here
        return 1;
    }
    return 0;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    return 1;
}

public OnPlayerEnterCheckpoint(playerid)
{
    return 1;
}

public OnPlayerLeaveCheckpoint(playerid)
{
    return 1;
}

public OnPlayerEnterRaceCheckpoint(playerid)
{
    return 1;
}

public OnPlayerLeaveRaceCheckpoint(playerid)
{
    return 1;
}

public OnRconCommand(cmd[])
{
    return 1;
}

public OnPlayerRequestSpawn(playerid)
{
    return 1;
}

public OnObjectMoved(objectid)
{
    return 1;
}

public OnPlayerObjectMoved(playerid, objectid)
{
    return 1;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
    return 1;
}

public OnVehicleMod(playerid, vehicleid, componentid)
{
    return 1;
}

public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
    return 1;
}

public OnVehicleRespray(playerid, vehicleid, color1, color2)
{
    return 1;
}

public OnPlayerSelectedMenuRow(playerid, row)
{
    return 1;
}

public OnPlayerExitedMenu(playerid)
{
    return 1;
}

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    return 1;
}

public OnRconLoginAttempt(ip[], password[], success)
{
    return 1;
}

public OnPlayerUpdate(playerid)
{
    return 1;
}

public OnPlayerStreamIn(playerid, forplayerid)
{
    return 1;
}

public OnPlayerStreamOut(playerid, forplayerid)
{
    return 1;
}

public OnVehicleStreamIn(vehicleid, forplayerid)
{
    return 1;
}

public OnVehicleStreamOut(vehicleid, forplayerid)
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    return 1;
}

public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
    return 1;
}

So, let's save and compile this script ! Press F5. It should ask you where to save. Select your server folder's gamemodes folder. And save it there. After saving, it should compile your script and must say this :

Code:
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase
This means our script was successfully compiled and ready to use in our server !

We will edit this gamemode later. But first, I will tell you what is function and what is callback.


5) What is function ?

Function is a part of scripting. It tells the script what to do in which callback. Such as,

SendPlayerMessage(playerid, 'COLOR' , "Asdasdasdas");

is a function. It tells the script to send player a message in a color you specified. And the message should be asdasdasdas. Well, you must put " " before writing your text. It means string in scripting languages. And string means text in proper English. Let's give an example to understand this better:

If we add this to OnPlayerConnect, it should look like this:

pawn Code:
public OnPlayerConnect(playerid)
{
        SendPlayerMessage(playerid, COLOR_RED, " Welcome to our server ! Have fun ! :)" );
    return 1;
}

So, this means :

When a player connects, this message will appear to him. Just him. If we want to send this to public, we will simply make:

pawn Code:
public OnPlayerConnect(playerid)
{
        SendPlayerMessageToAll(COLOR_RED,"A player has joined our server ! Welcome ! :)");
        return 1;
}

Noticed ? We didn't write playerid. Guess why. Because we are sending this to EVERY player who is playing in our server. Not to a specified person.

pawn Code:
public OnPlayerConnect(playerid)

is a callback which we will talk about later.

pawn Code:
SendPlayerMessageToAll(COLOR_RED,"A player has joined our server ! Welcome ! :) ");

is a function. We should put a ( after the function. And obey its' writing rules. So we wrote a COLOR and then put a , and then put " for telling the script our text goes now. And we closed the " for telling the script our text has finished. Then we closed our ( so that it means our writing has finished and put a ; for telling our work with this line has finished. Do not miss ) and a ; else our script will give a error.

Still, our script should give a error. It should be this :

Code:
error 017: undefined symbol "COLOR_RED"
we will define the color later. So it can stay.

For more about functions, visit wiki.sa-mp.com !

[B][I][CENTER]6) What is callback ?

Callbacks are the skeletons of scripting. Callbacks tell our script statements. You can understand them with your English though. So I won't tell too much about callbacks. They are really easy to understand ! Such as :

pawn Code:
OnPlayerConnect

means when a player connects

pawn Code:
OnPlayerDisconnect

means when a player disconnects

pawn Code:
OnPlayerSpawn

means when a player spawns can be any reason. Connecting, dying etc.

pawn Code:
OnPlayerDeath

means when a player dies.

But, there are several important callbacks. I will talk about them :


They are :

pawn Code:
main()

and
pawn Code:
OnGameModeInIt

main is for console works. Such as printing something to console. Also you can connect your server to a MySQL database in main !

OnGameModeInIt is almost for everything ! Pickups, map-icons , objects, vehicles, classes, Gamemode text.

That is all I am going to tell about callbacks. Visit wiki.sa-mp.com for more ! You can ask help in here too !

7) Defining Colors !

We will use this:

pawn Code:
#define

We are going to define COLOR_RED as we wrote COLOR_RED.

It should be

pawn Code:
#define COLOR_RED 0xA10000AA

#define is a pawno function. It helps us defining things we want to do. We were going to define COLOR_RED so we wrote COLOR_RED. and 0xA10000AA is red's color code in Pawno. So if we were going to define COLOR_YELLOW it should have been like this :

pawn Code:
#define COLOR_YELLOW 0xE0E377AA

it is same. Hope you understood !

Includes


Look at the top of your script. It should say :

pawn Code:
#include <a_samp>

it means this is a script for SA-MP. And loads SA-MP's normal functions. So the more includes you have got in your script, the more functions you have got to use. But do NOT write nonsense includes to your gamemode just to have more functions. Add the ones you need.

Just do not forget this syntax:

pawn Code:
#include <include name>

write your include name between < and >


9) Writing "Hello World!"


Lets make a command.

For so, we will use :

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])

callback. we currently have 1 ready command. We will edit it a bit.

it is :

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[]) // if a player writes command
{ // callback starts.
    if (strcmp("/mycommand", cmdtext, true, 10) == 0) // if a player writes /mycommand
    { // if he writes, start these functions:
        // Do something here
        return 1; // return true.
    } // we ended this command's functions.
    return 0; // return false because there is a true above.
} // we ended this callback's functions.

Do NOT forget returns ! They are useful things !

lets do it like this :

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[]) // if a player writes command
{ // callback starts.
    if (strcmp("/scripting", cmdtext, true, 10) == 0) // if a player writes /scripting
    { // if he writes, start these functions:
        SendPlayerMessage(playerid, COLOR_RED, " Hello world !"); // If he writes, send him a red colored "Hello world !" message.
        return 1; // return true.
    } // we ended this command's functions.
    return 0; // return false because there is a true above.
} // we ended this callback's functions.

Hello world is a standard. Practise and work on this command and try making something more ! You will get used to it, I swear !

10) Last but not least, Well-known compiler errors !


the worst of them is :

Code:
error 025: function heading differs from prototype
The solution of this is :

you wrote a forward wrong. Such as:

If you should have written

pawn Code:
forward SetPlayerTeamFromClass(playerid, classid);

but wrote:

pawn Code:
forward SetPlayerTeamFromClass();

you will get this error.

the most common error :

Code:
fatal error 100: cannot read from file: "the include's name"
open your pawno and hit 'New' and close the pawno and open your gamemode. Compile it again. If it gives error again, go to ****** and ****** the include it asks for and add it to your pawno->includes folder. And compile again , Done !


Code:
error 054: unmatched closing brace ("}")
This happens when you put more than 1 closing }'s. Delete 1 and compile again. If it still happens delete more


Code:
error 001: expected token: ";", but found "}"
This happens when you didn't put a ";" at the end of the line. Except " " 's ofc. So you should put a ;


Code:
error 021: symbol already defined: "a thing you have already defined"
This happens when you define a thing more than one time. Search for it in your script and delete the one you defined extra.


This is all I guess. Hope I helped. You can ask questions of course ! Good Luck !

I will continue this series when I find time though. See you guys !

Tell me all the things I did wrong. I will edit

Last edited by Rufio; 18/07/2013 at 07:15 PM.
Rufio is offline   Reply With Quote
Old 17/07/2013, 12:09 PM   #2
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default Re: PAWN For Beginners : 1 - Understanding PAWN

Why do you tell new players to use strcmp? Rather tell them to use better methods available such as ZCMD or YCMD.
__________________
Jochemd is offline   Reply With Quote
Old 17/07/2013, 12:22 PM   #3
Twizted
High-roller
 
Join Date: Feb 2011
Location: Portugal
Posts: 1,475
Reputation: 289
Default Re: PAWN For Beginners : 1 - Understanding PAWN

Use ZCMD. Anyway, looking forward for your next tutorial, hopefully it's something to do with Variables.
Twizted is offline   Reply With Quote
Old 17/07/2013, 12:32 PM   #4
Team_PRO
High-roller
 
Team_PRO's Avatar
 
Join Date: Aug 2012
Location: Philippines
Posts: 1,816
Reputation: 87
Default Re: PAWN For Beginners : 1 - Understanding PAWN

well nice work and explaining it
mabye you should add like

Code:
public OnPlayerConnect(playerid)
{
        SendPlayerMessage(playerid, COLOR_RED, " Welcome to our server "%s" ! Have fun ! :)"pName );
    return 1;
}
Something Like Adding Names to it
sorry if wrong already forgot to script
__________________
Retired Player, Since 2009 Player
Team_PRO is offline   Reply With Quote
Old 17/07/2013, 01:06 PM   #5
Sublime
High-roller
 
Sublime's Avatar
 
Join Date: Jul 2013
Location: 6ix god
Posts: 997
Reputation: 223
Default Re: PAWN For Beginners : 1 - Understanding PAWN

You should probably refer from the Pawn Documentation Guide for future enhancements of this tutorial/next part of the tutorial.
Sublime is offline   Reply With Quote
Old 17/07/2013, 03:50 PM   #6
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default Re: PAWN For Beginners : 1 - Understanding PAWN

Quote:
Originally Posted by Jochemd View Post
Why do you tell new players to use strcmp? Rather tell them to use better methods available such as ZCMD or YCMD.
I started with strcmp 5 years ago I guess. I will tell ZCMD and YCMD in the next tutorial. Thought it would be better to start with strcmp. Thank you for your suggestion.

@Twizted, Thank you

@Team_PRO, I will tell about adding names in the next tutorial too. Thank you for your suggestion.

@Sublime, Thank you. I will try to have a look on this.
Rufio is offline   Reply With Quote
Old 18/07/2013, 03:13 AM   #7
TDM
Banned
 
Join Date: Jul 2013
Location: Screw h8trs
Posts: 193
Reputation: 50
Default Re: PAWN For Beginners : 1 - Understanding PAWN

nice tut
Reallu helped
TDM is offline   Reply With Quote
Old 18/07/2013, 10:43 AM   #8
AndreT
Gangsta
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 966
Reputation: 414
Default Re: PAWN For Beginners : 1 - Understanding PAWN

I agree that beginners need to be told about more efficient ways to handle commands, but the command "processors" that use CallLocalFunction only become favorable when there are more commands than just a few. CallLocalFunction, too, uses string comparisons, but does it using binary search resulting in logarithmic compexity. But it does not search from amongst your commands, but all public functions registered with the AMX (everything such as native callbacks, timers, etc. are included in the search).

So for just a few commands I would favor using string comparisons in OnPlayerCommandText. Especially when the code is created by or for beginners who could find the idea of callback hooking or includes confusing!
AndreT is offline   Reply With Quote
Old 18/07/2013, 04:41 PM   #9
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default Re: PAWN For Beginners : 1 - Understanding PAWN

@TDM I'm glad if I helped you

@AndreT this is a bit complicated to me lol
Rufio is offline   Reply With Quote
Old 18/07/2013, 04:58 PM   #10
KingHual
Banned
 
Join Date: Jun 2010
Location: Bulgaria
Posts: 2,294
Reputation: 1389
Default Re: PAWN For Beginners : 1 - Understanding PAWN

Quote:
It has got a compiler which helps you finding errors and warnings which prevents your script working good
No, that is NOT what a compiler is used for. A compiler compiles source code into object code (binary).

Quote:
We will make a gamemode so we should delete these lines :

pawn Code:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT
Why SHOULD you delete comments? They do not affect the code in ANY way. The definition has already been commented, so it's being ignored.

Quote:
and also do NOT forget to delete this :

pawn Code:
#endif

You must delete this even if it is a filterscript.
So what you're saying is, even if we check whether a condition is true using #if, we should not use #endif to end the if statement?

Quote:
Function is a part of scripting. It tells the script what to do in which callback.
These are called instructions, not functions.

PS: These are just a few of the errors in your tutorial.
KingHual 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
[Plugin] Vectoral Pawn - STL Data containers for pawn Rancho Plugin Development 69 26/03/2019 12:50 AM
[Tool/Web/Other] PAWN Scanner - Scan PAWN files for information. Slice Tools and Files 10 15/07/2012 08:09 PM
New to pawn, trying to convert some C code to pawn CHC Scripting Help 2 22/06/2012 06:09 AM
[Help With Pawn] Pawn and compiler is not working..... D_Less Client Support 3 09/03/2012 05:49 AM
[HELP] [pawn][/pawn] bbcode for phpbb3 Kasura Help Archive 4 20/11/2010 05:35 AM


All times are GMT. The time now is 03:13 AM.


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