Skip to content

Latest commit

 

History

History
663 lines (562 loc) · 13.5 KB

shared function.md

File metadata and controls

663 lines (562 loc) · 13.5 KB

Script Functions

Command Functions

Event Functions

Blip Functions

Util Functions

Area Functions

Vehicle Functions

Player Functions


log

This outputs a line of text to the server's log. This could be useful for debugging.

Syntax

log(string text)

Arguments

  • string text: The text to be output to the log.

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
addEventHandler( "onScriptInit",
function()
{
	log("server started");
});

date

This function is used to return the current date.

Syntax

date()

Return Values

  • table - day, month, year and yearday

Example

//SERVER
function init()
{
	local currentDate = date();
	log( currentDate["day"] + "," + currentDate["month"] + "," + currentDate["year"] + "," +currentDate["yearday"] );
}
addEventHandler( "onScriptInit", init );

getMaxPlayers

This function is used to return the maximum amount of slots in the server.

Syntax

getMaxPlayers()

Return Values

  • int: The maximum player slots

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local value = getMaxPlayers()
	sendPlayerMessage( playerid, "getMaxPlayers: " + value + "." );
});

getPlayerCount

This function is used to return the current amount of players in the server.

Syntax

getPlayerCount()

Return Values

  • int: Number of players

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local value = getPlayerCount()
	sendPlayerMessage( playerid, "getPlayerCount: " + value + "." );
});

getTickCount

This function returns amount of time that your system has been running in milliseconds. By comparing two values of getTickCount, you can determine how much time has passed (in milliseconds) between two events. This could be used to determine how efficient your code is, or to time how long a player takes to complete a task.

Syntax

getTickCount()

Return Values

  • int: Returns an integer containing the number of milliseconds since the system the server is running on started.

Example

//SERVER
addEventHandler( "onScriptInit",
function()
{
	log("Tick Count: " + getTickCount());
});

getServerName

This function is used to return the name of the server.

Syntax

getServerName()

Return Values

  • string: A string containing the server's name.

Example

//SERVER
addEventHandler( "onScriptInit",
function()
{
	log(getServerName());
});

getPlayers

This function is used to retrieve table (playerid : playerName) of all current connected players.

Syntax

getPlayers()

Return Values

  • table: Table with all currently connected players. For each table entry 'key' is an playerId and 'value' is an player name.

Example

//SERVER
addEventHandler("onPlayerDisconnected", function(playerid, reason) {
	local players = getPlayers();
	local list = "";
	foreach (idx, name in players) {
		list += ", " + name;
	}
	sendPlayerMessageToAll("Still active players: " + list);
});

getVehicles

This function is used to retrieve array of all created vehicle ids.

Syntax

getVehicles()

Return Values

  • array: Array of all created vehicle ids.

Example

//SERVER
addEventHandler("onScriptExit", function() {
	local vehicles = getVehicles();
	// call destoryVehicle for every id in array
	vehicles.apply(destroyVehicle);
});

addCommandHandler

This function is used to add a command handler.

Syntax

addCommandHandler(string command, function)

Arguments

  • string command: The command which would be typed into the chat
  • function: The function to call when the command is typed

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	setPlayerHealth( playerid, 720.0 );
});

addEventHandler

This function is used to create a function handler for an event.

Syntax

addEventHandler(string event, function)

Arguments

  • string event: The name of the event
  • function: The function which will be called when the event is triggered

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
function init()
{
	setGameModeText( "My GameMode Name" );
	setMapName( "My Map Name" );
}
addEventHandler( "onScriptInit", init );

callEvent

This function is used to call an event created by addEventHandler

Syntax

callEvent(string event, ...)

Arguments

  • string event: The event name
  • ... : (Optional) Arguments to pass onto the event

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
function customEventFnc( total )
{
	// This would output "Total Is: 53"
	log( "Total is: " + total );
}
addEventHandler ( "customEvent", customEventFnc );

function init()
{
	// Call customEvent and pass 53 as an argument
	callEvent("customEvent", 53);
}
addEventHandler( "onScriptInit", init );

removeEventHandler

This function is used to remove a function event handler

Syntax

removeEventHandler(string event, function)

Arguments

  • string event: The name of the event to be removed
  • function: The function linked to the event

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
function customEventFnc( total )
{
	// This would output "Total Is: 53"
	log( "Total is: " + total );
}
addEventHandler ( "customEvent", customEventFnc );

function init()
{
	removeEventHandler("customEvent", customEventFnc);
}
addEventHandler( "onScriptInit", init );

createBlip

This function is used to create a blip on the mini-map.

Syntax

createBlip(float x, float y, int library, int icon)

Arguments

  • float x: The X position of the blip
  • float y: The Y position of the blip
  • int library: The ID of the icon's library
  • int icon: The ID of the icon

Return Values

  • int - The created blipid

Example

//SERVER
function init()
{
	createBlip( -300.0, 120.0, 0, 1 );
}
addEventHandler( "onScriptInit", init );

destroyBlip

This function is used to destroy a blip.

Syntax

destroyBlip(int blipid)

Arguments

  • int blipid: The ID of the blip you want to destroy

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
function init()
{
	local blipid = createBlip( -300.0, 120.0, 0, 1 );
	destroyBlip(blipid)
}
addEventHandler( "onScriptInit", init );

attachBlipToPed

This function is used to attach a blip to a ped.

Syntax

attachBlipToPed(int blipid, int pedid)

