concommand/Add

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Described how the underscore bullshit works.)
Line 1: Line 1:
 
{{Func
 
{{Func
 
|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.
 +
 +
NOTE:
 +
If you register a command on the server with something like concommand.Add("mycommand",function() print("asdf") end) and then call it client side with RunConsoleCommand("mycommand") then you will get a message saying "Unknown command 'mycommand'". To fix this, rename your command to contain an underscore.
 
|Realm=Shared
 
|Realm=Shared
 
|IsClass=No
 
|IsClass=No

Revision as of 05:51, 31 July 2015

 concommand.Add( )

Contents

Description

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

NOTE: If you register a command on the server with something like concommand.Add("mycommand",function() print("asdf") end) and then call it client side with RunConsoleCommand("mycommand") then you will get a message saying "Unknown command 'mycommand'". To fix this, rename your command to contain an underscore.

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 the 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 )
    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 _, v in pairs( player.GetAll() ) do
		print( v:Nick() .. ", " .. v:SteamID() .. "\n" )
	end
end)


Personal tools
Navigation