Author Topic: How to code SA-MP Pawn [Tutorial]  (Read 38463 times)

0 Members and 1 Guest are viewing this topic.

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
How to code SA-MP Pawn [Tutorial]
« on: February 12, 2010, 09:03:06 pm »
Introduction
There is hundreds of tutorials on the web to help you learn to code. Whether it be PHP, C or even PAWN. This tutorial though is for our regulars at AdrenalineX. A quick overview of how easy adding new features to the server can be.

Tools Required
You don't need much to get to started, just download the SA-MP Server files and you're ready to go.
Extract this file to create a new folder, the server folder.
Contents Of Server Folder

Quote
pawno : The scripting folder, this contains the pawno.exe application that's used to script Pawn.
server.cfg : This contains all the setup data for the server, including gamemode, filterscripts and plugins.
gamemodes : The gamemode is the main script that is hosted.
filterscripts : Filterscripts are extra scripts needed by the server, like Anti-Cheat scripts for example.
plugins : These are server side plugins, .dll files that hook into the server from the host server.
server.exe : The server application, double click to start server.
samp-npc.exe : The NPC (Bot) application. Used by server.exe only. Each both had it's own samp-npc.exe process.
announce.exe : To announce the server on the SA-MP Master List. Used by the server.exe only.

Getting Started
Inside the pawno folder is an application named 'pawno.exe', double click it to load the window. Click 'file' then 'new' to auto create a base for your script.
NOTE: Net Framework 1.1 is needed, as it uses it to compile the script.
Pawno Apllication

On the right, is the SA-MP functions. Goto the line you would like to add the function to, then double click the function from the list on the right. This will happen :

This function isn't complete yet, you will see on the bottom of your screen the extra info (params as they are called) need to complete the function.

As you can see, it needs a playerid and a float value for the armour.
Code: [Select]
SetPlayerArmour(playerid, 50.0);The ; is needed to let the script know the function has ended.

As you can see, the function is complete and when the player spawns, it will set the players armour to 50.0.

Tesing Code
Now we have started to script, we should test it to see it in action. Click 'file' and then 'save as'. The script needs to saved into the 'gamemodes' folder. Select a name to save as, I'm going to call it TestCode. Press F5 to compile the script.
In your main server folder, open the file named server.cfg and edit the gaemodes line.
Quote
gamemode0 gtl 1
You should change it to your script name, I called mine TestCode.
Quote
gamemode0 TestCode 1
While you have the server.cfg open, edit the rcon password.
Quote
rcon_password mypassword
Go back to your server folder and start the server by double clicking server.exe.

Add your local server I.P to your SA-MP client. By default the I.P and port will be 127.0.0.1:7777.
Join the server.


Callbacks
SA-MP callbacks are easily described as triggers, the player triggers an event that contacts the server.  Along with the triggered event, extra params are sent, thus enabling the server to take the data from the event.
Quote
public OnPlayerDeath( playerid, killerid, reason )
{
   GameTextForPlayer( playerid, " WASTED ! ",5000,0);
   GameTextForPlayer( killerid, " YOU KILLED HIM ! ",5000,0);
   printf("[[debug] Player %d got killed by %d    reason id %d ",playerid, killerid, reason );
    return 1;
}

