#sub?

Posted by Dhai on Tue 18 Oct 2005 10:13 PM — 11 posts, 37,311 views.

#0
I think there's something called '#sub' in Zmud, where I can gag the triggered text, and replace it with something else. I've tried to do this in Mushclient, with this trigger:

<triggers>
  <trigger
   enabled="y"
   group="Combat"
   keep_evaluating="y"
   match="^(\w+) springs forward and mauls your (left|right) (arm|leg)\.$"
   omit_from_output="y"
   regexp="y"
   send_to="12"
   sequence="50"
  >
  <send>ColourNote("silver", "black", "%1: .mauled. /%2/ /%3/!")</send>
  </trigger>
</triggers>


But when I test trigger: \0AMalifuus springs forward and mauls your right leg.\0A

It matches, but I don't see the ColourNote. I'd _really_ like to be able to do this, please help!
Australia Forum Administrator #1
It's a characteristic of the way MUSHclient handles triggers and omitting, that if you omit from output it also omits any notes you do in "send to script".

However you can achieve what you want by calling a script subroutine from the trigger, like this:


<triggers>
  <trigger
   enabled="y"
   group="Combat"
   keep_evaluating="y"
   match="^(\w+) springs forward and mauls your (left|right) (arm|leg)\.$"
   omit_from_output="y"
   regexp="y"
   script="mytrigger"
   sequence="50"
  >
  </trigger>
</triggers>


And, in your script file (this example is in Lua) 

function mytrigger (name, line, wildcards)
  ColourNote("silver", "black", wildcards [1] ..
             ": .mauled. /" .. wildcards [2] .. 
             "/ /" .. wildcards [3] .. "/!")
end -- function




The difference is that rather than doing the scripting inline, it is deferred to the script file. That way you see your note.
#2
That sort of solves my problem. The thing is, I have about 30 of these triggers, and not all of the desired substitutions can be done with wildcards. For example, I have, triggering off of:

^A hand\-sized tick with a human face leaps for your throat\.$

the substituted text of
"Ticked!"

I could obviously make a seperate function for each of these triggers but that wouldn't be efficient, and I could also get a special wildcard that's unique to each trigger (like making "hand-sized tick" a wildcard so I could have if statements to check what trigger called the sub function.

Ideally, I'd like to have something like this working:


function sub(text)
  ColourNote("silver", "black", text)
end


where in my triggers, I'd Send-To: Script, and in Send, I'd do

sub("Blah blah blah")


I'd think that would work, as you said, because it's getting deferred to the script file, but as of now I've got that first trigger I posted sending

sub("%1: .maul. /%2/ /%3/")


to the sub function I mentioned earlier, and it is doing the same thing I complained about in my first post.
Australia Forum Administrator #3
Calling a sub in send-to-script doesn't really help. That effectively is still calling the script as the trigger is processed.

Putting a sub name into the "script" field defers execution of the sub until MUSHclient has finished omitting lines, so lines displayed in the script then, are still seen.

However there are ways of dealing with what you are trying to do without writing 30 functions.

For one thing, you can use named wildcards, which is particularly easy to access from Lua scripting. I have reworked my example to show how the parts of the line are named now rather than simply wildcard 1, wildcard 2 etc.

The script could then test to see if a particular name exists (ie. is not nil), and adjust its display accordingly.

Also, the script can see which trigger called it, that is the first argument, the "name" argument. By giving each trigger a different label (name) then the script can conditionally handle different types of triggers.


<triggers>
  <trigger
   enabled="y"
   group="Combat"
   keep_evaluating="y"
   match="^(?P&lt;who&gt;\w+) springs forward and mauls your (?P&lt;which&gt;left|right) (?P&lt;part&gt;arm|leg)\.$"
   omit_from_output="y"
   regexp="y"
   script="mytrigger"
   sequence="50"
  >
  </trigger>
</triggers>



Revised script using names rather than numbers

function mytrigger (name, line, wildcards)

  ColourNote("silver", "black", wildcards.who ..
             ": .mauled. /" .. wildcards.which .. 
             "/ /" .. wildcards.part .. "/!")

end -- function
#4
Ah, yes. Thank you. It'll take a bit more work than I was expecting, but it certainly solves my problem.
USA #5
i want to do roughly the same thing except i dont have any wildcard in the line i want to replace. i was thinking of setting it up in the following manner:

triggers>
<trigger
enabled="y"
group="Combat"
keep_evaluating="y"
match="^a sprig of eliotropia$"
omit_from_output="y"
regexp="y"
script="mytrigger"
sequence="50"
>
</trigger>
</triggers>

i want it to output
a sprig of eliotropia +8

im just not sure how to set up the script to out put it.
Australia Forum Administrator #6
Well a script to simply send static stuff is really simple:


function mytrigger (name, line, wildcards)
  ColourNote("silver", "black", "a sprig of eliotropia +8")
end -- function


However as you are echoing back most of what you received, you can get that from wildcard 0, the whole matching line:


function mytrigger (name, line, wildcards, styles)
  ColourNote("silver", "black", wildcards [0] ..  " +8")
end -- function

Australia Forum Administrator #7
Now if you wanted to do a lot of these, and I assume you do, rather than writing a different script function for each one, you can have a single one, and have it look up the addition in a table. In this case we use the description as the key for matching in the table.


extras = {

  ["a sprig of eliotropia"] = "+8",
  ["a leaf of sunflower"] = "+16",
  ["a bunch of tulips"] = "+4",

}
 
function mytrigger (name, line, wildcards, styles)

  local extra = extras [line] -- table lookup

  -- append extra stuff if found in table
 
  line = line .. " " .. extra or ""
  
  ColourNote("silver", "black", line)
end -- function



Now each trigger can call the same script function, and inside the function it works out what to append to the line.
USA #8
i tried setting it up as you said except i get this error message

There was a problem in script file "D:\Program Files\games\MUSHclient\scripts\sprigs.lua":

The trigger subroutine named "sprig" could not be found.
The trigger (sprig) subroutine named "sprig" could not be found.

i copied and pasted it exactly

i only got that when it was working off a trigger, but when there was no trigger calling it, it didnt work
Russia #9
That means you copied it to a wrong place. If your trigger is inside the world (not in a plugin), then the function must be inside the script file attached to that world in the Configuration->Scripting dialogue. And the function must have the same name as the one you put in the Script field of your trigger.
Australia Forum Administrator #10
The trigger you pasted above calls a function "mytrigger" but the message you pasted says it can't find "sprig". You must have changed the trigger.