Garry's Mod Wiki

Revision Difference

concommand.Add#561431

<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="24-L33">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. <callback> <arg name="ply" type="Player">The player that ran the concommand. NULL entity if command was entered with the dedicated server console.</arg> <arg name="cmd" type="string">The concommand string (if one callback is used for several concommands).</arg> <arg name="args" type="table">A table of all string arguments.</arg> <arg name="argStr" type="string">The arguments as a string.</arg> </callback> </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">Console_Command_Auto-completion</page>) This only properly works on the client since it is **not** networked. ⤶ <CallbAck>⤶ ⤶ <callback>⤶ <arg name="cmd" type="string">The concommand this autocompletion is for.</arg> <arg name="args" type="string">The arguments typed so far.</arg> <ret name="tbl" type="table">A table containing the autocomplete options to display.</ret> </Callback>⤶ </callback>⤶ </arg> <arg name="helpText" type="string" default="nil">The text to display should a user run 'help cmdName'.</arg> <arg name="flags" type="number" alttype="table" default="0">Console command modifier flags. Either a bitflag, or a table of enums. 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 ) ply:Kill() 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 ) </code> </example>