Custom Callbacks
Along with the SA-MP Callbacks, you can also create custom callbacks to track player changes. A good example is OnPlayerHeal( playerid, Float:increase ).
The above on it's own though it's pretty useless as all you're only telling the server the players ID and a float value.
Under your include <a_samp> add the following line:
Code: [Select]
forward OnPlayerHeal( playerid, Float:increase );The above gives the compiler an address to call, basically when OnPlayerHeal is called, it won't give any errors.
Now create the actual custom callback, but keep it empty for now.
Quote
public OnPlayerHeal( playerid, Float:increase )
{
    return 1;
}
Unlike the SA-MP Callbacks, we have to manually code when they are triggered. OnPlayerHeal, the player can be healed at any time, so the best solution here is a timer. Creating a timer is very easy, it even appears on the right of the Pawno application.
Under OnGameModeInit() (this callback happens only when the server is started/restarted) you need to add the code to create a timer.
Quote
public OnGameModeInit()
{
    SetTimer( "HealthTimer", 1000, 1);
    return 1;
}
Function Name : This is the timers function name.
Time delay : 1000 is 1 second, 100 is a 10th of a second.
Repeat : Repeat the timer (0 = no repeat, 1 = repeat)
Now we need to create some new variables to store the health data.
Under your include <a_samp> add the following line:
Code: [Select]
new Float:playerHealth[MAX_PLAYERS][2];The new identifies it as a new variable. Float: lets it also know it's a float value (56.44). playerHealth is the name we're going to call the variable. The [MAX_PLAYERS] states that is can store data for all players (ID). The [2] means it has two storage places (two values to the same variable name).
Now we can add the code that will let the server know if the player has healed.
Quote
public HealthTimer() 
{
    // now we have to scan all connected players, to
    // see if their health has changed.  This requires a loop

    for(new i; i < MAX_PLAYERS; ++)
    {
        // the above loops through all player ID's
        // the i we now define as the playerid

        GetPlayerHealth( i, playerHealth[ i ][ 0 ] );
        if( playerHealth[ i ][ 0 ] >  playerHealth[ i ][ 1 ] )
        {
            // player found with health increased, call OnPlayerHeal
            OnPlayerHeal( i, playerHealth[ i ][ 0 ] - playerHealth[ i ][ 1 ] );
        }
        playerHealth[ i ][ 1 ] = playerHealth[ i ][ 0 ];
   }
    return 1;
}
« Last Edit: February 21, 2010, 03:07:22 pm by [UK]Rac3r »

Offline ﱡ קּﻰﺢ Love

  • Admin
  • Posts: 1,936
  • Elements of life: Air, Water, Oil, Fuel
    • View Profile
    • LSR Forum
Re: How to code SA-MP Pawn [Tutorial]
« Reply #1 on: February 12, 2010, 09:08:31 pm »
Nice Rick.Maybe someone else might get interested too and jump in it. :)
Sign-A-True

Offline [MAF]PyroFox

  • Leader
  • Posts: 1,426
  • ¯\(°_o)/¯
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #2 on: February 13, 2010, 12:56:52 am »
Nice! It's very easy to learn all the basics of pawn, you can learn how to make a cmd to spawn cars, make textdraws, teleport commands, weapon commands, kick/ban, they're all very easy, and it's a good way to start learning it. You could make a decent freeroam/stunt server out of learning all of that. The sa-mp wiki is good for the basics but that's about it. It gets a bit harder from there on. I've tried to learn more but I can't get my head around some things coz the wiki doesn't explain it very well, or you can try to look at the source of some gamemodes and try to understand it, I learned a lot from doing that. But still, it's a good idea to learn a bit about it anyways even if you don't have any plans to script regularly coz you can learn more about sa-mp and what's possible and what's not and therefore give better suggestions to the scripters.
« Last Edit: February 13, 2010, 12:59:05 am by PyroFox »

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #3 on: February 13, 2010, 07:54:25 am »
I hope so Love, always healthy to have coders  ;D

You're right Pyrofox, that's exactly how I learned how to code also. I had no passed knowledge of any coding language before I started coding Pawn, with a little help from released scripts, I soon got my head around what does what and why.

Offline [MAF]falky

  • Admin
  • Posts: 2,750
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #4 on: February 13, 2010, 01:32:38 pm »
same

Re: How to code SA-MP Pawn [Tutorial]
« Reply #5 on: February 14, 2010, 09:18:17 am »
Weee  nice tutorial   :D :D

