ExecuteCommandHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(13 intermediate revisions by 11 users not shown)
Line 1: Line 1:
__NOTOC__  
{{Server client function}}
__NOTOC__
This function will call all the attached functions of an existing console command, for a specified player.
This function will call all the attached functions of an existing console command, for a specified player.
{{Note|You can only execute commands created with addCommandHandler. You cannot execute MTA harcoded commands due to security reasons.}}
{{Note|Serverside commands can only be executed by the server. The same applies to the client side}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<section name="Server" class="server" show="true">
bool executeCommandHandler ( string commandName, player thePlayer, [ string args ] )
<syntaxhighlight lang="lua">bool executeCommandHandler ( string commandName, player thePlayer, [ string args ] )</syntaxhighlight>
</syntaxhighlight>  
 
==Required Arguments==
*'''commandName:''' The name of the command you wish to execute. This is what must be typed into the console to trigger the function.
*'''thePlayer:''' The player that will be presented as executer of the command to the handler function(s) of the command.


===Required Arguments===  
==Optional 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.
{{OptionalArg}}
*'''thePlayer:''' This is the player that will be passed to the attached functions of the console command.
* '''args:''' Additional parameters that will be passed to the handler function(s) of the command that is called, separated by spaces.
</section>
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">bool executeCommandHandler ( string commandName, [ string args ] )</syntaxhighlight>


===Optional Arguments===
==Required 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]].
*'''commandName:''' The name of the command you wish to execute. This is what must be typed into the console to trigger the function.


==Optional Arguments==
{{OptionalArg}}
* '''args:''' Additional parameters that will be passed to the handler function(s) of the command that is called, separated by spaces.
</section>
===Returns===
===Returns===
Returns ''true'' if the command handler was called successfully, ''false'' otherwise.
Returns ''true'' if the command handler was called successfully, ''false'' otherwise.


==Example==  
==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). It then creates a second command handler ''createmarker2'' which will call the first one.
<section name="Server" class="server" show="true">
This example defines a command handler for the command ''createmarker'' (which creates a red marker at the caller's position). It then creates a second command handler ''createmarker2'' which will call the first one.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Register the command handler and attach it to the 'consoleCreateMarker' function
-- Define the function that will handle the 'createmarker' command
addCommandHandler ( "createmarker", "consoleCreateMarker" )
-- Define our function that will handle this command
function consoleCreateMarker ( playerSource, commandName )
function consoleCreateMarker ( playerSource, commandName )
-- If a player triggered it (rather than the admin) then
-- If a player triggered it (rather than the admin) then
if ( playerSource )
if ( playerSource ) then
-- Get that player's position
-- Get that player's position
x, y, z = getEntityPosition ( playerSource )
x, y, z = getElementPosition ( playerSource )
-- Create a marker at their position
-- Create a marker at their position
createMarker ( x, y, z, 0, "checkpoint", 255, 0, 0, 255 )
createMarker ( x, y, z, 0, "checkpoint", 255, 0, 0, 255 )
Line 34: Line 47:
end
end
end
end
-- Add the function as a handler for the command
addCommandHandler ( "createmarker", consoleCreateMarker )


-- Register another command
-- Define a second console command that will just call the first.
addCommandHandler ( "createmarker2", "consoleCreateMarker2" )
-- First define the function
function consoleCreateMarker2 ( playerSource, commandName )
function consoleCreateMarker2 ( playerSource, commandName )
  -- re-route back to the original
-- re-route back to the original
  executeCommandHandler ( "createmarker", playerSource )
executeCommandHandler ( "createmarker", playerSource )
end
end
-- Then add it as a handler for the new console command
addCommandHandler ( "createmarker2", consoleCreateMarker2 )
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Server functions}}
{{Server functions}}

Latest revision as of 14:49, 24 May 2019

This function will call all the attached functions of an existing console command, for a specified player.

[[{{{image}}}|link=|]] Note: You can only execute commands created with addCommandHandler. You cannot execute MTA harcoded commands due to security reasons.
[[{{{image}}}|link=|]] Note: Serverside commands can only be executed by the server. The same applies to the client side


Syntax

Click to collapse [-]
Server
bool executeCommandHandler ( string commandName, player thePlayer, [ string args ] )

Required Arguments

  • commandName: The name of the command you wish to execute. This is what must be typed into the console to trigger the function.
  • thePlayer: The player that will be presented as executer of the command to the handler function(s) of the command.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • args: Additional parameters that will be passed to the handler function(s) of the command that is called, separated by spaces.
Click to collapse [-]
Client
bool executeCommandHandler ( string commandName, [ string args ] )

Required Arguments

  • commandName: The name of the command you wish to execute. This is what must be typed into the console to trigger the function.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • args: Additional parameters that will be passed to the handler function(s) of the command that is called, separated by spaces.

Returns

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

Example

Click to collapse [-]
Server

This example defines a command handler for the command createmarker (which creates a red marker at the caller's position). It then creates a second command handler createmarker2 which will call the first one.

-- Define the function that will handle the 'createmarker' command
function consoleCreateMarker ( playerSource, commandName )
	-- If a player triggered it (rather than the admin) then
	if ( playerSource ) then
		-- Get that player's position
		x, y, z = getElementPosition ( 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
-- Add the function as a handler for the command
addCommandHandler ( "createmarker", consoleCreateMarker )

-- Define a second console command that will just call the first.
-- First define the function
function consoleCreateMarker2 ( playerSource, commandName )
	-- re-route back to the original
	executeCommandHandler ( "createmarker", playerSource )
end
-- Then add it as a handler for the new console command
addCommandHandler ( "createmarker2", consoleCreateMarker2 )

See Also