Parameters

Posted by tobiassjosten on Mon 25 Apr 2005 09:30 AM — 6 posts, 28,561 views.

Sweden #0
I found a LUA function on these forums, which looked like this:
function mytrigger (name, line, wildcards)

  Note ("In trigger ", name)

  Send ("go ", wildcards.where)  -- follow him
  Send ("kill ", wildcards.who)  -- attack him again

end


Now I'm wondering, where do you set those parameters (name, line, wildcards)? Are they just fetched from the trigger? What additional info can you include?
USA #1
this function is apparently called by a trigger using the script tag in xml..

<trigger
...
script="..."
...
></trigger>

MC then automatically calls the function nameed in the tag, and sets name to the name of the trigger, if any, line to the entire line that was matched, and wildcs to an array that contains any wildcards that were matched by a regular expression.
USA #2
Script functions called by triggers (and aliases) get three parameters. The name of the trigger that called it, the line that the trigger matched on, and then an array (or the equivalent in whatever language youre working with) of the wildcards.
Likewise, Timers are only send the timername (since there are no wildcards or lines).

Those are just the parameters of any script function that you want to use via the 'script' field (the script function to execute with a given trigger/alias/etc).
Sweden #3
I see, thanks. Could I skip out on using some parameters too? Like:
function mytrigger (line)

Would the line-parameter be set to the name of the trigger in that example? Eg, is the parameters represented in that function by their name, or their order?
#4
You need all three parameters for the function to execute properly. I've seen people using triggers to call script functions and ignoring all three parameters completely, which I find pointless since they should have just called the script function from the body of the trigger command and done it the right way.
USA #5
The parameters are nameless, like all other languages with subroutines.

Unlike other languages (where you have to match function names and argument lists), Lua doesn't care what the function's arguments looks like, it could have one argument, or five. Mushclient will always pass three, if you have only one argument in your function definition, then that will become the name, and the rest of the passed arguments will be discarded. If you have five, then your first three will be the name, the line, and the wildcards, and then your fourth and fifth would be nil.

So, if you just wanted the line, you'd need two parameters, one for the name, the second for the line.