[Tutorial] New Scripting tutorials
#1

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. 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.
http://www.sa-mp.com/download.php
Select 'SA-MP Windows Server Download'
Extract this file to create a new folder, the server folder.
Contents Of Server Folder


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

__________________________________________________ _____________________________
You may +rep for me if i help thanks a lot.
Reply
#2

Use blocks for showing code.There are already plenty of these tutorials.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)