Offline [MAF]Sighmoan

  • Admin
  • Posts: 1,599
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #6 on: February 15, 2010, 02:22:58 pm »
I'm sure you all know about it already, but i am doing some simple reading up on it here too:

http://wiki.sa-mp.com/wiki/PAWN_tutorial_1

Right, i have started to have a read about and i have simple question. Is pretty much all stored data (racer login's, race records, buildmode race co-od's etc) manipulated with these:

io_write
io_read
io_append
io_readwrite

NOTE: Net Framework 1.1 is needed, as it uses it to compile the script.
Also, my functions aren't showing on the right hand side in pawno? Help! I have extracted the server stuff into a folder called SAMP_SERVER in the root of the GTA SAN ANDREAS folder in program files.

Will version 3 work ok do you know?
« Last Edit: February 16, 2010, 10:17:30 am by [UK]Simon »

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #7 on: February 17, 2010, 06:37:21 pm »
You need to run pawno.exe as administrator.

The net framework should work the same, go for it.

Offline [MAF]Sighmoan

  • Admin
  • Posts: 1,599
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #8 on: February 23, 2010, 03:25:17 pm »
I must be doing something wrong as when i do this:

Code: [Select]
public OnGameModeInit()
{
    // People can spawn with either the CJ skin or The Truth skin.
SetGameModeText("Blank Script");
AddPlayerClass(0, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0);
    AddPlayerClass(1, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0);
    return 1;
}

It is letting me select from the CJ or TT, but it isn't showing me their skins in the skin selector?

Offline [MAF]mooman

  • Leader
  • Posts: 6,299
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #9 on: February 23, 2010, 03:47:20 pm »
you have to make the camera point at them or something
will read and answer your forum PMs when I'm less busy!

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #10 on: February 23, 2010, 04:08:44 pm »
Yes, you need something like this:

Code: [Select]
public OnPlayerRequestClass(playerid, classid)
{
switch(classid)
{
case 0://cj
{
SetPlayerInterior(playerid,14);
SetPlayerPos(playerid, -1854.4232,27.9001,1061.1436);
SetPlayerCameraPos(playerid,-1857.2335,26.3764,1061.0);
SetPlayerCameraLookAt(playerid, -1854.4232,27.9001,1061.1436);
SetPlayerFacingAngle(playerid,125.0);
}
    case 1://truth
{
SetPlayerInterior(playerid,8);
SetPlayerPos(playerid, 2806.5720,-1171.5488,1025.5703);
SetPlayerCameraPos(playerid, 2809.7527,-1170.0004,1024.9);
SetPlayerCameraLookAt(playerid, 2806.5720,-1171.5488,1026.0);
SetPlayerFacingAngle(playerid,288);
}
}
return 1;
}

Offline [MAF]Sighmoan

  • Admin
  • Posts: 1,599
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #11 on: February 24, 2010, 02:14:25 pm »
Ok done that and now the players spawn in a weird place, so i guess i have to set that on the onplayerspawn (or whatever, i am not in front of it now) call?

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #12 on: February 24, 2010, 05:37:51 pm »
Yep, OnPlayerSpawn you need a few functions:

SetPlayerPos( player, x, y, z);
SetPlayerFacingAngle( playerid, a);
SetCameraBehindPlayer(playerid); // this resets the camera angle you set in skin select

Offline [FSR]Ush

  • Admin
  • Posts: 13,449
  • Aart
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #13 on: February 24, 2010, 05:55:47 pm »
Mmm, this seems interesting :)

Offline [MAF]Sighmoan

  • Admin
  • Posts: 1,599
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #14 on: February 26, 2010, 09:09:05 am »
Will anyone be interested if i document what i do and learn so anyone else wanting to learn can use it?

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #15 on: February 26, 2010, 10:04:52 am »
Hope so Simon, that's the reason I did this tutorial.

It's a golden gamble.

