[Tutorial] Learning PAWN for Beginners
#1


A guide to the PAWN language used in SA-MP by Alonzo Torres.



This is for the guy or girl who wants to learn scripting and some concepts of programming. I am going to try to have lots of pictures, I'll explain everything but I won't write a book about every subject. I will treat you as you do not know anything about programming before. It is important to know how to open files, to write on a keyboard and to be able to read though. You are free to comment on mistakes I've made however do not attempt to tell me how I should do things, my highest priority is to teach beginners, not to please advanced coder's interest. Anyway without further ado, let's begin!



There are some essential things which you have to understand before learning PAWN or other languages. The first thing is, do not expect to be good at something from the start. We all started from nothing and everyone you meet which know how to code knows that you will have to dedicate a lot of your time to be able to learn even the basics. Please do encourage yourself to search for information and never ask for code, but answers. This way you'll learn more instead of copying code from others.

However, with this being said there's tons of benefits you'll get from learning coding. There's much more to get from it rather than the possibility to create your own server. Allow me to list a few of the reasons why YOU should learn to code:
  • You'll get better at troubleshooting. - Troubleshooting is A-Z in coding.
  • Your imagination is stimulated. - There's nothing stopping you but your imagination.
  • Improved patience. - Coding might improve your patience, it did for me.
  • You'll get to see the results of your code - The feeling you get when you see the results, amazing!

Who is Alonzo Torres?

I am Alonzo Torres, also known as Viktor. I decided to write this guide because I believe I've got enough technical skills and think I am a suitable teacher. I've been with SA-MP for many years, and coded PAWN for about two years. However, I did a lot of coding in other languages before PAWN. The first language I learnt was JAVA. I know a lot of languages and feel comfortable with programming and coding. I'm doing this as a hobby rather than a job.







