hmm, what have I stuffed up? (AddTriggerEx question)

Posted by Wyd on Sun 18 Jun 2006 03:47 AM — 7 posts, 27,251 views.

#0
Ok, I have just started playing around with lua coding, and need to create a trigger within a function... I came up with this:


AddTriggerEx(id,currentroom,
"WORM WARP TO " .. string.upper (destination),
trigger_flag.Enabled or trigger_flag.RegularExpression,
custom_colour.NoChange,0," "," ",2,100)


id, currentroom and destination all have values, and the function seems to work without a problem...except the trigger doesn't seem to work.

Also, if I create a trigger like this, will it be saved within the plugin when I close mushclient?

Thanks in advance for the help

Wyd
USA #1
This line is probably the problem: trigger_flag.Enabled or trigger_flag.RegularExpression.

For starters, you need to have those as the flags, but you probably have that already.

But more importantly, or in Lua is the binary or, not the bitwise or. Look at this example:
$ lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> =(1 or 2)
1
> 
We should have gotten 3, not 1, but since it's binary, we got 1.

If you can guarantee that the operands in your expression will all be powers of 2 -- flags, never combinations of flags -- you can simply sum them instead of trying to combine them with bit operations.

You can also extend Lua with bitwise operators, as described in this tutorial:
http://lua-users.org/wiki/BinaryModuleTutorial

Maybe if you ask Nick nicely, he can include the bitwise library as a default extension to the Lua environment. :)
Australia Forum Administrator #2
To troubleshoot you should get the return value from the call.

eg.


id = "test"
currentroom = "blah"
destination = "somewhere"

print (
AddTriggerEx(id,currentroom,
"WORM WARP TO " .. string.upper (destination),
trigger_flag.Enabled or trigger_flag.RegularExpression,
custom_colour.NoChange,0," "," ",2,100)
)


This prints 30009. That tells you that the call failed with error code 30009. You can look that up in the documentation, or use the "check" function supplied in the exampscript.lua file:



id = "test"
currentroom = "blah"
destination = "somewhere"

check (
AddTriggerEx(id,currentroom,
"WORM WARP TO " .. string.upper (destination),
trigger_flag.Enabled or trigger_flag.RegularExpression,
custom_colour.NoChange,0," "," ",2,100)
)

Opens dialog:

[string "Immediate"]:5: Script name is not in the script file


Your problem here is that the script suboutine name you have given is " " (note the space). If you want no script it must be "" (no space - that is, an empty name).

Your other problem is that the expression:


trigger_flag.Enabled or trigger_flag.RegularExpression


... won't do what you expect. Try printing it:


print (trigger_flag.Enabled or trigger_flag.RegularExpression) --> 1


In Lua the "or" is a logical or, not a bitwise or. You should do one of two things here:


bit.bor (trigger_flag.Enabled, trigger_flag.RegularExpression) --> 33


That does a bitwise "or" and uses the "bit" library included with MUSHclient. Or, perhaps more simply, just add them together:


trigger_flag.Enabled + trigger_flag.RegularExpression --> 33

Amended on Sun 18 Jun 2006 04:39 AM by Nick Gammon
Australia Forum Administrator #3
Quote:

Also, if I create a trigger like this, will it be saved within the plugin when I close mushclient?


No, plugins do not extend themselves like this. Plugins can be shared between worlds, and only the variables are saved to the plugin "state" file.

If plugins need to create triggers "on the fly" they will have to recreate them each time. You could use ExportXML to save the trigger (into a variable for example) and ImportXML to put it back next time.
USA #4
(There should be a way to see easily that somebody replied while you were posting... hmm.)

Looks like Nick already included the bitwise libraries; that's good, since it makes it much easier to solve this problem. :)
Australia Forum Administrator #5
Quote:

Maybe if you ask Nick nicely, he can include the bitwise library as a default extension to the Lua environment


See:

http://www.gammon.com.au/scripts/doc.php?general=lua

Look for the "Bit manipulation library" in there.
USA #6
Heh, we made about 3 posts right past each other. :-)
Quote:
Looks like Nick already included the bitwise libraries;
I've never seen a forum that notified users of posts made while they were writing a post. It would prevent a fair amount of missing posts because you thought you were the last to say something.