Variable trigger matching (or creating a rewrite rule)

Posted by Tharius on Sun 01 Dec 2013 03:41 AM — 5 posts, 25,068 views.

#0
I'm looking to create a trigger to match my prompt but rather than hardcode the match pattern I'd like to use the mud output of the prompt change command to rewrite the trigger.

eg (apologies ... I'm used to zmud but looking to get a lot better at mush's scrpiting system):

#trig {matchMyPromptRegEx} {assignVariables}
#trig {matchMultiLinePromptChangeText} {#untrig matchMyPromptRegEx;#trig {newMatchMyPromptRegEx} {assignVariables}}

In essence either I'd like to have a trigger that tests for a match against a variable in which would be stored a regex or a method to delete/modify an existing trigger from within the lua scripting (sort of akin to the way you can use javascript to rewrite a drop down box in html).

The idea is that if I change my prompt I don't have to go hand editing my scripts, that the change will be registered.

Thanks
Australia Forum Administrator #1
Yes, the match text can itself be a variable, eg.

@my_prompt

If you are matching on a regular expression you would need to be cautious about auto-generating this variable, as some characters have special meaning inside a regexp, however in principle you can certainly do that.

Quote:

... or a method to delete/modify an existing trigger from within the lua scripting ...


That is achievable too, but using the variable is probably simpler.

Template:function=SetTriggerOption
SetTriggerOption

The documentation for the SetTriggerOption script function is available online. It is also in the MUSHclient help file.

#2
Nick Gammon said:

Yes, the match text can itself be a variable, eg.

@my_prompt

If you are matching on a regular expression you would need to be cautious about auto-generating this variable, as some characters have special meaning inside a regexp, however in principle you can certainly do that.


Nick,

Thanks for the speedy reply ... and the good news. Matching against a variable seems the easiest, I'll go ahead and play with it and see what comes of it.

I had realized the warning about the reg ex special characters but if I can get the variable approach working perhaps I don't need to be so general after all and can do something much simpler.

Steve
#3
Nick,

As you anticipated there is some difficulty with getting the matching regex from inside the variable to work.

When I match from the variable but exclude the regex expressions (eg I match the literal text) the technique works perfectly.

So here's a version of it, I have also tried \w, (\w) \(\w\) and double slash combinations etc so normal string escaping doesn't seem to be the solution (I simplified it from a larger expression to get as simple an example as possible)

It "seems" that although the string parses in ok, that the regular expression evaluation does not happen after variable expansion.

This is precisely what I need to complete this small prompt writing script and would open up a world of possibilities.

<variables>
<variable name="testvar">A little \\w string</variable>
</variables>

<triggers>
<trigger
enabled="y"
expand_variables="y"
keep_evaluating="y"
match="@testvar"
regexp="y"
send_to="2"
sequence="100"
>
<send>saw testvar</send>
</trigger>
</triggers>
Australia Forum Administrator #4
Are you trying to match some word? In that case the testvar needs to be:


<variables>
  <variable name="testvar">A little (\w+) string</variable>
</variables>


And the trigger needs a "!" after the "@" to tell it not to convert the regular expression, like this:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   expand_variables="y"
   keep_evaluating="y"
   match="@!testvar"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>saw testvar, got word: %1</send>
  </trigger>
</triggers>


With those changes, it works:



A little fish string
saw testvar, got word: fish


(MUD output in bold).