AddCommandHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
__NOTOC__
This function will create a console command and attach it to a function, so that whenever a player or administrator uses the command the function is called.
Multiple command handlers can be attached to a single function, and they will be called in the order that the handlers were attached.
==Syntax==
<syntaxhighlight lang="lua">
bool addCommandHandler ( string commandName, string handlerFunction )
</syntaxhighlight>
===Required Arguments===
*'''commandName:''' This is the name of the command you wish to add. This is what must be typed into the console to trigger the function.
*'''handlerFunction:''' This is the name of the function that you want the command to trigger. This function should take 3 parameters:
<syntaxhighlight lang="lua">player playerSource, string commandName, string args</syntaxhighlight>
* '''playerSource:''' The player who triggered the command. If not triggered by a player (e.g. by admin), this will be ''false''.
* '''commandName:''' The name of the command triggered. This is useful if multiple commands go through one function.
* '''args:''' All the characters after the commandName.
===Returns===
Returns ''true'' if the command handler was added successfully, ''false'' otherwise.
==Example==
This example defines a command handler for the command ''createmarker''. This will create a red marker at the player who uses it's position.
<syntaxhighlight lang="lua">
-- Register the command handler and attach it to the 'consoleCreateMarker' function
addCommandHandler ( "createmarker", "consoleCreateMarker" )
-- Define our function that will handle this command
function consoleCreateMarker ( playerSource, commandName, args )
-- If a player triggered it (rather than the admin) then
if ( playerSource )
-- Get that player's position
x, y, z = getPlayerPosition ( playerSource )
-- Create a marker at their position
createMarker ( x, y, z, 0, "checkpoint", 255, 0, 0, 255 )
-- Output it in the chat box
outputChatBox ( "You got a red marker", playerSource )
end
end
</syntaxhighlight>
==See Also==
{{Server_Functions}}
Handler function format is "player, key, args"
Handler function format is "player, key, args"

Revision as of 13:20, 20 May 2006

This function will create a console command and attach it to a function, so that whenever a player or administrator uses the command the function is called.

Multiple command handlers can be attached to a single function, and they will be called in the order that the handlers were attached.

Syntax

bool addCommandHandler ( string commandName, string handlerFunction )

Required Arguments

  • commandName: This is the name of the command you wish to add. This is what must be typed into the console to trigger the function.
  • handlerFunction: This is the name of the function that you want the command to trigger. This function should take 3 parameters:
player playerSource, string commandName, string args
  • playerSource: The player who triggered the command. If not triggered by a player (e.g. by admin), this will be false.
  • commandName: The name of the command triggered. This is useful if multiple commands go through one function.
  • args: All the characters after the commandName.

Returns

Returns true if the command handler was added successfully, false otherwise.

Example

This example defines a command handler for the command createmarker. This will create a red marker at the player who uses it's position.

-- Register the command handler and attach it to the 'consoleCreateMarker' function
addCommandHandler ( "createmarker", "consoleCreateMarker" )

-- Define our function that will handle this command
function consoleCreateMarker ( playerSource, commandName, args )
	-- If a player triggered it (rather than the admin) then
	if ( playerSource )
		-- Get that player's position
		x, y, z = getPlayerPosition ( playerSource )
		-- Create a marker at their position
		createMarker ( x, y, z, 0, "checkpoint", 255, 0, 0, 255 )
		-- Output it in the chat box
		outputChatBox ( "You got a red marker", playerSource )
	end
end

See Also

Template:Server Functions


Handler function format is "player, key, args"