Useful Functions

From SA-MP

Jump to: navigation, search

Contents

Useful Functions (uf.inc)

Here you can find useful functions for your pawno scripts. They are mostly copied from this topic from SA-MP Forums, these can all be implemented with the Useful Functions include file.

AddStaticPickup2

Allows the saving of models, types and pickup positions, it is essential for OnPlayerPickupItem


(model, type, Float:X, Float:Y, Float:Z)
modelThe modelid of the pickup to spawn
typeThe typeid of the pickup to spawn
Float:XThe x value of the spawn position
Float:YThe y value of the spawn position
Float:ZThe z value of the spawn position
AddStaticPickup2(50,2,29.5,36.5,49.8);

By: Sacky

AddStaticVehicle2

Enables saving of vehicleclasses to a script (Alot of vehicle functions rely on this)


(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:z_angle, color1, color2)
modelidThe modelid of the vehicle to spawn
Float:spawn_xThe x value of the spawn position
Float:spawn_yThe y value of the spawn position
Float:spawn_zThe z value of the spawn position
Float:z_angleThe angle value of the spawn position
color1The first colour of the vehicle
color1The second colour of the vehicle
AddStaticVehicle2(50,2.6,29.5,36.5,49.8,6,7);

By: Sacky

AddStaticVehicleEx2

Allows the saving of models to the AddStaticVehicleEx function, for people who prefer this function to AddStaticVehicle


(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:z_angle, color1, color2,respawn_delay)
modelidThe modelid of the vehicle to spawn
Float:spawn_xThe x value of the spawn position
Float:spawn_yThe y value of the spawn position
Float:spawn_zThe z value of the spawn position
Float:z_angleThe angle value of the spawn position
color1The first colour of the vehicle
color1The second colour of the vehicle
respawn_delayThe amount of time in miliseconds the vehicle will wait before respawning when it dies
AddStaticVehicleEx2(50,2.6,29.5,36.5,49.8,6,7,7000);

By: Sacky

AddStaticVehicleLine

Makes a line of vehicle's


(Modelid, Float:Start[2], Float:Rate[2], Float:Z, Float:Z_Angle, MaxAmount)
ModelidThe modelid of the vehicles to spawn
Float:Start[2]The x value of the starting position
Float:Rate[2]The y value of the starting position
Float:ZThe z value of the starting position
Float:Z_AngleThe angle value of the spawn position for the cars in the line
MaxAmountThe amount of cars in the line
AddStaticVehicleLine(50,2.6,3.8,7.4,270.0,5);

By: Sacky

BanTeam

Bans all the players in an entire team


(team)
teamThe teamid to ban
BanTeam(1);

By: dyraXon

ConvertSeconds

Converts an integer of seconds into a string that will display hours,minutes and seconds


