05.11.2013, 08:04
(
Последний раз редактировалось AlonzoTorres; 05.11.2013 в 15:32.
)
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"
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;
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;
CONTINUES ON THE POST #3