AddCommandHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(→‎Example: Collapsed example sections, improved clientside example)
Line 34: Line 34:


==Example==  
==Example==  
<section name="Server" class="server" show="true">
<section name="Server" class="server">
'''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.
'''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.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 106: Line 106:
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
<section name="Client" class="client" show="true">
<section name="Client" class="client">
'''Example 1:''' This creates a GUI window and allows a player to change it's alpha (the visibleness/transparency) value with a command. Note that player is not an argument for clientside.
'''Example 1:''' This creates a GUI window and allows a player to change its alpha (transparency) value with a command. Note that, in a clientside script, the player using the command is not passed as a parameter to the handler function, since it is always the local player.


<syntaxhighlight lang="lua">--Create a gridlist
<syntaxhighlight lang="lua">--Create a window
myWindow = guiCreateWindow ( 0.30, 0.10, 0.5, 0.60, "GUI window title", true )
myWindow = guiCreateWindow ( 0.30, 0.10, 0.5, 0.60, "GUI window title", true )


--Add a command handler to change the alpha of the GUI window.
--Add a command handler to change the alpha of the GUI window. Usage example: 'alpha 0.15'
--Usage example: 'alpha 155', where 155 is stored as alphaAmount
function changeAlpha ( commandName, alphaAmount )
function changeAlpha ( commandName, alphaAmount )
alphaAmount = tonumber(alphaAmount)
-- All command parameters are strings, so we'll convert the value to a number first
guiSetAlpha ( myGridList, alphaAmount )
alphaAmount = tonumber(alphaAmount)
guiSetAlpha ( myGridList, alphaAmount )
end
end
addCommandHandler ( "alpha", changeAlpha )
addCommandHandler ( "alpha", changeAlpha )

Revision as of 12:04, 11 October 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

Click to collapse [-]
Server
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.
Click to expand [+]
Client

Returns

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

Example

Click to expand [+]
Server
Click to expand [+]
Client

See Also