(time)
timeThe amount of seconds as an integer
ReturnsThe converted amount of seconds into hours, minutes, and seconds (Displays like: x hours, x minutes and x seconds
ConvertSeconds(12000);

By: El Presy

DegreesToRadians

Converts degree's to radians


(Float:degrees)
Float:degreesThe degree you want to convert
ReturnsThe conversion of the degree to a radian
DegreesToRadians(270);

By: Sacky

encrypt

This function allows you to encrypt strings, very useful for storing passwords without fear of someone cracking them


(string[])
string[]The string you want to encrypt
ReturnsThe encrypted string
encrypt("User Password");

Note: This function requires you to set a unique key to use with it. This is so no-one elses use of the function generates the same hashes as you. This key must contain ALL the letters and digits you want to make valid in a password or it will produce wierd results. They don't have to be listed only once but must be listed at least once, in any random order. The location of the key is clearly documented within the function. Don't change your key or no-one will be able to log in.

There is also a PHP version here.

By: Y_Less

floatrand

Generates a random number with 2 decimal places


(Float:min, Float:max)
Float:minThe minimum the random float can be
Float:maxThe maximum the random float can be
ReturnsThe random float between the minimum and maximum values
floatrand(1.2,5.6);

By: Y_Less

GameTextCheck

Returns 1 if the text won't crash your server, 0 if it will


(string[])
string[]The string to be checked
ReturnsThe 1 if the text is ok, 0 if it isn't
GameTextCheck("Hello World");

By: Kamazy

GameTextForTeam

Sends GameText to all the players in the specified team, the same as GameTextForPlayer except you can specify the team


(team, text[], time, style)
teamThe team you want the message sent to
text[]The text to be sent
timeThe time for the text to remain on the screen
styleThe style of the text
GameTextForTeam(1,"Hello",1000,3);

By: Y_Less

GetClosestCar

Returns the ID of the closest car


(playerid)
playeridThe first playerid to parse into the function
ReturnsThe closest vehicle to the player
GetClosestCar(1);

By: Darkrealm

GetClosestPlayer

Returns the ID of the closest player


(playerid)
playeridThe first playerid to parse into the function
ReturnsThe closest player to the specified player
GetClosestPlayer(1);

By: Slick

GetCurrentPosition

Returns the current position as a string


(playerid)
playeridThe playerid to get the position from
ReturnsThe current position in string format
GetCurrentPosition(1);

By: Sacky

GetDistanceBetweenPlayers

Returns the distance between players as an integer


(playerid,playerid2)
playeridThe first playerid to parse into the function
playerid2The second playerid to parse into the function
ReturnsThe distance between the players in meters as an integer
GetDistanceBetweenPlayers(1,2);

By: Slick

GetDistanceToCar

Returns the distance between the player and the car as an integer (Note: This doesnt fully work properly due to the GetVehiclePos not working properly, instead it will give you the distance to the car spawn)


(playerid,carid)
playeridThe player to parse
caridThe vehicleid to parse
ReturnsThe distance between the player and the vehicle
GetDistanceToCar(1,2);

By: Darkrealm

GetPickups

Returns the number of pickups on the server
ReturnsThe number of pickups present on the server
GetPickups();

By: Sacky

GetPlayerAcceleration

Returns the player accelaration in MPH as an integer, it requires the uftimer to be implemented in the gamemode


(playerid)
playeridThe player you want to get the speed from
ReturnsThe acceleration of the player
GetPlayerAcceleration(5);

By: Sacky

GetPlayerDistanceToPoint

Returns the player distance to a 2D point


(playerid,Float:x,Float:y)
playeridThe player you wish to query
Float:xThe x value of the point
Float:yThe y value of the point
ReturnsThe distance to the point
GetPlayerDistanceToPoint(1,1.9,8.5);

By: Sacky

GetPlayerDistanceToPointEx

Returns the player distance to a 3D point


(playerid, Float:x, Float:y, Float:z)
playeridThe player you wish to query
Float:xThe x value of the point
Float:yThe y value of the point
Float:zThe z value of the point
ReturnsThe distance to the point
GetPlayerDistanceToPoint(1,1.9,8.5,9.5);

By: Sacky

GetPlayerId

Returns the player ID from a playername


(playername[])
playername[]The name you want to get the playerid from
ReturnsThe playerid (if none then returns -1)
GetPlayerId("Y_Less");

By: Y_Less

GetPlayers

Returns the number of players on the server
ReturnsThe number of players on the server
GetPlayers();

By: rapidZ

GetPlayerSkin

Returns the class of the players skin


(playerid)
playeridThe player you want to get the skin from
ReturnsThe skin id (0 if invalid)
GetPlayerSkin(5);

By: Sacky

GetPlayerSpeed

Returns the player speed in KMPH as an integer, it requires the uftimer to be implemented in the gamemode


(playerid)
playeridThe player you want to get the speed from
ReturnsThe speed of the player
GetPlayerSpeed(5);

By: Sacky

GetPointDistanceToPoint

Returns the distance between 1 x and y value and another x and y value


(Float:x,Float:y,Float:x2,Float:y2)
Float:xThe x value of the first point
Float:yThe y value of the first point
Float:x2The x value of the second point
Float:y2The y value of the second point
ReturnsThe distance between the 2 points
GetPointDistanceToPoint(12.3,14.5,109.7,88.9);

By: Sacky

GetPointDistanceToPointEx

Returns the distance between 1 x,y and z value and another x,y and z value


(Float:x,Float:y,Float:z,Float:x2,Float:y2,Float:z2)
Float:xThe x value of the first point
Float:yThe y value of the first point
Float:zThe z value of the first point
Float:x2The x value of the second point
Float:y2The y value of the second point
Float:z2The z value of the first point
ReturnsThe distance between the 2 points
GetPointDistanceToPointEx(12.3,14.5,15.6,109.7,88.9,13.4);

By: Sacky

GetVehicleClass

Returns the Vehicleclass/modelid (Needs AddStaticVehicle2 or AddStaticVehicleEx2)


(vehicleid)
vehicleidThe id of the vehicle to return the class from
ReturnsThe vehicle class (0 if invalid vehicle)
GetVehicleClass(1);

By: Sacky

GetVehicleName

Returns the name of a vehicle (Needs AddStaticVehicle2 or AddStaticVehicle2Ex)


(vehicleid)
vehicleidThe id of the vehicle to return the class from
ReturnsThe name of a vehicle (by class)
GetVehicleName(1);

By: Sacky

GetVehicleType

Returns the type of a vehicle (Needs AddStaticVehicle2 or AddStaticVehicle2Ex)


(vehicleid)
vehicleidThe id of the vehicle to return the type from
ReturnsThe type of a vehicle (eg. CAR or BOAT)
GetVehicleType(1);

By: Sacky

GetVehicleZAngle

Returns the vehicles angle


(vehicleid)
vehicleidThe id of the vehicle to return the angle from
ReturnsThe angle of the vehicle
GetVehicleZAngle(1);

By: Sacky

GetVehicles

Returns the number of vehicles on the server
ReturnsThe number of vehicles present on the server
GetVehicles();

By: Sacky

GivePlayerWeaponAlt

A version of GivePlayerWeapon, that displays an alert for weapon classes that cannot be given to players for some reason, then defaults to giving "Unarmed"


(playerid, weaponid, ammo)
playeridThe playerid to give the weapon to
weaponidThe weaponid you want to give to the player
ammoHow much ammo to give to the player
GivePlayerWeaponAlt(1,26,500);

By: Allan

GivePlayerWeaponEx

Lets you specify an unlimited number of weapons and ammo to be given to a player


( playerid, ... )
playeridThe playerid to give the weapon to
...The weaponid you want to give to the player, also specify the ammo
GivePlayerWeaponEx(1,25,400,26,400);

By: Peter

GiveTeamMoney

Gives every payer on a team a certain amount of money


(team, amount)
teamThe team to give the money to
amountThe amount of money to give to the team
GiveTeamMoney(1,500);

By: Hellomy

HexToRGBA

Converts Hexidecimal into RGBA


(colour, &r, &g, &b, &a)
colourThe hexidecimal colour
&rThe red colour to store
&gThe green colour to store
&bThe blue colour to store
&aThe alpha colour to store
HexToRGBA(0xAFAFAFAA,r,g,b,a);

By: Betamaster

icos

Inverse Trigonomic Function (cosine)


(Float:adj,Float:hyp)
Float:adjThe length of the adjacent side
Float:hypThe length of the hypotenuse side
ReturnsThe processed inverse trigonomic equation (Float)
icos(5.3,4.6);

By: Sacky

isin

Inverse Trigonomic Function (sine)


(Float:opp,Float:hyp)
Float:oppThe length of the opposite side
Float:hypThe length of the hypotenuse side
ReturnsThe processed inverse trigonomic equation (Float)
isin(5.3,4.6);

By: Sacky

IsNumeric

Returns 1 if the string is numeric, 0 if not


(string[])
string[]The string you want to check
Returns1 if numeric, 0 if not numeric
IsNumeric("12");

By: Mike

IsPickupConnected

Returns 1 if the pickup is connected and 0 if it isn't, it requires you to use AddStaticPickup2 rather than AddStaticPickup


(pickupid)
pickupidThe pickup id you want to query
Returns1 if it is connected, 0 if it isn't
IsPickupConnected(254);

By: Sacky

IsPlayerInAnyInterior

Returns the 1 if they are, returns 0 if they arent (There are some interiors which escape this function, it will work on about 95% of interiors)


(playerid)
playeridThe playerid you want to query
Returns1 if they are in an interior, 0 if they aren't
IsPlayerInAnyInterior(5);

By: Sacky

IsPlayerInArea

Returns 1 if the player is in the area and 0 if they aren't


(playerid, Float:minx, Float:maxx, Float:miny, Float:maxy)
playeridThe playerid you want to query
Float:minxThe minimum x value for the area
Float:minyThe minimum y value for the area
Float:maxxThe maximum x value for the area
Float:maxyThe maximum y value for the area
Returns1 if they are in the area, 0 if they aren't
IsPlayerInArea(2, 2.0, 2.0, 4.0, 4.0);

By: Y_Less

IsPlayerInAreaEx

Returns 1 if the player is in the area and 0 if they aren't


(playerid, ...)
playeridThe playerid you want to query
...The x and y values for your area
Returns1 if they are in the area, 0 if they aren't
IsPlayerInArea(2, 2.0, 2.0, 4.0, 4.0);

By: kool

IsPlayerInBoat

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are in a boat, 0 if they aren't
IsPlayerInBoat(5);

By: Sacky

IsPlayerInCar

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are in a car, 0 if they aren't
IsPlayerInCar(5);

By: Sacky

IsPlayerInCircle

Returns 1 if the player is in the circle and 0 if they aren't


(playerid, Float:x, Float:y, radius)
playeridThe playerid you want to query
Float:xThe x value of the center of the circle
Float:yThe y value of the center of the circle
radiusThe radius of the circle
Returns1 if they are in the area, 0 if they aren't
IsPlayerInCircle(2,13.9,14.8,5);

By: Sacky

IsPlayerInCube

Returns 1 if the player is in the cube and 0 if they aren't


(playerid, xmin, ymin, zmin, xmax, ymax, zmax)
playeridThe playerid you want to query
xminThe minimum x value of the cube
yminThe minimum y value of the cube
zminThe minimum z value of the cube
xmaxThe maximum x value of the cube
ymaxThe maximum y value of the cube
zmaxThe maximum z value of the cube
Returns1 if they are in the area, 0 if they aren't
IsPlayerInCube(2,2,3,4,1,2,3);

By: 50p

IsPlayerInHelicopter

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are in a helicopter, 0 if they aren't
IsPlayerInHelicopter(5);

By: Sacky

IsPlayerInMonsterTruck

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are in a monster truck, 0 if they aren't
IsPlayerInMonsterTruck(5);

By: Sacky

IsPlayerInPlane

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are in a plane, 0 if they aren't
IsPlayerInPlane(5);

By: Sacky

IsPlayerInSphere

Returns 1 if the player is in the sphere and 0 if they aren't


(playerid, Float:x, Float:y, Float:z, radius)
playeridThe playerid you want to query
Float:xThe x value of the center of the sphere
Float:yThe y value of the center of the sphere
Float:zThe z value of the center of the sphere
radiusThe radius of the circle
Returns1 if they are in the spherer, 0 if they aren't
IsPlayerInCircle(2,13.9,14.8,5,9.7);

By: Sacky

IsPlayerInQuad

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are in a quad, 0 if they aren't
IsPlayerInQuad(5);

By: Sacky

IsPlayerInRange

Returns 1 if if they are, 0 if they aren't


(playerid, Float: Range, Float: Z_Range, Float:tar_x, Float:tar_y, Float:tar_z)
playeridThe playerid you want to query
Float:RangeThe range the player can be in
Float:Z_RangeThe height range the player can be in
Float:tar_xThe x value for the point
Float:tar_yThe y value for the point
Float:tar_zThe z value for the point
Returns1 if they are in the range, 0 if they aren't
IsPlayerInRange(2, 2.0, 2.0, 4.0, 4.0, 4.0);

By: Simon

IsPlayerDriver

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are a driver, 0 if they aren't
IsPlayerDriver(5);

By: Sacky

IsPlayerOnBicycle

Returns the 1 if they are, returns 0 if they arent (Needs AddStaticVehicle2 or AddStaticVehicle2Ex)


(playerid)
playeridThe playerid you want to query
Returns1 if they are on a bicycle, 0 if they aren't
IsPlayerOnBicycle(5);

By: Sacky

IsPlayerOnBike

Returns the 1 if they are, returns 0 if they arent (Needs AddStaticVehicle2 or AddStaticVehicle2Ex)


(playerid)
playeridThe playerid you want to query
Returns1 if they are on a bike, 0 if they aren't
IsPlayerOnBike(5);

By: Sacky

IsPlayerOnFoot

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are on foot, 0 if they aren't
IsPlayerOnFoot(5);

By: Sacky

IsPlayerPassenger

Returns the 1 if they are, returns 0 if they arent


(playerid)
playeridThe playerid you want to query
Returns1 if they are a passenger, 0 if they aren't
IsPlayerPassenger(5);

By: Sacky

IsTeamInArea

Returns 1 if the team is in the area and 0 if they aren't


(team, Float:minx, Float:maxx, Float:miny, Float:maxy)
teamThe teamid you want to query
Float:minxThe minimum x value for the area
Float:minyThe minimum y value for the area
Float:maxxThe maximum x value for the area
Float:maxyThe maximum y value for the area
Returns1 if they are in the area, 0 if they aren't
IsTeamInArea(2, 2.0, 2.0, 4.0, 4.0);

By: dyraXon

IsVehicleConnected

Returns 1 if the vehicle is connected and 0 if it isn't


(vehicleid)
vehicleidThe vehicle's id you want to query
Returns1 if it is connected, 0 if it isn't
IsVehicleConnected(254);

By: Sacky

IsVehicleInUse

Returns 1 if the vehicle is in use and 0 if it isn't


(vehicleid)
vehicleidThe vehicle's id you want to query
Returns1 if it is in use, 0 if it isn't
IsVehicleInUse(254);

By: Sacky

itan

Inverse Trigonomic Function (tangent)


(Float:opp,Float:adj)
Float:oppThe length of the opposite side
Float:adjThe length of the adjacent side
ReturnsThe processed inverse trigonomic equation (Float)
itan(5.3,4.6);

By: Sacky

KickTeam

Kicks all the players in an entire team


(team)
teamThe teamid to kick
KickTeam(1);

By: Sacky

left

Returns the first characters in the string, it will return how many you set in the len


(source[], len)
source[]The string you want to take the left characters from
lenThe number of characters from the left you want to return
ReturnsThe number of characters from the left specified in the len
left("Hello",1);

By: Y_Less

lengthdir_x

Returns the position relative to the current position taking angle and distance as the arguments


(Float:length,Float:angle)
Float:lengthHow long the line is
Float:angleThe angle in which to draw the virtual line
ReturnsThe length of the line
lengthdir_x(32.4,170.9);

By: Slick

lengthdir_y

Returns the position relative to the current position taking angle and distance as the arguments


(Float:length,Float:angle)
Float:lengthHow long the line is
Float:angleThe angle in which to draw the virtual line
ReturnsThe length of the line
lengthdir_y(32.4,170.9);

By: Slick

minrand

Generates a random integer between the minimum and maximum numbers set


(min, max)
minThe minimum number the random integer can be
maxThe maximum number the random integer can be
ReturnsThe random number between the min and max as an integer
minrand(1,5);

By: Y_Less

mktime

Returns the unix-timestamp of a date/time, equivalent to PHP's one: mktime on php.net


(hour,minute,second,day,month,year)
hourThe hour integer to pass
minuteThe minute integer to pass
secondThe second integer to pass
dayThe day integer to pass
monthThe month integer to pass
yearThe year integer to pass
Returnsseconds since the beginning of unix time/timestamp
mktime(1,1,1,1,1,2005);

By: mabako