Difference between revisions of "AddCommandHandler"

From Multi Theft Auto: Wiki
Jump to: navigation, search
(Syntax)
(Syntax)
Line 15: Line 15:
 
<section name="Server" class="server" show="true">
 
<section name="Server" class="server" show="true">
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
bool addCommandHandler ( string commandName, function handlerFunction, bool restricted = false )
+
bool addCommandHandler ( string commandName, function handlerFunction, [bool restricted = false] )
 
</syntaxhighlight>  
 
</syntaxhighlight>  
  
Line 21: Line 21:
 
*'''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 (see below). 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.
 +
===Optional Arguments===
 +
{{OptionalArg}}
 
*'''restricted:''' Specify whether or not this command should be restricted by default. Use this on commands that should be inaccessible to everyone as default except special users specified in the ACL (Access Control List). This is to make sure admin commands such as ie. 'punish' won't be available to everyone if a server administrator forgets masking it in ACL. This argument defaults to false if nothing is specified.
 
*'''restricted:''' Specify whether or not this command should be restricted by default. Use this on commands that should be inaccessible to everyone as default except special users specified in the ACL (Access Control List). This is to make sure admin commands such as ie. 'punish' won't be available to everyone if a server administrator forgets masking it in ACL. This argument defaults to false if nothing is specified.
 
</section>
 
</section>

Revision as of 17:28, 21 November 2007

Note: It is strongly advised that you do not use the same name for your handler function as the command name, as this can lead to confusion if multiple handler functions are used. Use a name that describes your handler's purpose more specifically.

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.

For users, a command is in the format:

commandName argument1 argument2

This can be triggered from the player's console or directly from the chat box by prefixing the message with a forward slash (/). For server side handlers, the server admin is also able to trigger these directly from the server's console in the same way as they are triggered from a player's console.

Syntax

Click to collapse [-]
Server
bool addCommandHandler ( string commandName, function handlerFunction, [bool restricted = false] )

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.

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.

  • restricted: Specify whether or not this command should be restricted by default. Use this on commands that should be inaccessible to everyone as default except special users specified in the ACL (Access Control List). This is to make sure admin commands such as ie. 'punish' won't be available to everyone if a server administrator forgets masking it in ACL. This argument defaults to false if nothing is specified.
Click to collapse [-]
Client
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


Template:Incomplete