Garry's Mod Wiki

Revision Difference

Autocomplete_Tutorial#517309

<cat>Dev.Lua</cat> # Introduction ⤶ Autocomplete is a feature that predicts words and helps you increase the efficiency of your console commands. By general, Garry's Mod will give you some auto completion by showing you the available console commands, but you can (somewhat) easily extend this.<br/>⤶ <image src="autocomplete1.jpg" alt="Autocomplete_showing_several_DarkRP_commands"/>⤶ Autocomplete is a feature that predicts words and helps you increase the efficiency of your console commands. By general, Garry's Mod will give you some auto completion by showing you the available console commands, but you can (somewhat) easily extend this.<br/>⤶ <br/> This tutorial will help you create both a clientside and a (more advanced) serverside console commands with autocomplete.&lt;br clear=all&gt; # Clientside Console commands ## Let's start We're going to create a clientside console command called "rp_steamid" which will find the SteamID by inputing a player's nick. Simple enough, but with the help of autocomplete we can make this a nice and fast experience. ``` local function GetSteamID( ply, cmd, args ) local nick = args[1] nick = string.lower( nick ) for k,v in pairs( player.GetAll() ) do if string.find( string.lower( v:Nick() ), nick ) then MsgN( "SteamID: " .. v:SteamID() ) return end end MsgN( "Couldn't find player." ) end concommand.Add( "rp_steamid", GetSteamID ) ``` This is how a simple steamid grabbing script could look like. We can now type in ``` rp_steamid Donkie ``` And out comes ``` > SteamID: STEAM_0:0:16436609 ``` ⤶ ⤶ ⤶ <image src="autocomplete2.jpg" alt="The_current_state"/>⤶ But as you can see on the image, we have no clue who on the server is called Don, there might be several people named similarly, which can give us a hard day finding who it actually was who we wanted the SteamID from, the player can also have a bunch of weird characters which is impossible for us to type into the field without a bunch of copypasting.<br/> <br/> To enable autocomplete, we have to create our own function that defines what we should display to the user. This is done like this:&lt;br clear=all&gt; ``` local function AutoComplete( cmd, stringargs ) print( cmd, stringargs ) end concommand.Add( "rp_steamid", GetSteamID, AutoComplete ) ``` Now, everytime the user types or removes a character from the console input, the function AutoComplete will get called along with the parameters.<br/> * cmd: Will always be the command, not useful for us right now. (Gives "rp_steamid"). * stringargs: Gives us the rest of the input. Typing in "rp_steamid Don" will make stringargs = " Don". In this function we are also meant to return something, a table. This table should contain the set of values we want to display to the user in the autocomplete box. ``` local function AutoComplete( cmd, stringargs ) print( cmd, stringargs ) local tbl = {} table.insert(tbl, "Test 1") table.insert(tbl, "Test 2") table.insert(tbl, "Test 3") return tbl end ``` Gives us<br/> <br/>⤶ <image src="autocomplete3.jpg"/><br/>⤶ Which should give you a clear idea how this works. All we need to do now is to return all players matching "Don" instead of a bunch of test values. ``` local function AutoComplete( cmd, stringargs ) print( cmd, stringargs ) stringargs = string.Trim(stringargs) -- Remove any spaces before or after. stringargs = string.lower(stringargs) local tbl = {} for k,v in pairs(player.GetAll()) do local nick = v:Nick() if string.find( string.lower( nick ), stringargs ) then nick = "\"" .. nick .. "\"" -- We put quotes around it in case players have spaces in their names. nick = "rp_steamid " .. nick -- We also need to put the cmd before for it to work properly. table.insert(tbl, nick) end end return tbl end ``` ⤶ <br/>⤶ ⤶ <image src="autocomplete4.jpg"/> <image src="autocomplete5.jpg"/><br/>⤶ And we can simply click one of the values to get the steamid of that person. # Serverside Console Commands Console commands registered in the server realm will not appear on clients connected to the server and as such there is no reason for autocomplete on such commands. You can still do it via the same method as above, but it will only be usable in the very specific case where someone hosts the server (a listen server) from their game client, the autocomplete will only work for the host of the server since the command will only appear for them. Same applies to singleplayer games. # The End Thanks for reading this, I hope that more people will use autocomplete with their console commands in the future. Over and out -- 12:49, 26 February 2014 (UTC) Over and out --[[User:Donkie|Donkie]] 12:49, 26 February 2014 (UTC) ## Disclaimer I am well aware that my code can be optimized beyond max, I simply dumbed it down for it to be easier understood.