Offline [MAF]Sighmoan

  • Admin
  • Posts: 1,599
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #16 on: February 26, 2010, 10:23:55 am »
I will do then. I haven't really started in earnest but will make notes on the small simple things i have learn so far and you can amalgamate anything you think prudent into your initial post?

Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #17 on: February 26, 2010, 07:24:04 pm »
Sure.

Offline ﱡ קּﻰﺢ Love

  • Admin
  • Posts: 1,936
  • Elements of life: Air, Water, Oil, Fuel
    • View Profile
    • LSR Forum
Re: How to code SA-MP Pawn [Tutorial]
« Reply #18 on: February 26, 2010, 09:55:36 pm »
It will be like apply on the way guide i think.Would be a nice side help to your guide Rac3r.I see youve added new stuff, nice! But i didnt read any bit of it. :L
Sign-A-True

Offline [MAF]MrHess

Re: How to code SA-MP Pawn [Tutorial]
« Reply #19 on: February 26, 2010, 11:37:01 pm »
I still cant stand that stuff, all I can do is text commands XD, I want to open SK server to run @ home, but I need to fix the modes to work on 0.3 and cba to do it, so if somebody wants to gimme a lil hand..... :P
I would much rather be hated for what I am than loved for something that I'm not

Re: How to code SA-MP Pawn [Tutorial]
« Reply #20 on: February 27, 2010, 01:26:29 pm »
Next tutorial is about public OnPlayerConnect and public OnPlayerDisconnect.

1.
Code: [Select]
public OnPlayerConnect(playerid)
{
new player[100];
GetPlayerName(playerid,player, 24);
format(player, 100, "%s has joined the server.",player);// Player Name
SendClientMessageToAll(0xA52A2AAA, player);// Color

SendClientMessage(playerid,0xA52A2AAA,"Welcome to blabla..... ");// SCM

GameTextForPlayer(playerid,"Welcome to Adrenaline~r~X ~w~server.",5000,5);//GT

ShowPlayerDialog(playerid, dialogid, dialogstyle, "welcoming title", "Welcome\nBlablan\nBlabla", "button1", "button2");//  \n = next line

return 1;
}


Game Text: http://wiki.sa-mp.com/wiki/GameTextStyle



2.
Code: [Select]
public OnPlayerDisconnect(playerid, reason)
{
new player[100];//name
GetPlayerName(playerid, player, 30);
switch (reason) {
case 0: format(hrac, 100, "%s has  left the  server.",player);// Reason 1
case 1: format(hrac, 100, "%s has  left the  server.(Timeout)",player);// Reason 2
case 2: format(hrac, 100, "%s has  left the  server (Kick/Ban)",player);// Reason 3
}
SendClientMessageToAll(0xA52A2AAA, player);//color

return 1;
}

This function are easy :) 


Offline [MAF]Rac3r

  • Leader
  • Posts: 2,807
  • Well, this is embarrassing.
    • View Profile
Re: How to code SA-MP Pawn [Tutorial]
« Reply #21 on: May 20, 2012, 04:03:34 pm »
Bump:  Max, here's a nice tutorial to get started.

Offline [MAF]Cromiell

Re: How to code SA-MP Pawn [Tutorial]
« Reply #22 on: May 20, 2012, 04:41:13 pm »
lol Max coding, heh good luck. Even I can do a lot of nice stuffs in website source I have no clue about PAWN. I mean I know how it works but I don't know how to script stuffs there. I'm "learning" since 1 year. :L By the time I would have had already known a lot but I am just too lazy.

Offline [MAF]Karlis

Re: How to code SA-MP Pawn [Tutorial]
« Reply #23 on: May 20, 2012, 04:49:36 pm »
if max really knows basics of C++ like he says this should be easy.

EDIT: nice and short documentation, helped me to get started: http://thedarkjoker94.cer33.com/?p=1017
« Last Edit: May 20, 2012, 04:52:08 pm by [MAF]Karlis »