Indeed, definitions and other information is important however you're here to learn how to script PAWN, not to get a bigger vocabulary. Heck! Saves me some time, and there's always ****** and www.thefreedictionary.com. The first thing you'll need is to download the latest server software for SA-MP under the tab called (depending of your OS of course) "SA-MP Windows Server Download" found here. Unzip the folder (if you don't know how to unzip a folder then all you have to do is to rightclick it and click on extract, then chose a location to save your files. The location of the files does not matter. Now if you did all this correctly you should have the following files in front of you (if not please drink a glass of water then re-read what I've typed:


NOTE: I FORGOT TO ADD DESCRIPTION ABOUT SCRIPTFILES: This map holds files related to the script, like vehicles, houses and/or player and more.

Now let's try if it works shall we? I bet you already did! And I also bet that the program just shut down directly! Well no worries, there's only one thing which has to be done to get your server up. And it's a pain trust me, nah kidding all you have to do is change one value in the file server.cfg:


WARNING: NOT SETTING UP A RCON PASSWORD WILL RESULT IN YOUR SERVER CRASHING.


Let's try to start out server now, shall we? If you did change your RCON password then your server should start. Also please note that your client has to be the same version as your server, otherwise it won't work.



As you can tell from the image above, the server loaded 7 filterscripts. All these can be found in the filterscripts folder and they all have its own purpose. Based on the name we can guess what they do, like skinchanger.amx probably makes it possible for us to change skin.

We will investigate this further. As part of the tutorial we will actually rewrite and take a look at the code we're given from the start. Of course, we will have to go through the basics of scripting PAWN first. I will teach you enough for you to jump into any code and understand what the purpose of it is. We also notice that 1781 vehicles are loaded, houses, businesses, banks and police related stuff.

Keep up the good work, we've already got the definitions done, we've downloaded the server and now we've also changed the RCON password and started the server!

We're getting closer towards touching code!

But before we touch the code and do some scripting we should take a look at how the current GM (Gamemode) works and what we possibly could change. Or even what features we could add! Now you might ask yourself if it's not better to start basic and learn each subject after subject and well it depends. I prefer to let you take a look at this product which is final and let you change how it looks, how it works and add something to it. That way you'll understand how powerful and fun it is. Learning things over and over again without testing them is boring and you will have a hard time learning. Of course, I will still teach you the basics after we've edited the GM you get from the server package by default, since you'll get to do your own GM!

Let's take a look at how it looks!

The first thing you'll notice is that it says that it SA-MP 0.3 Server and that it welcomed you to Grand Larceny. This is something we'll change really soon because well, we want to know how to do it. Please note though, if you want to start your own server, never use others code. Always be original, start with a DM server and move on from there. No one will like you if you use code others can download for free and/or if you use stolen code which might be copyfraud. Golden Rule: Never do edits (others scripts with some modification) - This is an action of the devil.

HOWEVER, feel free to download any script and use it to learn, that's the main purpose of released GMs in my option. Not to be used as edits by 12342 kids.

The next thing is a skin selection screen, there's probably where the skinchanger.amx comes in, right?
We'll see if that's true later!

Last but not least there's a lot of vehicles, businesses and everything which was loaded from the start, which we could see in the server's console.

So as a summary what we want to do first is to change the name of the server and the welcome text at the beginning. That's pretty cool isn't it? After that we'll go into the more advanced editing, like changing skins, vehicles or even manipulate businesses!

Let's carry on!

I now want you to open the pawno directory and let's take a look at what's inside:



Scared? Me too. Lots of files I know. But listen, we will only use 50% of the files here and the include folder will only contain some scripts. These scripts can be if you for instance wants to add the functionality to get the numberplate of a vehicle, which isn't a default function in SA-MP. What the hell is a function? Just remember that a function adds some functionality, it has a purpose and result in something. Further about functions will be discussed later on.

Set up properties for running as administrator
You'll now have to set up that you want PAWNO to run as administrator every time you start it. Why? Otherwise you'll get an annoying error and some functionalities will be disabled (actually it will work but there's a cool feature which is disabled if you don't). All you have to is to right-click and click on properties (at the bottom of the list). Then go to the Compability tab as shown below and tick that you want the program to run as administrator.






So yes, let's take off the blanket and reveal the first code. First open up PAWNO and click on the button on the image below. This will make it possible for us to open files with .pwn file exstension which includes PAWN code which we will modify:
You now want to open the grandlarc.pwn in your gamemodes folder.

It will look like this if you opened the correct file:


Let me go through the first 36 lines above and explain some keywords. Firstly:

#include: This will include some code from a file. The file exstension of this could be .inc or .pwn. The first include is a_samp.inc. This is an essential include as it includes the main functionality of SA-MP. It includes core.inc, float.inc, string.inc, file.inc, time.inc, datagram.inc, a_players.inc, a_vehicles.inc, a_objects.inc and a_sampdb.inc. Together all these files make it possible for us to script using the SA-MP standrad functionality. Later on we'll learn how to create our own includes.

#pragma tabsize 0: Please remove this. You will regret if you don't in the future. Using this you won't get warnings regarding loose indentation, a.k.a if you place code in a way which kind of is hard to read for the human being and don't indent correctly.

#define: This will simply define a word and give it the value next to it. I want you to make your own define on line 14, so instead of having #pragma tabsize 0 (which is for ignorant people) you'll have your very own define! All you have to do is to use the keyword #define followed by a name, and then a value next to it. I want the name of it to be SERVER_NAME and the value will be the name of your server.

pawn Код:
#define SERVER_NAME "My First Server"
new: This keyword is used to declare a variable. To declare something means you create it. There are multiple types of datatypes which can be used when creating variables. A variable is a container for something.



No! You are wrong! But don't worry it's actually really simple. Let me go through all the relevant datatypes or at least the ones you'll be using in the beginning.

INTEGER: This is the easiest datatype out there and if you look at line 25 there's a variable being declared called total_vehicles_from_files. This is an integer variable because it's value is a whole number, meaning it has no decimal point like .5 or .42 which FLOATS has. To declare an integer variable you need the new keyword, a name and then a value seperated by an equal sign (=). However, you don't have to have the value with the equal sign, you can simply write the name after the new keyword but don't forget to include the right datatype and end your statement with a semicolon ( ; ). A statement is simply an instruction like apples = 5;
EXAMPLE:

pawn Код:
new apples = 12;
KEEP IN MIND THAT WHEN DECLARING VARIABLES WE USE THE SEMICOLON ( ; ) at the end.
This code will declare a variable called apples with the integer value of 12.

FLOAT: A float is simply a number with a decimal point, like 24.23 or 1.5. To declare one you do the same thing as with an integer, however you hav to use the keyword Float: before the name, like this:

pawn Код:
new Float:PI = 3.14;
This declares a variable with the datatype Float called PI with the value of 3.14.

CONTINUES ON THE POST #3
Reply
#2

Half of the post doesn't match with the thread title.
Reply
#3

Array: This was at least for me a scary subject in the beginning however I can ensure you that even the most advanced arrays are actually quite simple. To illustrate my point please take a look at this train below:



Now Mr. Apple and Miss. PI wold like to take a trip together to a music festival. Noone of them knows how to drive a train, so there has to be a driver. So eventually it would look like this:



How can this be similar to a array you migth ask?

Well it's actually really simple, every wagon of the train has one seat for a person. We don't want to be racist so we let integers, floats and other variables to sit in these wagons. Now PAWN and other languages decided that the driver is not really included, however they will have to count him in, because he does take up some space on the track. So in coding it's actually \0 or NULL which is the value, it will take up one slot of your array, however you never count with it. Another important thing to notice is that the first wagon is 0, the next one is 1 and so on. So it doesn't start at one (1) but zero (0).


Notice how an array (train) actually don't have to fill out the slots (wagons) and also remember that you can have a lot of wagons, for instance most arrays uses the define MAX_PLAYERS, which can have 100s of wagons!

How do I build my own train?

It's really simple, first you have to ask yourself how many wagons you'll need, what could possibly be the MAX amount of passengers? The next thing you'll have to do is to add the driver of the train with the number of vehicles which will be equal to the size of your array. For instance: If I want two wagons then I'll have the 0 as apples, 1 as PI and then 2 which is the size of the array, will be the driver.

pawn Код:
new train[2];
Since arrays are zero-indexed (starting from 0 and goes up) you might think that there's three slots here, however, remember that the driver also takes up one slot of the track. So the code above is a array or a train with two slots or wagons. WARNING: ADDING MORE PASSENGERS THAN THERE ARE WAGONS WILL RESULT IN THE FOLLOWING ERROR: error 032: array index out of bounds


Now I want to make a train containing the english alphabet, let's attempt it shall we?

Now how many wagons are there in the alphabet? Well based on Wikipedia the modern English alphabet is a Latin alphabet consisting of 26 letters. This means, our train will have 26 passengers, and so the size will be 26! Now what we also have to do since we will be using text or as we will soon learn, a string... We have to add the size of the string. For one character like "A" we'll need 2 slots, one for \0 (NULL) and one for the character itself.

pawn Код:
new alphabet_train[26][2];
Adding passengers to the wagons!

Please add the code above to your code, right under line 23 - "#define CITY_LAS_VENTURAS 2". I then want you to go to line 48 or 49, and right after "print("---------------------------------------\n");" I want you to click on ENTER and then TAB, resulting your marker to be positioned here:



Now remember, your first wagon is 0, second wagon is 1 etc... To access the first wagon and give it a value you can simply do this (NOTE: You can exchange this value with the name of a variable you've created (If you mess up with this don't worry, just do as I do and I'll explain later on why you failed):

pawn Код:
alphabet_train[0] = "A";
Now I've added all the 26 characters to the train by doing this:

pawn Код:
main()
{
    print("\n---------------------------------------");
    print("Running Grand Larceny - by the SA-MP team\n");
    print("---------------------------------------\n");
    alphabet_train[0] = "A", alphabet_train[1] = "B",
    alphabet_train[2] = "C", alphabet_train[3] = "D",
    alphabet_train[4] = "E", alphabet_train[5] = "F",
    alphabet_train[6] = "G", alphabet_train[7] = "H",
    alphabet_train[8] = "I", alphabet_train[9] = "J",
    alphabet_train[10] = "K", alphabet_train[11] = "L",
    alphabet_train[12] = "M", alphabet_train[13] = "N",
    alphabet_train[14] = "O", alphabet_train[15] = "P",
    alphabet_train[16] = "Q", alphabet_train[17] = "R",
    alphabet_train[18] = "S", alphabet_train[19] = "T",
    alphabet_train[20] = "U", alphabet_train[21] = "V",
    alphabet_train[22] = "W", alphabet_train[23] = "X",
    alphabet_train[24] = "Y", alphabet_train[25] = "Z";
}

Let's take the train for a testride!
Under the "alphabet_train[25] = "Z";" snippet once indented (one tab) please write this:
pawn Код:
printf("The last letter in our alphabet is: %s", alphabet_train[25]);
printf: Prints something in the console like the normal print does, however with printf we can format the string (text) and by chosing the datatype of our variable using the keywords found below we can use our variable in the console.

Quote:
Originally Posted by SA-MP WIKI
%b Inserts a number at this position in binary radix
%c Inserts a single character.
%d Inserts an integer (whole) number
%f Inserts a floating point number.
%i Inserts an integer.
%s Inserts a string.
%x Inserts a number in hexadecimal notation.
%% Inserts the literal '%'


Now please click on the blue arrow and if everything works as it should you should get something like this:


The warnings "warning 203: symbol is never used:" are caused due to the fact that some variables in your script are not used, to solve this you can temporarly comment them out by adding "/*" at the start of your commenting and then "*/" at the end, like the picture below. We might want to use these variables in the future, so there's no point removing them completely!

Do this with thisanimid and lastanimid variables at line 41-42 too. If you get "Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase" when compiling again then you've done everything correctly so far!

Let's see if it works by starting our server!

As you can see it works, it got the last wagon of our train, which is Z!


Now that you know what an integer, a float, a string and an array is, I think we're ready to move on to the more advanced topics.



NOTE: Before continuing please type #define SERVER_VERSION 1.0 right under "#define SERVER_NAME "My First Server"".

Now what if we want to use a version in out name? Is it possible? Well yes it is, by using format. Under main you should put the following code:

pawn Код:
new name_of_server[42];
    format(name_of_server, sizeof(name_of_server), "hostname %s v. %.01f", SERVER_NAME, SERVER_VERSION);
    SendRconCommand(name_of_server);
We first create a string called name_of_server with the size of 42, it could be anything however if your server has a long name you might face a problem where some letters are let out. We then use format. The first thing we have to enter is that variable, we then enter sizeof() and between ( and ) we put the variable again. This tells the format that the train should fill as many seats as there are passengers and then shutdown the other places (add \0 to them). We then choose the text and the variable types we want to use. We want to use hostname because SendRconCommand uses it to change the name of the server. We then add %s ( a string) and %.01f ( a float with one decimal ) and after that all you have to do is to provide the defines for the server's name and version.



Go to line 76 and change it to this:

pawn Код:
SendClientMessage(playerid,COLOR_WHITE,"Welcome {88AA88}to{FFFFFF} my server at Grand{88AA88}L{FFFFFF}arceny");
Your assignment for this tutorial will be to use format to change all the versal (big) letters to the letters in your alphabet train.



CONTINUES AT POST #15
Reply
#4

Quote:
Originally Posted by Sublime
Посмотреть сообщение
Half of the post doesn't match with the thread title.
Never throw code at a rookie, I know the first post didn't have much code in it, but I had to clarify some things before I continued. Thanks for the feedback! I will think about that next time.
Reply
#5

Nice!
Reply
#6

Cool
Reply
#7

Awesome, u made this tut look cool with the pics and Trains Good Job.
Reply
#8

Professional!
Nice job!
Reply
#9

Array is hard to learn, I might take a look to this tutorial when I needed to, +repped.
Reply
#10

Thank you everyone for the feedback, I really do appreciate it!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)