Arguments

  • int blipid: The ID of the blip you want to establish
  • int pedid: The ID of the ped

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//CLIENT
addCommandHandler ( "run", 
function ( playerid )
{
	local pedid = createPed( 0, 100.0, 100.0, -20.0, 0.0, 0.0, 0.0 );
	local blipid = createBlip( -300.0, 120.0, 0, 1 );
	attachBlipToPed( blipid, pedid );
	return 1;
});

attachBlipToPlayer

This function is used to attach a blip to a player.

Syntax

attachBlipToPlayer(int blipid, int playerid)

Arguments

  • int blipid: The ID of the blip you want to establish
  • int playerid: The ID of the player

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
addCommandHandler ( "run", 
function ( playerid )
{
	local blipid = createBlip( -300.0, 120.0, 0, 1 );
	attachBlipToPlayer( blipid, playerid );
	return 1;
});

attachBlipToVehicle

This function is used to attach a blip to a vehicle.

Syntax

attachBlipToVehicle(int blipid, int vehicleid)

Arguments

  • int blipid: The ID of the blip you want to establish
  • int vehicleid: The ID of the vehicle

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
addCommandHandler ( "run", 
function ( playerid )
{
	local vehicleid = createVehicle( 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 );
	local blipid = createBlip( -300.0, 120.0, 0, 1 );
	attachBlipToVehicle( blipid, vehicleid );
	return 1;
});

clamp

This function is used to clamp a value between a minimum float and maximum float value.

Syntax

clamp(float minimum, float value, float maximum)

Arguments

  • float minimum: The minimum float
  • float value: The value
  • float maximum: The maximum float

Return Values

  • float - The clamped value

Example

//SERVER
function init()
{
	// This would output: Clamp: 3
	log("Clamp: " + clamp(1.0, 10.0, 3.0) );
}
addEventHandler( "onScriptInit", init );

fromRGB

This function is used to convert a RGB(A) colour.

Syntax

fromRGB(int r, int g, int b[, a = 255])

Arguments

  • int r: The red (RGB) colour
  • int g: The green (RGB) colour
  • int b: The blue (RGB) colour
  • int a: (Optional) The alpha, default 255

Return Values

  • array - R, G, B and A colours

Example

//CLIENT
addEventHandler( "onClientFrameRender",
function( post )
{
	dxDrawText("hello world", 0.0, 0.0, fromRGB(255, 255, 255, 255), true, "tahoma-bold", 1.0);
});

getDistanceBetweenPoints2D

This function is used to return the distance between two 2D points.

Syntax

getDistanceBetweenPoints2D(float PointX, float PointY, float pointX2, float pointY2)

Arguments

  • float PointX: The X coordinate of the first point
  • float PointY: The Y coordinate of the first point
  • float pointX2: The X coordinate of the second point
  • float pointY2: The Y coordinate of the second point

Return Values

  • float - distance

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local myPos = getPlayerPosition(playerid)
	local dis = getDistanceBetweenPoints2D( 400.0, 200.0, myPos[0], myPos[1] );
	sendPlayerMessage( playerid, "Distance between points: " + dis + "." );
});

getDistanceBetweenPoints3D

This function is used to return the distance between two 3D points.

Syntax

getDistanceBetweenPoints3D(float PointX, float PointY, float PointZ, float pointX2, float pointY2, float pointZ2)

Arguments

  • float PointX: The X coordinate of the first point
  • float PointY: The Y coordinate of the first point
  • float PointZ: The Z coordinate of the first point
  • float pointX2: The X coordinate of the second point
  • float pointY2: The Y coordinate of the second point
  • float pointZ2: The Z coordinate of the second point

Return Values

  • float - distance

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local myPos = getPlayerPosition(playerid)
	local dis = getDistanceBetweenPoints3D( 400.0, 200.0, -14.0, myPos[0], myPos[1], myPos[2] );
	sendPlayerMessage( playerid, "Distance between points: " + dis + "." );
});

explodeVehicle

This function is used to explode vehicle.

Syntax

explodeVehicle(int vehicleid)

Arguments

  • int vehicleid: The ID of the vehicle

Return Values

Returns 'true' if successful, 'false' otherwise.

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local myPos = getPlayerPosition(playerid)
	local vehicleid = createVehicle( 0, myPos[0], myPos[1], myPos[2], 0.0, 0.0, 0.0 );
	explodeVehicle( vehicleid );
});

getIndicatorLightState

This function can be used to get vehicle indicator lights state.

Syntax

getIndicatorLightState(int vehicleid, int indicatorSide)

Arguments

  • int vehicleid: The ID of the vehicle
  • int indicatorSide: side of the indicator, possible values: INDICATOR_LEFT, INDICATOR_RIGHT

Return Values

Boolean result of getting vehicle indicator lights state (true/false)

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local myPos = getPlayerPosition(playerid)
	local vehicleid = createVehicle( 0, myPos[0], myPos[1], myPos[2], 0.0, 0.0, 0.0 );
	local prevState = getIndicatorLightState(vehicleid, INDICATOR_LEFT);
	sendPlayerMessage( playerid, "getIndicatorLightState: " + prevState + "." );
});

getPlayerHealth

This function is used to return the players health.

Syntax

getPlayerHealth(int playerid)

Arguments

  • int playerid: The ID of the player

Return Values

  • float: The amount of health

Example

//SERVER
addCommandHandler( "run",
function( playerid )
{
	local value = getPlayerHealth(playerid)
	sendPlayerMessage( playerid, "getPlayerHealth: " + value + "." );
});