Can I create a custom event?

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Can I create a custom event?

Post by zhenzh »

I'd like to have a custom event rather than those pre-defined sysEvents.
For example, I want an event keep on monitoring specified lua coroutine thread. Each time the coroutine status get to suspended, the event will be risen.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Can I create a custom event?

Post by Vadi »

Sure - you can do that with https://wiki.mudlet.org/w/Manual:Lua_Fu ... raiseEvent.

Also curious to see what you use coroutine for - not many people are using them!

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Can I create a custom event?

Post by zhenzh »

I'd like to use it for simulate a wait line result.
Different from simple trigger, wait line needs pending the function flow until the trigger is fired. Coroutine.yield and coroutine.resume are used here to implement pending/goahead in a function and the event is planed to be used for monitoring the coroutine status and the result of trigger. It tells the function when its flow can be goahead.
For examle, compare to the below two functions:

Code: Select all

function A()
    return <create trigger>
end

funcion B()
    return <wait line>
end
function A will be returned immediately once the trigger is created, its return value only tells the result of trigger creation which is useless. We can not know whether the trigger has been fired at that time and what the matched line is.(for some regex trigger capturing values from matched line)

While function B will only be returned after the wait line is completed as the function will be pended from exiting by coroutine.yield until the trigger(created in wait line function) is successfully fired and all regex values are captured. In such case, we can even use the matched line or values as the return value of the function B to get a meanful return value.

We can also combine a set of simple wait line to construct a complex operation such as some jobs like:

Code: Select all

function job()
    if <wait line 1> == true then
        if <wait line 2> == true then
            ....
            return true
        end
    end
    return false
end
Invoking function can get the exact result of the whole job

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Can I create a custom event?

Post by Vadi »

That's neat. It'd be great if you could share a small package that does this, I think people would be interested in it.

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Can I create a custom event?

Post by zhenzh »

Sure.
It may need some time for investigation as I'm not familair with the event system.

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Can I create a custom event?

Post by zhenzh »

I have a general design for the function which should be look like:

Code: Select all

function wait_line(pattern, action)
    thread = coroutin.running()
    tempTrigger(pattern, [[wait_resume()]], 1)
    return coroutin.yield()
end

function wait_event()
    send(action) -- the action here is suposed to generate lines to be matched by trigger
end
registerAnonymousEventHandler("coroutin.yield", wait_event)

function wait_resume()
    return coroutin.resume(thread)
end
The fact is that I find the wait_event will not be auto risen by mudlet. May be I have some misunderstood about the event mechanism.
I expect that the event will be auto risen when some function is invoked once this function name is registered to an event handler. So that I can get the result flow as "invoking coroutin.yield -> rising event coroutin.yield -> invoking wait_event -> send pattern line -> trigger fired -> invoking wait_resume"

I have read some event examples from mudlet manual such as sysXXX event or GMCP event. Especially the GMCP, "registerAnonymousEventHandler("gmcp.Char.Items.Add", inventoryAdd)" seems each time the value of gmcp.Char.Items.Add changed, the event can be auto risen.

Is that also effective on any other customised variables? Can an event be risen at the point of time the specified variable chaning its value by registering this variable to some handler? Or the GMCP is just a special case that mudlet additionally hard coded variable changing monitoring section?

User avatar
demonnic
Posts: 884
Joined: Sat Dec 05, 2009 3:19 pm

Re: Can I create a custom event?

Post by demonnic »

you've maybe got it backward, when you register an event handler for an event, it calls that function when the event is raised, not the other way around. If you want the function to raise the event, use raiseEvent("MyEventName")

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Can I create a custom event?

Post by zhenzh »

I may understand why invoking function can not rise event. For all lua operation is considered as 3rd-party operation from which mudlet can not be informed.

I need think about some work around to get the event risen at the point of time coroutin.yield() run.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Can I create a custom event?

Post by Vadi »

Yeah GMCP is a special case because those variables are set by the game. So Mudlet does not monitor it, it receives them from the game and then notifies the user that they've changed.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Can I create a custom event?

Post by Vadi »

Invoking a function can raise an event if you do raiseEvent() inside it

Post Reply