• Making smart command args?
    5 replies, posted
Hey everyone. First of all, this is my code: [LUA] function TEMPTestHUD( ply, cmd, args ) local counter = 0 local message = "" for k,v in pairs(args) do counter = counter + 1 end if counter == 0 then message = "" elseif counter == 1 then message = args[1] elseif counter == 2 then message = args[1].." "..args[2] elseif counter == 3 then message = args[1].." "..args[2].." "..args[3] elseif counter == 4 then message = args[1].." "..args[2].." "..args[3].." "..args[4] end GLOBALplayerNotifyAll( message ) end concommand.Add("ftmtemp_testhud", TEMPTestHUD) [/LUA] You can probably figure out what I am trying to do. There must be a smarter way of doing this. I hopefully don't have to keep on this chain for another 20 args[]... Any help is appreciated :D - Fillipuster
[QUOTE=Fillipuster;45593940]Hey everyone. First of all, this is my code: function TEMPTestHUD( ply, cmd, args ) local counter = 0 local message = "" for k,v in pairs(args) do counter = counter + 1 end if counter == 0 then ... [/QUOTE] For starters, there is a shortcut operator for getting the number of elements in a numerically-indexed table (e.g., the args parameter from concommand): [LUA]if ( #args == 0 ) then -- etc [/LUA] Secondly, there is a 4th parameter in the callback function of concommand.Add: [LUA] concommand.Add( "Test", function( ply, cmd, args, str ) print( str ) end ) [/LUA] It provides you with the exact values they inputted after the command. For example, typing "Test this is a test" would print back "this is a test".
[QUOTE=Mista Tea;45594037]I'll reply a bit more later but for starters there is a shortcut operator for getting the number of elements in a numerically-indexed table (e.g., the args parameter from concommand): if ( #args == 0 ) then -- etc[/QUOTE] Thats nice for a start. Does this also work with Player.GetAll() for returning the number of players on the server?
You could try something like this [LUA] local msg = "" if #args < 1 then return msg end for i = 1, #args do msg = ("%s%s "):format ( msg, args[i] ) end return msg [/LUA] wait just use table.concat [LUA] msg = table.concat( args, " " )[/LUA] ok fuck me just use the 4th arg like mista tea said :suicide:
[QUOTE=Fillipuster;45594067]Thats nice for a start. Does this also work with Player.GetAll() for returning the number of players on the server?[/QUOTE] Yes, you can use it with any numerically-indexed table, including player.GetAll(). I updated my first post. [B]concommand.Add[/B] has a [B]4th parameter[/B] that is a string containing all of the passed arguments, so you don't need to do any extra work [LUA]function TEMPTestHUD( ply, cmd, args, str ) GLOBALplayerNotifyAll( str ) -- as simple as that end concommand.Add("ftmtemp_testhud", TEMPTestHUD)[/LUA]
[QUOTE=Mista Tea;45594167]Yes, you can use it with any numerically-indexed table, including player.GetAll(). I updated my first post. [B]concommand.Add[/B] has a [B]4th parameter[/B] that is a string containing all of the passed arguments, so you don't need to do any extra work [LUA]function TEMPTestHUD( ply, cmd, args, str ) GLOBALplayerNotifyAll( str ) -- as simple as that end concommand.Add("ftmtemp_testhud", TEMPTestHUD)[/LUA][/QUOTE] Ah, thats a no-brainer. Thanks alot Mista Tea! And thanks to you too rejax. I might be able to use that example somewhere else :D Cya later.
Sorry, you need to Log In to post a reply to this thread.