05.02.2015, 09:38
NOTE: This tutorial is aimed towards the less knowledgeable coders of SA-MP/PAWN and will seem obvious and boring to others.
Most of you probably know what stock functions are. They can speed up your coding greatly. I'm going to show you how to make a few and how they can be used.
The first one I'll talk about is "PColor":
Possibly one of the shortest pieces of code you've ever seen. This simply returns the color of a player. So rather than having to type "GetPlayerColor(playerid) >>> 8" every time, you simply use this:
The next stock function is "SetPlayerPosEx".
When teleporting a player, we usually check if they're in a vehicle first, so we don't remove them from their vehicle whilst teleporting them. If you have a lot of teleport commands in your script, it'll take a while to type out the above coding every time, so we simply use SetPlayerPosEx instead. Also, this function allows you to enter a facing angle for players or a z angle for vehicles.
Example:
A related stock function to the above would be "GetPlayerFacingAngleEx":
With this function, we can check whether the player is in a vehicle, and accordingly choose whether to use GetVehicleZAngle or GetPlayerFacingAngle. Saves a lot of typing!
On almost every command you'll create in your script, you will need to check for errors in the command's usage and subsequently send an error message. You would usually type something like:
But we can shorten this and save time every time we have to send an error by using this function:
Which changes our error sending to:
It also ensures that our error messages are consistent throughout all of our commands in terms of color and styling.
So, there are some stock functions for you to use and hopefully gain some ideas from. Stock functions basically come in handy when
1) having to write out a large chunk of code that clutters up a certain callback, and
2) having to write out a certain bit of code over and over again.
Most of you probably know what stock functions are. They can speed up your coding greatly. I'm going to show you how to make a few and how they can be used.
The first one I'll talk about is "PColor":
pawn Код:
stock PColor(playerid)
{
new color = GetPlayerColor(playerid) >>> 8;
return color;
}
pawn Код:
format(string, sizeof(string), "{%06x}", PColor(playerid);
The next stock function is "SetPlayerPosEx".
pawn Код:
stock SetPlayerPosEx(playerid, Float:x, Float:y, Float:z, Float:a)
{
if(IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
SetVehiclePos(GetPlayerVehicleID(playerid), x, y, z);
SetVehicleZAngle(GetPlayerVehicleID(playerid), a);
}
else
{
SetPlayerPos(playerid, x, y, z);
SetPlayerFacingAngle(playerid, a);
}
return 1;
}
Example:
pawn Код:
CMD:teleportme(playerid, params[])
{
SetPlayerPosEx(playerid, 0.0, 0.0, 0.0, 270.0);
return 1;
}
A related stock function to the above would be "GetPlayerFacingAngleEx":
pawn Код:
stock GetPlayerFacingAngleEx(playerid, Float:a)
{
if(IsPlayerInAnyVehicle(playerid))
{
GetVehicleZAngle(GetPlayerVehicleID(playerid), a);
}
else
{
GetPlayerFacingAngle(playerid, a);
}
return 1;
}
On almost every command you'll create in your script, you will need to check for errors in the command's usage and subsequently send an error message. You would usually type something like:
pawn Код:
CMD:ban(playerid, params[])
{
if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: {FFFFFF}You don't have permission to use this command!");
pawn Код:
stock Error(playerid, text[])
{
new string[144];
format(string, sizeof(string), "{FF0000}ERROR: {FFFFFF}%s", text);
SendClientMessage(playerid, -1, string);
return 1;
}
pawn Код:
CMD:ban(playerid, params[])
{
if(!IsPlayerAdmin(playerid)) return Error(playerid, "You don't have permission to use this command!");
So, there are some stock functions for you to use and hopefully gain some ideas from. Stock functions basically come in handy when
1) having to write out a large chunk of code that clutters up a certain callback, and
2) having to write out a certain bit of code over and over again.