concommand/Add

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (typo fix)
 
(14 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=Add
 
|Parent=concommand
 
 
|Description=Creates a console command that runs a function in lua with optional autocompletion function and help text.
 
|Description=Creates a console command that runs a function in lua with optional autocompletion function and help text.
|Realm=Shared
+
 
 +
{{Bug|Issue=1183|This will fail if the concommand was previously removed with {{LibraryFunction|concommand|Remove}} in a different realm (creating a command on the client that was removed from the server and vice-versa).}}
 +
|Realm=Shared and Menu
 
|IsClass=No
 
|IsClass=No
 +
|File=lua/includes/modules/concommand.lua
 +
|Line=28
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=string
 
|type=string
 
|name=name
 
|name=name
|desc=The command name to be used in console.
+
|desc=The command name to be used in console.<br><br>
  
It may not contain any of the following (even as a substring) or the command will not be registered:<br/>
+
This cannot be a name of existing console command or console variable. It will silently fail if it is.
toggleconsole, exit, gameui_activate, connect, disconnect
+
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=function
 
|type=function
|name=commandFunc
+
|name=callback
|desc=The function to run when the concommand is executed.<br>
+
|desc=The function to run when the concommand is executed. Arguments passed are:
Args: ( {{FuncArg|Player|ply}}, {{FuncArg|string|cmd}}, {{FuncArg|table|args}}, {{FuncArg|string|fullstring}} )
+
* {{FuncArg|Player|ply|The player that ran the concommand. NULL entity if command was entered with the dedicated server console.}}
 +
* {{FuncArg|string|cmd|The concommand string (if one callback is used for several concommands).}}
 +
* {{FuncArg|table|args|A table of all string arguments.}}
 +
* {{FuncArg|string|argStr|The arguments as a string.}}
 
}}
 
}}
 
{{Arg
 
{{Arg
 
|type=function
 
|type=function
|name=autoCompleteFunc
+
|name=autoComplete
|desc=The function to call which should return a table of options for autocompletion.<br>
+
|desc=The function to call which should return a table of options for autocompletion. ([[Autocomplete Tutorial|Autocompletion Tutorial]])
This only properly works on the client since it is '''not''' networked.<br>
+
 
[[Autocomplete Tutorial|Autocompletion Tutorial]]
+
This only properly works on the client since it is '''not''' networked. Arguments passed are:
Args: ( {{FuncArg|string|cmd}}, {{FuncArg|string|args}} )
+
* {{FuncArg|string|cmd|The concommand this autocompletion is for.}}
 +
* {{FuncArg|string|args|The arguments typed so far.}}
 
|default=nil
 
|default=nil
 
}}
 
}}
Line 38: Line 43:
 
|type=number
 
|type=number
 
|name=flags
 
|name=flags
|desc=Concommand flags from {{Enum|FCVAR}}.
+
|desc=Concommand modifier flags. See {{Enum|FCVAR}}.
 
|default=0
 
|default=0
 
}}
 
}}
 
{{Example
 
{{Example
|Description=Adds a concommand 'killyourself' which will kill the user.
+
|Description=Adds a concommand ''killyourself'' which will kill the user.
|Code=concommand.Add("killyourself",function( ply )
+
|Code=concommand.Add("killyourself",function( ply, cmd, args )
 
     ply:Kill()
 
     ply:Kill()
 
     print("You killed yourself!")
 
     print("You killed yourself!")
 
end)
 
end)
 
 
 
}}
 
}}
 
{{Example
 
{{Example
|Description=When every a player runs "retrieveplayers" in the console it'll print anyone who's on the server SteamID & Nick. ~ Good@Facepunch.net
+
|Description=A concommand that prints the SteamID and nickname of every player on the server.
 
|Code=concommand.Add( "retrieveplayers", function()   
 
|Code=concommand.Add( "retrieveplayers", function()   
        for _, v in pairs( player.GetAll() ) do
+
for _, ply in ipairs( player.GetAll() ) do
                print(v:Nick()..", "..v:SteamID().."\n" )
+
print( ply:Nick() .. ", " .. ply:SteamID() .. "\n" )
  end
+
end
 
end)
 
end)
 
 
}}
 
}}

Latest revision as of 04:55, 22 February 2019

 concommand.Add( )

Contents

Description

Creates a console command that runs a function in lua with optional autocompletion function and help text.

BUG

This will fail if the concommand was previously removed with concommand.Remove in a different realm (creating a command on the client that was removed from the server and vice-versa).

Issue Tracker: #1183

Arguments

string name

The command name to be used in console.

This cannot be a name of existing console command or console variable. It will silently fail if it is.

Arguments

function callback

The function to run when the concommand is executed. Arguments passed are:

  • Player ply - The player that ran the concommand. NULL entity if command was entered with the dedicated server console.
  • string cmd - The concommand string (if one callback is used for several concommands).
  • table args - A table of all string arguments.
  • string argStr - The arguments as a string.

Arguments

function autoComplete=nil

The function to call which should return a table of options for autocompletion. (Autocompletion Tutorial)

This only properly works on the client since it is not networked. Arguments passed are:

  • string cmd - The concommand this autocompletion is for.
  • string args - The arguments typed so far.

Arguments

string helpText=nil

The text to display should a user run 'help cmdName'.

Arguments

number flags=0

Concommand modifier flags. See FCVAR_ Enums.

Examples

Example

Adds a concommand killyourself which will kill the user.

concommand.Add("killyourself",function( ply, cmd, args )
    ply:Kill()
    print("You killed yourself!")
end)


Examples

Example

A concommand that prints the SteamID and nickname of every player on the server.

concommand.Add( "retrieveplayers", function()  
	for _, ply in ipairs( player.GetAll() ) do
		print( ply:Nick() .. ", " .. ply:SteamID() .. "\n" )
	end
end)


Personal tools
Navigation