Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ General
➜ The "two triggers on the same line" problem
The "two triggers on the same line" problem
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Cadari
(25 posts) Bio
|
Date
| Wed 22 Aug 2007 10:34 PM (UTC) |
Message
| Hello all. Stumbled upon a small (I hope) problem. Here is it. Imagine you have a line:
First thing. Second thing.
The trigger firing on Second thing is not enabled all the time and must be turned on only if the First thing precedes it. So, after we've got the First thing, the second trigger enables, does his work and turns itself off, so it won't fire without getting the First thing first. That's the code:
<triggers>
<trigger
enabled="y"
keep_evaluating="y"
match="First thing"
regexp="y"
send_to="12"
sequence="100"
>
<send>
world.EnableTriggerGroup('second', True)
world.Note('1st trigger fired, 2nd trigger on')
</send>
</trigger>
<trigger
group="BASHER"
match="Second thing"
regexp="y"
send_to="12"
sequence="100"
>
<send>
world.EnableTriggerGroup('second', False)
world.Note('2nd trigger fired and turned off.')
</send>
</trigger>
</triggers>
Everything's working just fine:
You say, "First thing. Second thing."
1st trigger fired, 2nd trigger on
2nd trigger fired and turned off.
You say, "Second thing."
But as I need to do some complex work inside these triggers, I would prefer having the triggers call functions from the script file. The code changes to:
<triggers>
<trigger
enabled="y"
keep_evaluating="y"
match="First thing"
name="first"
regexp="y"
script="first"
sequence="100"
>
</trigger>
<trigger
group="second"
match="Second thing"
name="second"
regexp="y"
script="second"
sequence="100"
>
</trigger>
</triggers>
With the corresponding functions looking exactly like above:
def first(name, line, wildcards):
world.EnableTriggerGroup('second', True)
world.Note('1st trigger fired, 2nd trigger on')
def second(name, line, wildcards):
world.EnableTriggerGroup('second', False)
world.Note('2nd trigger fired and turned off.')
Unfortunately, this breaks everything. Now it works like:
You say, "First thing. Second thing."
1st trigger fired, 2nd trigger on
As you can see, the second trigger fails to fire.
Sorry for such a long depiction of a problem, thought it might help to include everything. Would be grateful for any thoughts. | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #1 on Wed 22 Aug 2007 11:06 PM (UTC) |
Message
| Both triggers listed fire on the same sequence. Try setting the second one to sequence=101. The lower the sequence value, the earlier the trigger is tested. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Cadari
(25 posts) Bio
|
Date
| Reply #2 on Wed 22 Aug 2007 11:10 PM (UTC) Amended on Wed 22 Aug 2007 11:11 PM (UTC) by Cadari
|
Message
| Tried already. Doesn't help. What makes me wonder is how it's working when the code is put in the trigger window and sent to Script and refuses to work in the case of using script file routines. | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #3 on Thu 23 Aug 2007 12:14 AM (UTC) |
Message
| When a script is called from a trigger it is processed immediately. Perhaps when sent to script, all triggers are matched, and then the scripts are run. I'm not terribly familiar with the source though, but it's an educated (at about a second grade level) guess. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Isthiriel
(113 posts) Bio
|
Date
| Reply #4 on Thu 23 Aug 2007 02:28 AM (UTC) |
Message
| Is there any reason why you can't just have one trigger that fires on "First thing. Second thing."?
Or, rather than using EnableTrigger, have a global variable in the script file that the second function checks as to whether it should fire. | Top |
|
Posted by
| Nick Gammon
Australia (23,046 posts) Bio
Forum Administrator |
Date
| Reply #5 on Thu 23 Aug 2007 03:58 AM (UTC) |
Message
| When you call a function in the script file, the processing is deferred, but as far as I can see they should be run in the correct sequence.
However, if you are trying to match on the same line this technique won't work. All triggers are processed, and then scripts (in the script file) are called. Thus it is too late to enable the second trigger.
Why not put most of the processing in the script file (for "second thing") and just leave in place the stuff to enable the second trigger.
However the way you have done it, it won't do exactly what you described anyway.
Since you say you want to match on "second thing" only if "first thing" precedes it, in the same line, all you have done is make sure that if you have "first thing" somewhere on the line, it will match "second thing" somewhere on the line, not necessarily after it.
You probably want something like this:
match = "first thing.*second thing"
That will match "second thing", which is on the same line as "first thing", and after it. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #6 on Thu 23 Aug 2007 04:30 AM (UTC) |
Message
| Well, here's an idea. It looks like you're using VBscript, and I'm not sure what the equivalent would be for that, but here's what I would do with Lua.
<trigger
enabled="y"
keep_evaluating="y"
match="First thing"
script="firstmatch"
regexp="y"
send_to="1"
sequence="100"
>
</trigger>
function firstmatch( sTrig, sLine, wildcards )
Note('1st trigger fired, trying second')
local wc = {}
local match = 0
match, _, wc[1], wc[2], wc[3], wc[4] = string.find( sLine, "^Second thing$" )
if match ~= nil then
wc[0] = sLine
secondmatch( sTrig, sLine, wc )
end
end
function secondmatch( sTrig, sLine, wildcards )
Note('2nd trigger fired and turned off.')
end
I'm pretty sure there's a better way to toss the regex match off of string.find into a table, but I have no idea what that would be. Something similar could be done with whatever vbscript has for regex matching. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Cadari
(25 posts) Bio
|
Date
| Reply #7 on Thu 23 Aug 2007 07:50 AM (UTC) |
Message
|
Quote: Is there any reason why you can't just have one trigger that fires on "First thing. Second thing."?
Yes, unfortunately, there is. One trigger is just not enough to do what I want.
Quote: Why not put most of the processing in the script file (for "second thing") and just leave in place the stuff to enable the second trigger.
Because there's a lot of work in the first trigger as well. Actually, both routines are just huge :)
Quote: Well, here's an idea. It looks like you're using VBscript, and I'm not sure what the equivalent would be for that, but here's what I would do with Lua.
That was Python, but it doesn't matter. Thanks for idea, I'll try this out.
My curiosity as to why it's working in one case and does not in another fulfilled :) Reading Isthiriel's post I just realized, that using a global variable would serve my purpose even better than what I was trying to do. Everything's working right now :) Thank you all for the input! | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
21,595 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top