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.
This tutorial will help you create both a clientside and a (more advanced) serverside console commands with autocomplete.
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.
This is how a simple steamid grabbing script could look like. We can now type in
And out comes
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.
To enable autocomplete, we have to create our own function that defines what we should display to the user. This is done like this:
Now, everytime the user types or removes a character from the console input, the function AutoComplete will get called along with the parameters.
- 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.
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.
And we can simply click one of the values to get the steamid of that person.
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.
Thanks for reading this, I hope that more people will use autocomplete with their console commands in the future.
Over and out --[[User:Donkie|Donkie]] 12:49, 26 February 2014 (UTC)
I am well aware that my code can be optimized beyond max, I simply dumbed it down for it to be easier understood.