View Full Version : Quick question of my own
Hey all
Can anyone quickly tell me how to go about writing a command that accepts multiple arguments which are separated into multiple variables, but the last variable can be a number of words, without using something like:
addCommand("me","cmdMe",1)
It would be ideal to be able to give a message to the player in the event of incorrect input, instead of nothing - hence why I'd rather not use addCommand's third parameter.
Please advise!
Thanks
yo, this should work:
addCommand("me", "cmdMe")
function cmdMe(PlayerID, ArgumentOne, ArgumentTwo, ArgumentThree, ArgumentFour, ...)
-- the 3 dots mean to fetch everything the player typed after ArgumentFour, and put them into an assossiative table called 'arg'.
-- you could then loop around the table and split them into variables
-- If the player typed /me is testing a b c d e f g h
ArgumentOne = "is"
ArgumentTwo = "testing"
ArgumentThree = "a"
ArgumentFour = "b"
arg[1] = "c"
arg[2] = "d"
arg[3] = "e"
arg[4] = "f"
arg[5] = "g"
arg[6] = "h"
end
Thanks Mex!
And here's a function I just threw together to concatenate a table (I'm just used to PHP, etc so I call 'em arrays :)):
function concatArray(Array)
local At = 1
local Output = ""
while Array[At] do
Output = Output.." "..Array[At]
At = At + 1
end
return string.sub(Output, 2)
end
And here's a /me function built using it.
function cmdMeTwo(PlayerID, ...)
outputChatBox("** "..getPlayerName(PlayerID).." "..concatArray(arg),255,255,255,255)
end
addCommand("metwo","cmdMeTwo")
Clearly that's excessive for a /me command but for /sus or anything else that requires text input after other parameters it's just the trick.
youlikethaaaat
06-30-2009, 06:23 PM
Thanks Mex!
And here's a function I just threw together to concatenate a table (I'm just used to PHP, etc so I call 'em arrays :)):
function concatArray(Array)
local At = 1
local Output = ""
while Array[At] do
Output = Output.." "..Array[At]
At = At + 1
end
return string.sub(Output, 2)
end
Wouldn't it be better to just use: table.concat({...}, " ") instead of writing a function to do basically the same thing..?
You could have told me that before :p