trig type: lua function condition

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

trig type: lua function condition

Post by Caled »

From reading the help files, I believe this trigger type is similar to the one zmud/cmud calls an "expression" trigger type. That is, rather than firing on a line from the mud output, it fires when its expression evaluates to be true.

I've played around with it, and read the help files, but I can't get a clear idea on how to make this work as I want it to, though. (Part of my problem is still with just learning the lua of course).

Code: Select all

Pattern: if eqBal_check() = true
Command:
raiseEvent( "onEqBal" , pstats , eqbal , armb )
Script:

Code: Select all

function eqBal_check()
    if eqBal="eb" and armb="lr" then
        return true
    else
        return false
    end
end
Sorry to be asking more questions, I promise I'm doing my best with the help files first though. I want to raise the "onEqBal" event whenever the "armb" and "eqbal" variables indicate I am fully balanced. I think that something like what I have should do that, but I think I'm getting the trigger quite wrong.

guest

Re: trig type: lua function condition

Post by guest »

if thisVariable == 10 then ....

A == B A equals B
A <= B A smaller or equal to B
A >= B A greater or equal to B
A ~= B A not eaqual to B

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: trig type: lua function condition

Post by Caled »

The bit I don't get, is do I put the whole if expression in the pattern, and the "then" part in the commands, or the entire if statement in the pattern? The help file seems to suggest the second, but I don't see how that is possible.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: trig type: lua function condition

Post by Heiko »

Caled wrote:From reading the help files, I believe this trigger type is similar to the one zmud/cmud calls an "expression" trigger type. That is, rather than firing on a line from the mud output, it fires when its expression evaluates to be true.

I've played around with it, and read the help files, but I can't get a clear idea on how to make this work as I want it to, though. (Part of my problem is still with just learning the lua of course).

Code: Select all

Pattern: if eqBal_check() = true
Command:
raiseEvent( "onEqBal" , pstats , eqbal , armb )
Script:

Code: Select all

function eqBal_check()
    if eqBal="eb" and armb="lr" then
        return true
    else
        return false
    end
end
Sorry to be asking more questions, I promise I'm doing my best with the help files first though. I want to raise the "onEqBal" event whenever the "armb" and "eqbal" variables indicate I am fully balanced. I think that something like what I have should do that, but I think I'm getting the trigger quite wrong.
No problem that you ask questions. Mudlet is still in the making and there will definitely be more people who have the same difficulties, but who are too afraid to ask questions. This forum is intended as a permanent source of information. I'm currently assembling the manual so it's a good time to ask with respect to the manual, so I can see where I need to clarify things more or give more detailed information.

For expression triggers to work you need to compile Mudlet yourself from the latest git sources.
For Ubuntu users this is very simple.
You find instructions here: http://mudlet.sourceforge.net/wordpress/?page_id=30

Lua function conditions can be used as expression triggers if you put your expression in a free function (-> Trigger Editor -> Scripts) that returns a truth value (true or false). Then the trigger will fire when this functions returns true e.g. if you have gained balance.

The simplest solution would be: to make the trigger a multi-condition AND trigger and use 2 Lua expression:

Lua function condition #1:
return eqBal == "eb"
Lua function condition #2:
return armb == "lr"
and as script:
raiseEvent( ....)

another solution would be:

Lua function condition:
eqBal_check()

free function defined in some script item:

Code: Select all

function eqBal_check()
    if eqBal == "eb" then
        if armb == "lr" then
            return true
        end
    end
    return false
end
The operators "and" and "or" in Lua do *NOT* return boolean values. They behave differently. Have a look here: http://lua-users.org/wiki/ExpressionsTutorial

The second issue is:
Command:
raiseEvent( "onEqBal" , pstats , eqbal , armb )
The "send text" field (formerly called command) sends its content as plain text as is. If you put Lua code in there it will not be executed but sent to the MUD as is.
Consequently, you'd want to put your rainseEvent() call into the trigger script instead.
Using events is much superior to using lua function conditons as expression conditions on a general basis as it's a lot faster and generally helps improve your system design as it decouples the event generator from the event recievers and thus greatly simplifies system creation as the event sender doesn't have to care about if or how the event is being processed.
I haven't yet arrived at the event section of the manual, but you find some info on how to declare event handlers in the lua api table on raiseEvent() and I've made an simple example that uses events in my Achaea inline map demo script. When you have compiled the latest sources yourself you can download the improved second version of the script and experiment. It should work for you now.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: trig type: lua function condition

Post by Heiko »

A little system design tip:

The simplest and fastest solution for your problem would be to stay away from expression triggers completely and simply use a trigger chain with your prompt detection trigger as the filter:
little substring trigger on "eb" and another little substring trigger on "lr" each of which set a little variable in their respective script such as eqBal=true.
In your prompt detection trigger script you simply add the line: raiseEvent("gotPrompt")

As events are processed *after* all triggers have been processed you can be sure that both of your little trigger chain elements have been run and set their respective values before the onPrompt-event handler is being called. Consequently, in your gotPrompt-event handler function all the values you are interested in have been set beforehand and you can evaluate them and act accordingly. This approach is both simpler and faster than the expression trigger solution as using Lua function conditions effectively means that you run the Lua code on every single line you receive from the MUD, unless the LFCs are part of a multi-condition trigger, which would mean that the LFCs would only be run when they would be the current condition to be tested of their respective trigger state. LFCs are implemented in such a way that the trigger engine only calls the compiled byte code, but running as few scripts as possible is always the best idea.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: trig type: lua function condition

Post by Caled »

Thanks for the help, I've learned a lot from all that. My reason for looking at expression trigs is basically because my techniques for efficient scripting come from dealing with the oddities of CMUD and getting that to perform well (an expression trigger in this instance performs much better than an if statement in the prompt trigger - CMUD only though, not zMud).

So I need to try and forget those preconceptions I have about efficient scripting.

I populate my 'armb' variable from triggers:

Code: Select all

Pattern: Left arm balance used
Script
leftarmb = "-"
armb = leftarmb .. rightarmb

Pattern: Right arm balance used
Script
rightarmb = "-"
armb = "-"

Pattern: You have recovered balance on your left arm.
Script
leftarmb = "l"
armb = leftarmb .. rightarmb

etc
^ In CMUD I did that with two triggers and just the 'armb' variable. For lua, I was restricted to the basic stuff I'd learned already. The ease of concatenation in lua is really nice though, and made this easy.


To raise it from the prompt trigger, I need to add something to stop it from raising the event every prompt that I have balance (I only want it to raise the event once, upon regaining complete balance).

So..

Code: Select all

Pattern: H:(\d+)\|(\d+) (\d+)% Exp:(\d+) <(\w+) ([eb\-]{2})
Script:
-- Set variables
pstats = matches[6]
eqbal = matches[7]
----------------------
-- Raise onEqbal upon regaining full balance and equilibrium
if regaining_bal = 1 then
    if eqbal = "eb" then
        if armb = "lr" then
            raiseEvent(onEqbal, etc)
            regaining_bal = 0
        end
    end
end
------------------------
That would be a lot simpler than using expression triggers. All I would need to do is add..
regaining_bal = 1
..to all the triggers that fire when I lose balance, arm balance or equilibrium. That is those two triggers at the start of this post, and another two more that look similar.

Do I need to define the else in a lua if statement, if I don't want to actually do anything in it?
Actually, that question I should be able to answer myself from the help files.

guest

Re: trig type: lua function condition

Post by guest »

Lua syntax errors in your script:

if regaining_bal == 1 then
if eqbal == "eb" then
if armb == "lr" then

Post Reply