Page 1 of 1

Event manager

Posted: Wed Apr 17, 2013 7:41 am
by dx876234
I dont know if its any interest but due to some discussions w. admin/lisa I've made an event manager as a userfunction.
It replaces the static event management in RoM with a subscription based one which enables multiple callbacks.
  • Subscription on micromacro and user events
  • Multiple priority based callbacks per event
  • Integrates into RoMBot, subscribes on systems events for all original callback handlers
Now, why?

Well I started off making detectors, like "is there any players close by" or "has boss spawned" and discovered I had code stashed away in multiple userfuntions which wanted to check errors and execute stuff at exit of scripts.

So, solution is this event manager, I can register a callback on specific events independently in userfunctions without messing up other userfunctions.

Example is detection that other players are close by, if I detect a player around I might want to do some actions, if its all clear again I want other actions, if I suspend a script I want the character to sit down, when I activate it it should stand up and Yell "Jipiii"...or someting, and when script terminates due to error ot normally I would like to have character sit down again. (just silly samples...)

Using the event manager is somewhat simple:

Code: Select all

function myCallback(event)
     if event.name == "USER_EVENT_SAMPLE" then
       print("Something")
   end
end

Events:define("USER_EVENT_SAMPLE")
Events:register("USER_EVENT_SAMPLE", myCallback)
.
.
Events:throw("USER_EVENT_SAMPLE")
Using this I made a userfunction to detect if players are close and fire off an event, to use in your waypoint:

Code: Select all

-- range: how far away we should detect players (max for client is typically 5-600)
-- cooldown: how long should we wait after all players have gone until we get a all clear event
PlayerDetection(range, cooldown)
The included event handler only prints out some text on console, to do anything else you need to register your own event handler. Ex:

Code: Select all

function handleEvent(event)
end

Events:register(USER_PLAYERCLOSE, handleEvent)
Events:register(USER_PLAYERGONE,  handleEvent)
The predefined events from micromacro are:

Code: Select all

SYS_EXIT         - at tertmination of script
SYS_RESUME    - at resume after a pause
SYS_SUSPEND  - at suspension of script
SYS_ERROR      - at errors in script
Hope this is usefull for some1

-dx

Re: Event manager

Posted: Tue Jun 04, 2013 1:25 am
by wtsiwtf
I'm wondering if any of the authors of rombot would be interested in altering IGF to catch all API events and raise them in the context of rombot. This makes the event programming model synchronous at the expense of being slightly resource intensive, however I'm unsure if Lua's thread-agnosticness will attract side effects.

Re: Event manager

Posted: Tue Jun 04, 2013 3:47 am
by rock5
Capturing all events is impractical especially as most users wont use them. Communication between client and bot needs to be kept to a minimum because it's slow, relatively speaking.

That said, igf does keep the last of every alert and warning message so you can always retrieve those without needing to set up an event monitor or anything. You call them with

Code: Select all

getLastWarning(message, age)
getLastAlert(message, age)
"message" is the message you are looking for and "age" is an optional argument to limit the accepted age of the message in seconds.

Re: Event manager

Posted: Tue Jun 04, 2013 8:09 am
by wtsiwtf

Code: Select all

getLastWarning(message, age)
getLastAlert(message, age)
Thanks rock! this is a good solution for my requirements.
As for the event raising is concerned, it can be made opt-in and/or filtered at OnLoad or custom IGF event. The need for this is to streamline certain player and inventory functions and reducing polling and yrest()s. I know it is going to be more resource intensive but this has its benefits in short-term bot activities where timing is more important than longevity.
Just thoughts and I can already see its downsides apart from Lua's limitations...