Garry's Mod Wiki

Revision Difference

auto-complete#547746

<cat>Dev.Lua</cat>⤶ # Auto-complete Overview⤶ ⤶ Auto-complete is a feature that predicts words & phrases, it helps the user understand a console command & increase their efficiency whilst using it.⤶ ⤶ ## Auto-completion with functions⤶ ⤶ To create auto-complete for your console command, a function named `AutoComplete` must be created. The function will determine what values are displayed based on what is passed to the function.⤶ ⤶ *@param* `cmd` — Will always be the command, not useful for us right now. (Gives "rp_steamid").⤶ ⤶ *@param* `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.⤶ ⤶ The function below gives the result as seen below the function.⤶ ⤶ ```lua⤶ local function AutoComplete(cmd, stringargs)⤶ local tbl = {⤶ "Test 1",⤶ "Test 2",⤶ "Test 3",⤶ }⤶ return tbl⤶ end⤶ ```⤶ ⤶ <image src="autocomplete3.jpg"/><br/>⤶ ⤶ ⤶ The function below gives the result as seen below the function.⤶ ⤶ ```lua⤶ local function AutoComplete(cmd, stringargs)⤶ --- Trim the arguments & make them lowercase.⤶ stringargs = string.Trim(stringargs:lower())⤶ --- Create a new table.⤶ local tbl = {}⤶ for _, ply in pairs(player.GetAll()) do⤶ local name = ply:Nick()⤶ if name:lower():find(stringargs) then⤶ --- Add the player's name into the auto-complete.⤶ name = "rp_steamid \"" .. name .. "\""⤶ table.insert(tbl, name)⤶ end⤶ end⤶ --- Return the table for auto-complete.⤶ return tbl⤶ end⤶ ```⤶ ⤶ <image src="autocomplete4.jpg"/>⤶ <image src="autocomplete5.jpg"/>⤶ ⤶ ## Console command replication⤶ ⤶ Console commands are not replicated. If you'd like to replicate a console command, you'll have to use <page>Global.AddCSLuaFile</page>, or create custom behavior on both the server & the client.⤶ ⤶ Special thanks to old-time contributors for the images.⤶ The code was written by shawnjbragdon.