Garry's Mod Wiki

Revision Difference

concommand.Add#546738

<function name="Add" parent="concommand" type="libraryfunc"> <description> Creates a console command that runs a function in lua with optional autocompletion function and help text. <bug issue="1183">This will fail if the concommand was previously removed with <page>concommand.Remove</page> in a different realm (creating a command on the client that was removed from the server and vice-versa).</bug> </description> <realm>Shared and Menu</realm> <file line="28">lua/includes/modules/concommand.lua</file> <args> <arg name="name" type="string">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.</arg> <arg name="callback" type="function">The function to run when the concommand is executed. Arguments passed are: * <page>Player</page> ply - The player that ran the concommand. NULL entity if command was entered with the dedicated server console. * <page>string</page> cmd - The concommand string (if one callback is used for several concommands). * <page>table</page> args - A table of all string arguments. * <page>string</page> argStr - The arguments as a string.</arg> <arg name="autoComplete" type="function" default="nil">The function to call which should return a table of options for autocompletion. (<page text="Autocompletion Tutorial">Autocomplete_Tutorial</page>) This only properly works on the client since it is **not** networked. Arguments passed are: * <page>string</page> cmd - The concommand this autocompletion is for. * <page>string</page> args - The arguments typed so far.</arg> <arg name="helpText" type="string" default="nil">The text to display should a user run 'help cmdName'.</arg> <arg name="flags" type="number" default="0">Concommand modifier flags. See <page>Enums/FCVAR</page>.</arg> </args> </function> <example> <description>Adds a concommand `killyourself` which will kill the user.</description> <code> concommand.Add("killyourself", function( ply, cmd, args ) concommand.Add( "killyourself", function( ply, cmd, args ) ply:Kill() print("You killed yourself!") end) print( "You killed yourself!" ) end ) </code> </example> <example> <description>A concommand that prints the SteamID and nickname of every player on the server.</description> <code> concommand.Add( "retrieveplayers", function() for _, ply in ipairs( player.GetAll() ) do print( ply:Nick() .. ", " .. ply:SteamID() .. "\n" ) end end) end ) </code> </example>