AddCommandHandler: Difference between revisions
mNo edit summary |
m (Minor corrections) |
||
Line 12: | Line 12: | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''commandName:''' This is the name of the command you wish to attach a handler to. This is what must be typed into the console to trigger the function. | *'''commandName:''' This is the name of the command you wish to attach a handler to. This is what must be typed into the console to trigger the function. | ||
*'''handlerFunction:''' This is the function that you want the command to trigger, which has to be defined before you add the handler. This function can take two parameters, playerSource and commandName, followed by as many parameters you expect after your command. These are all optional. | *'''handlerFunction:''' This is the function that you want the command to trigger, which has to be defined before you add the handler. This function can take two parameters, playerSource and commandName, followed by as many parameters you expect after your command (see below). These are all optional. | ||
====Handler function parameters==== | |||
<syntaxhighlight lang="lua">player playerSource, string commandName, [string arg1, string arg2, ...] </syntaxhighlight> | <syntaxhighlight lang="lua">player playerSource, string commandName, [string arg1, string arg2, ...] </syntaxhighlight> | ||
Line 27: | Line 28: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- Define our function that will handle this command | -- Define our function that will handle this command | ||
function consoleCreateMarker ( playerSource | function consoleCreateMarker ( playerSource ) | ||
-- If a player triggered it (rather than the admin) then | -- If a player triggered it (rather than the admin) then | ||
if ( playerSource ) then | if ( playerSource ) then | ||
Line 34: | Line 35: | ||
-- Create a size 2, red checkpoint marker at their position | -- Create a size 2, red checkpoint marker at their position | ||
createMarker ( x, y, z, "checkpoint", 2, 255, 0, 0, 255 ) | createMarker ( x, y, z, "checkpoint", 2, 255, 0, 0, 255 ) | ||
-- Output it in | -- Output it in his chat box | ||
outputChatBox ( "You got a red marker", playerSource ) | outputChatBox ( "You got a red marker", playerSource ) | ||
end | end | ||
end | end | ||
-- | -- Attach the 'consoleCreateMarker' function to the "createmarker" command | ||
addCommandHandler ( "createmarker", consoleCreateMarker ) | addCommandHandler ( "createmarker", consoleCreateMarker ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 69: | Line 70: | ||
end | end | ||
end | end | ||
-- | -- Attach the 'consoleSetVehicleColor' function to the "set_vehicle_color" command | ||
addCommandHandler ( "set_vehicle_color", consoleSetVehicleColor ) | addCommandHandler ( "set_vehicle_color", consoleSetVehicleColor ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Example 3:''' This example makes use of Lua's vararg expression to | '''Example 3:''' This example makes use of Lua's vararg expression to implement a ''check_parameters'' command to count the number of parameters passed, merge them all into a single string and output it. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- Define our function that will handle this command (which can accept a variable number of arguments after commandName) | -- Define our function that will handle this command (which can accept a variable number of arguments after commandName) | ||
Line 88: | Line 89: | ||
local stringWithAllParameters = table.concat( parametersTable, ", " ) | local stringWithAllParameters = table.concat( parametersTable, ", " ) | ||
-- Output this parameter list to the player's chatbox | -- Output this parameter list to the player's chatbox | ||
outputChatBox ( "Parameters passed: " .. | outputChatBox ( "Parameters passed: " .. stringWithAllParameters, playerSource ) | ||
end | end | ||
end | end | ||
-- | -- Attach the 'consoleCheckParameters' function to the "check_parameters" command | ||
addCommandHandler ( "check_parameters", consoleCheckParamters ) | addCommandHandler ( "check_parameters", consoleCheckParamters ) | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 15:45, 30 July 2007
This template is no longer in use as it results in poor readability. This function will attach a scripting function (handler) to a console command, so that whenever a player or administrator uses the command the function is called.
Multiple command handlers can be attached to a single command, and they will be called in the order that the handlers were attached. Equally, multiple commands can be handled by a single function, and the commandName parameter used to decide the course of action.
Syntax
bool addCommandHandler ( string commandName, function handlerFunction )
Required Arguments
- commandName: This is the name of the command you wish to attach a handler to. This is what must be typed into the console to trigger the function.
- handlerFunction: This is the function that you want the command to trigger, which has to be defined before you add the handler. This function can take two parameters, playerSource and commandName, followed by as many parameters you expect after your command (see below). These are all optional.
Handler function parameters
player playerSource, string commandName, [string arg1, string arg2, ...]
- 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.
- arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Example 3 below.
Returns
Returns true if the command handler was added successfully, false otherwise.
Example
Example 1: This example defines a command handler for the command createmarker. This will create a red marker at the position of the player player who uses it.
-- Define our function that will handle this command function consoleCreateMarker ( playerSource ) -- If a player triggered it (rather than the admin) then if ( playerSource ) then -- Get that player's position local x, y, z = getElementPosition ( playerSource ) -- Create a size 2, red checkpoint marker at their position createMarker ( x, y, z, "checkpoint", 2, 255, 0, 0, 255 ) -- Output it in his chat box outputChatBox ( "You got a red marker", playerSource ) end end -- Attach the 'consoleCreateMarker' function to the "createmarker" command addCommandHandler ( "createmarker", consoleCreateMarker )
Example 2: This example implements a set_vehicle_color command.
-- Define our function that will handle this command function consoleSetVehicleColor ( playerSource, commandName, col1, col2, col3, col4 ) -- If a player triggered this in-game if ( playerSource ) then -- Get the player's vehicle local playerVehicle = getPlayerOccupiedVehicle ( playerSource ) -- If the player is in a vehicle and we've got at least 1 parameter if ( playerVehicle and col1 ) then -- Get the vehicle's existing color and use it if fewer than 4 arguments were passed local exCol1, exCol2, exCol3, exCol4 = getVehicleColor ( playerVehicle ) if not col2 then col2 = exCol2 end if not col3 then col3 = exCol3 end if not col4 then col4 = exCol4 end -- Set the vehicle's color setVehicleColor ( playerVehicle, col1, col2, col3, col4 ) else -- If we didn't get at least 1 parameter or the player doesn't have a vehicle, display some help text outputConsole ( "This function will set your current vehicle's colors. A vehicle can have up to 4 colors.", playerSource ) outputConsole ( "Syntax: set_vehicle_color color1 [ color2 color3 color4 ]", playerSource ) outputConsole ( "You must be in a vehicle to use this function.", playerSource ) end end end -- Attach the 'consoleSetVehicleColor' function to the "set_vehicle_color" command addCommandHandler ( "set_vehicle_color", consoleSetVehicleColor )
Example 3: This example makes use of Lua's vararg expression to implement a check_parameters command to count the number of parameters passed, merge them all into a single string and output it.
-- Define our function that will handle this command (which can accept a variable number of arguments after commandName) function consoleCheckParameters ( playerSource, commandName, ... ) -- If a player, not an admin, triggered it, if playerSource then -- Pack all extra arguments passed in a table local parametersTable = {...} -- Get the number of arguments in that table local parameterCount = #parametersTable -- Output it to the player's chatbox outputChatBox ( "Number of parameters: " .. parameterCount, playerSource ) -- Join them together in a single comma-separated string local stringWithAllParameters = table.concat( parametersTable, ", " ) -- Output this parameter list to the player's chatbox outputChatBox ( "Parameters passed: " .. stringWithAllParameters, playerSource ) end end -- Attach the 'consoleCheckParameters' function to the "check_parameters" command addCommandHandler ( "check_parameters", consoleCheckParamters )
See Also
- getMaxPlayers
- getServerConfigSetting
- getServerHttpPort
- getServerName
- getServerPassword
- getServerPort
- isGlitchEnabled
- setGlitchEnabled
- setMaxPlayers
- setServerConfigSetting
- setServerPassword
- shutdown