ExecuteCommandHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:


===Optional Arguments===
===Optional Arguments===
*'''args:''' This is a string containing any additional arguments to be passed to the attached functions.
* '''args:''' Each word after command name in the original command is passed here in seperate variables. If there is no value for an argument, the variable will contain [[nil]].
 


<syntaxhighlight lang="lua">player playerSource, string commandName, [string args ...] </syntaxhighlight>
<syntaxhighlight lang="lua">player playerSource, string commandName, [string args ...] </syntaxhighlight>
Line 20: Line 21:
* '''commandName:''' The name of the command triggered. This is useful if multiple commands go through one function.
* '''commandName:''' The name of the command triggered. This is useful if multiple commands go through one function.
* '''args:''' Each word after command name in the original command is passed here in seperate variables. If there is no value for an argument, the variable will contain [[nil]].
* '''args:''' Each word after command name in the original command is passed here in seperate variables. If there is no value for an argument, the variable will contain [[nil]].


===Returns===
===Returns===

Revision as of 12:12, 23 June 2006

This function will call all the attached functions of an existing console command, for a specified player. 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.

Syntax

bool executeCommandHandler ( string commandName, player thePlayer, [ string args ] )

Required Arguments

  • commandName: This is the name of the command you wish to execute. This is what must be typed into the console to trigger the function.
  • thePlayer: This is the player that will be passed to the attached functions of the console command.

Optional Arguments

  • args: Each word after command name in the original command is passed here in seperate variables. If there is no value for an argument, the variable will contain nil.


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: Each word after command name in the original command is passed here in seperate variables. If there is no value for an argument, the variable will contain nil.


Returns

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

Example

Example 1: This example defines a command handler for the command createmarker (This will create a red marker at the player who uses it's position), then calls it.

-- 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 )
	-- If a player triggered it (rather than the admin) then
	if ( playerSource )
		-- Get that player's position
		x, y, z = getEntityPosition ( 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

-- Register another command
addCommandHandler ( "createmarker2", "consoleCreateMarker2" )
function consoleCreateMarker2 ( playerSource, commandName )
  -- re-route back to the original
  executeCommandHandler ( "createmarker", playerSource, args )
end

See Also