PAWN for Beginners

From SA-MP Wiki

Jump to: navigation, search


Image:Sshot-1.png
The 'samp folder.

Welcome to your very first tutorial about PAWN Scripting Language and PAWNO Program. This tutorial is designed to help beginners (newbies, noobs) to understand how PAWN works and how can you create a simple Server Script. When you finish this tutorial, you'll have a fully-functional Deathmatch script. Before starting, be sure you have this material:

  • SA-MP Server installed
  • Functional GTA:SA US/EU game copy
  • PAWNO Program (pawno.exe)
  • All this stuff working correctly on the GTA:SA Folder.

I recommend you create a Folder inside your GTA:SA Root folder with the name 'samp' and put all the material there. This will make easier the lesson as I use the following configuration: \SA Root\samp\ and all the other Folders that are in there (gamemodes, filterscripts, pawno, scriptfiles). So, you're equipped, let's begin.

Contents

What is PAWN and PAWNO?

According to Wikipedia, Pawn is a programming language that is embedded into other programs, for example, Half-Life or Quake, in our case, SA-MP. CompuPhase SMALL is a simple, typeless, 32-bit extension language with a C-like syntax. It is designed to be useful as an embedded systems programming language and is distributed under a liberal zLib/libpng license. This language is useful as a safer environment within or alongside the C programming language, for instance as a scripting language in games programming or on resource-limited systems. Now, however, it has been renamed to PAWN.

Now that we know what is PAWN, we can continue with PAWNO. PAWNO is the program used to code and write PAWN code. It's a simple IDE made to edit PAWN files. You can still use other IDE such as Notepad++, Sublime Text 2 or 3, Visual Basic, so on. You just need to know how to setup custom compiler and that's it. See below to setup your Notepad++ or Sublime Text to edit and compile PAWN scripts.

Starting

Image:Sshot-2.jpg
Your first PAWNO screen.

Go to your SA root folder, and search your brand-new folder, maybe called 'samp' or other thing. You'll see some .exes, some text files and some folders. Go right to the 'pawno' folder. Open pawno.exe, then click new on the toolbar. Yep. Your first SA-MP server script. Of course the server can work with this 'blank' script, but can you imagine a server full of unarmed CJ's running everywhere? That's not the kind of server we want. We want something with a lot of spawnpoints, some commands, multiple player models and deathly weapons. We need to code. We'll start coding in PAWN language.

The classes and spawnpoints

First we're gonna talk about the classes. The classes are how the Game or server call every player model. So, if you want to know, CJ is class 0. You can find the classes in peds.ide, but there's a better way to find them, using SA-MP Debug. Run debug and start changing classes with F11 and F12. When you find your class, you can run or spawn a vehicle (see Debug Guide for more info) and go to your new class spawnpoint. Type /save in the chat prompt (accesible typing F6 or T) and that coordinates, player class, facing angle and other info will be saved in PAWN-ready format. To put a commentary to easily know what class or spawnpoint is it, type /save [comment]. Cool, isn't it?

You already set all your spawnpoints with their respective classes? Well, so let's continue. Go to your GTA:SA root and search for a file called 'savedpositions.txt'. Open it and you'll see your saved Spawnpoints in PAWN-ready format, and maybe with a commentary. Go to pawno folder and execute pawno.exe. Search for a block of words like this one:

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;
}

That's the OnGameModeInit callback. (See here for more info about callbacks and functions) Now let's add our own spawnpoints, shall we? Delete that

AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
Image:Sshot-3.jpg
Example of 'savedpositions' file.

And add yours, that may look the same but with different data. Now, let's try it! Save your PAWN file into \samp\gamemodes\ and then click the button inmediatly on the left of the blue arrow (This will compile .pwn files into .amx, readable for the server). Now go to your 'samp' folder and open 'server.cfg'; replace the gamemode line with 'gamemode [your .pwn file name]', save and close, now run 'samp-server.exe'. Go to SA-MP client and add to the favorites this IP: 127.0.0.1. This is your local testing IP. It should say Unnamed SA-MP 0.2.2 Server, and as Gamemode 'Blank Script'. That's ok for now. Now connect. Surprise! What can you see? That classical electric-stairs spawpoint. Doesn't matter, change your class and you'll see you'll spawn at your designated place. Now we'll start modding more Script parts to personalize the server.

