[Tutorial] PAWN For Beginners : 1 - Understanding PAWN
#1

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 Код:
// 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 Код:
#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 Код:
#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 Код:
#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 Код:
#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 :

Код:
Pawn compiler 3.2.3664	 	 	Copyright © 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 Код:
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 Код:
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 Код:
public OnPlayerConnect(playerid)
is a callback which we will talk about later.

pawn Код:
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 :

Код:
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 Код:
OnPlayerConnect
means when a player connects

pawn Код:
OnPlayerDisconnect
means when a player disconnects

pawn Код:
OnPlayerSpawn
means when a player spawns can be any reason. Connecting, dying etc.

pawn Код:
OnPlayerDeath
means when a player dies.

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


They are :

pawn Код:
main()
and
pawn Код:
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 Код:
#define
We are going to define COLOR_RED as we wrote COLOR_RED.

It should be

pawn Код:
#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 Код:
#define COLOR_YELLOW 0xE0E377AA
it is same. Hope you understood !

Includes


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

pawn Код:
#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 Код:
#include <include name>
write your include name between < and >


9) Writing "Hello World!"


Lets make a command.

For so, we will use :

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
callback. we currently have 1 ready command. We will edit it a bit.

it is :

pawn Код:
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 Код:
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 :

Код:
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 Код:
forward SetPlayerTeamFromClass(playerid, classid);
but wrote:

pawn Код:
forward SetPlayerTeamFromClass();
you will get this error.

the most common error :

Код:
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 !


Код:
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


Код:
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 ;


Код:
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
Reply
#2

Why do you tell new players to use strcmp? Rather tell them to use better methods available such as ZCMD or YCMD.
Reply
#3

Use ZCMD. Anyway, looking forward for your next tutorial, hopefully it's something to do with Variables.
Reply
#4

well nice work and explaining it
mabye you should add like

Код:
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
Reply
#5

You should probably refer from the Pawn Documentation Guide for future enhancements of this tutorial/next part of the tutorial.
Reply
#6

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
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.
Reply
#7

nice tut
Reallu helped
Reply
#8

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!
Reply
#9

@TDM I'm glad if I helped you

@AndreT this is a bit complicated to me lol
Reply
#10

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 Код:
// 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 Код:
#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.
Reply
#11

Quote:
Originally Posted by Rufio
View Post
PAWN is a programming language which is based on C. PAWN is used by many games including SA-MP. It has got functions and callbacks just like any other programming language !
PAWN is a typeless scripting language.
PAWN callbacks differs from regular calbacks known from another programming and scripting languages. The AMX handles the scripted callbacks itself.

Quote:
Originally Posted by Rufio
View Post
[B][I][CENTER]Pawno is a programming language application which helps us creating PAWN scripts. It has got a compiler which 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.
Pawno is an editor, which helps you to create PAWN scripts. pawncc.exe is an extension to compile your PAWN scripts.

Quote:
Originally Posted by Rufio
View Post
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 :

pawn Code:
#define FILTERSCRIPT
This is only needed, if some includes actually requires information about where to initilialize code, otherwise it's not really necesarry.

Quote:
Originally Posted by Rufio
View Post
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 :
Callbacks are there to handle events procedural called by the server.
Reply
#12

Quote:
Originally Posted by king_hual
View Post
No, that is NOT what a compiler is used for. A compiler compiles source code into object code (binary).



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.



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?



These are called instructions, not functions.

PS: These are just a few of the errors in your tutorial.
Well, thank you for your answer. First of all, this is a BEGINNERS guide. So I am pretty sure they will understand 'Compiler checks errors' better. Should means a suggestion. I suggested it for helping the gamemode look better. Actually, I always delete it. I don't like how it looks like at the top of our script. Sorry for it. Seems like i wrote wrong there. I was a bit sleepy. You should NOT delete it if there is an #if statement. And I didn't know they were called instructions, I just guessed it as it is called 'functions' in almost all other programming languages. Thank you again for telling me my mistakes ^^


Quote:
Originally Posted by BIGETI
PAWN is a typeless scripting language.
PAWN callbacks differs from regular calbacks known from another programming and scripting languages. The AMX handles the scripted callbacks itself.


Pawno is an editor, which helps you to create PAWN scripts. pawncc.exe is an extension to compile your PAWN scripts.

pawn Code:
#define FILTERSCRIPT
This is only needed, if some includes actually requires information about where to initilialize code, otherwise it's not really necesarry.


Callbacks are there to handle events procedural called by the server.
Well, thank you for your answer too. I readed it was a C-based scripting language somewhere. Heard it was a mix of C,C# and C++. And actually it seemed like so to me too. But nvm, I will edit it as I am not sure.

I will edit the mistakes you posted. Thank you
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)