Garry's Mod Wiki

Revision Difference

concommand.Add#517269

<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.&amp;amp;lt;br&amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;&#xA;&#xA;This cannot be a name of existing console command or console variable. It will silently fail if it is.</arg> <arg name="name" type="string">The command name to be used in console.&#xA;&#xA;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:&#xA;* &lt;page&gt;Player&lt;/page&gt; ply - The player that ran the concommand. NULL entity if command was entered with the dedicated server console.&#xA;* &lt;page&gt;string&lt;/page&gt; cmd - The concommand string (if one callback is used for several concommands).&#xA;* &lt;page&gt;table&lt;/page&gt; args - A table of all string arguments.&#xA;* &lt;page&gt;string&lt;/page&gt; 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. ([Autocompletion Tutorial](/gmod/Autocomplete_Tutorial))&#xA;&#xA;This only properly works on the client since it is **not** networked. Arguments passed are:&#xA;* &lt;page&gt;string&lt;/page&gt; cmd - The concommand this autocompletion is for.&#xA;* &lt;page&gt;string&lt;/page&gt; args - The arguments typed so far.</arg> <arg name="helpText" type="string" default="nil">The text to display should a user run &#x27;help cmdName&#x27;.</arg> <arg name="flags" type="number" default="0">Concommand modifier flags. See &lt;page&gt;FCVAR&lt;/page&gt;.</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>