Introduction on modifying more Callbacks

The callbacks are sections of code run by the server, so if you want to show a big message on the screen when a player leaves, enters or gets killed you would put the code for that there. All the callbacks the game uses (except 2, but they are used by filterscripts, not gamemodes and are not covered here) are in your file, you cannot make up your own. Most of them appear as someones under do, but some (i.e. "OnPlayerCommandText", "OnPlayerRequestClass", "OnGameModeInit" and "main" (which is not strictly a callback and never has more than your game title) have other bits in already, either to make the game work or to show you what to do there. You may also use other, similar blocks (as we are about to) which look the same but are called functions, these have to be called by you yourself. 1

The basic server callbacks are these ones:

main()
{
	print(!" -> My script loaded! <-");
}
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 OnPlayerRequestSpawn(playerid)
{
	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 OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/mycommand", cmdtext, true, 10) == 0)
	{
		// Do something here
		return 1;
	}
	return 0;
}

Every server script has modifyed that callbacks to make a fun-playable Server. We'll do the same now.

Vehicle spawns

Now let's add some vehicles. The vehicle spawn lines are put in the OnGameModeInit callback, aswell as the player classes spawnpoints, the Gamemode name and other stuff. We'll do that later. Most of the commands in PAWN do pretty much exactly as they're named, we want to add a vehicle so lets look for vehicle commands:

CreateVehicle();
 
AddStaticVehicle();

If you click on either of these and look in the status bar (the bar at the bottom of the editor) you will see their syntax (what information/parameters you need to give them to work):

[a_samp.inc] native CreateVehicle(vehicletype, Float:x, Float:y, Float:z, Float:rotation, color1, color2, respawn_time);

[a_samp.inc] native AddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:z_angle, color1, color2);

Now they both look equally as good, but this is where prior knowledge comes in, CreateVehicle only places one car ingame, AddStaticVehicle creates a vehicle spawn, so when the car is destroyed or desynched it will reappear back where you placed the spawn. These need to be created at the start of the game so you would place this in your "OnGameModeInit()" callback.

List of available car colors
Enlarge
List of available car colors
The modelid/vehicletype numbers can be found in your vehicles.ide file, the Floats are just non whole numbers, basically co-ordinates and angles, and the color numbers can be found in your carcols.dat file, look for the reference number for the color you want, so "black" is "0", "taxi yellow" is "6", "currant blue poly" is "32" etc, "-1" means random (from that car's default colors, found lower in the carcols.dat file). If you want to get the position for a vehicle, go into debug mode, select your vehicle, get in and type "/save" then open up "savedpositions.txt" and copy the line. If you want to spawn a vehicle in debug mode, type "/v <vehicleid>" ingame and that vehicle will spawn (vehicleid is taken again from vehicles.ide), or type "/vsel" to bring up the vehicle command screen.

I got these co-ordinates:

2040.2279, 1344.4127, 11.0, 3.5436

Note: These are called floats, 11.0 is a float despite being whole, any whole number being used as a float must have a trailing ".0" to denote it as a float. These numbers are in the english format of using a decimal point ("."), commas are used to separate parameters. Also remember the 4th number is angle, so if we now add the following line to the game mode in OnGameModeInit, recompile and test, we will get a bright pink infernus outside the casino which is the default spawn position for CJ. Note: on a car which uses the secondary color (such as a cop car (id 596)) this would be a pink and blue car as that is the secondary color.

AddStaticVehicle(411, 2040.2279, 1344.4127, 10.6719, 3.5436, 126, 54);

Now you can easily go about saving positions and creating as many cars as you want (up to an engine defined limit of 700 individual cars, 70 different types (models) of cars). The spawns saved to the file will have "-1, -1" as the colors by default. 2

References

  1. Took from PAWN tutorial article. Go there for more info.
Personal tools
Navigation
Toolbox