• Could someone explain auto complete please? Thanks
    7 replies, posted
Hey, I have never understood how to use the autocomplete function and i would really like to try it. Thanks in advance :) Oh and could you give an example too? The wiki was no help to me :/
The third argument of [b][url=http://wiki.garrysmod.com/?title=Concommand.Add]Concommand.Add [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] should be a function which returns a table of autocomplete options. I think the autocomplete function is only called clientside though, so I think you'd have to create the concommand shared so it would work? (can someone check me on this? I'm not sure).
ok thanks :D I have understood, but is the 2nd argument a string or a table? cause when i do #args == 2 return DoShit() end that gets called when i put the second letter it [editline]2nd January 2011[/editline] must be a string, cause it gets the same results as [CODE] lua_run print( #"lo" == 2 ) > print( #"lo" == 2 )... true [/CODE]
-snip-
Wiki says: [code]autoCompleteFunc has two arguments - commandName and args. Command name is the name of the command being typed, and args is a list of arguments that have been typed so far.[/code] You should read more carefully nigaglio.
[QUOTE=Wizard of Ass;27146280]Wiki says: [code]autoCompleteFunc has two arguments - commandName and args. Command name is the name of the command being typed, and args is a list of arguments that have been typed so far.[/code] You should read more carefully nigaglio.[/QUOTE] From his test it seems the wiki is wrong though.
The wiki needs to be more clear on this, it doesn't say that the second argument is a table, it says that it is a list of arguments that have been typed. It is actually just a string containing everything typed after the command.
[QUOTE=Drakehawke;27146040]The third argument of [b][url=http://wiki.garrysmod.com/?title=Concommand.Add]Concommand.Add [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b] should be a function which returns a table of autocomplete options. I think the autocomplete function is only called clientside though, so I think you'd have to create the concommand shared so it would work? (can someone check me on this? I'm not sure).[/QUOTE] You should NEVER create a concommand shared, that will make the game confused, and it will end up telling you that the concommand doesn't exist because it can only run the concommand serverside or clientside, not both. If your concommand does something serverside, create a "hidden" concommand serverside which does what you want your concommand to do, and then create the actual concommand clientside and make it run the hidden serverside command. [lua]local autocomplete = {"world", "people", "dood", "maggot", "guys", "ladies", "lololol"} if SERVER then -- SERVERSIDE part -- "hidden" concommand, makes the player who ran it say something local function CC_Hello(pl, cmd, args) pl:ConCommand("say", "hello "..args[1]) end concommand.Add("__sv_hello", CC_Hello) else -- CLIENTSIDE part -- accessible concommand, runs the serverside concommand -- this is the one you should use local function CC_Hello(pl, cmd, args) RunConsoleCommand("__sv_hello") end -- autocomplete function local function AC_Hello(cmd, args) local tbl = {} -- searches for autocomplete entries which begin with whatever you are typing local pattern = "^"..args for _,v in ipairs(autocomplete) do if string.find(v, pattern) then table.insert(tbl, cmd.." "..v) end end return tbl end concommand.Add("hello", CC_Hello, AC_Hello) end[/lua] Simple example. It makes you say "hello <whatever you typed>". Let's say you started to write "hello l" in the console, AC_Hello with be called with cmd="hello" and args="l". Thus, pattern="^l", and when used with string.find, that will tell you if the word you want to check BEGINS with l. So the for loop iterates through the list of autocomplete entries, and looks for words which begin with whatever the variable args contains. Whenever it finds one, it inserts "hello <entry>" into the autocomplete table. In the end, you have tbl={"hello ladies", "hello lololol"} because "ladies" and "lololol" are the only entries which begin with "l". [editline]2nd January 2011[/editline] I suck at explaining. :saddowns:
Sorry, you need to Log In to post a reply to this thread.