Event manager

Additional botting resources. Addons may be either for the game itself or for the RoM bot.
Forum rules
Only post additional bot resources here. Please do not ask unrelated questions.
Post Reply
Message
Author
dx876234
Posts: 188
Joined: Sat Jul 24, 2010 6:13 am

Event manager

#1 Post 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
Attachments
userfunction_detector.lua
(4.1 KiB) Downloaded 175 times
userfunction_events.lua
(5.78 KiB) Downloaded 172 times
wtsiwtf
Posts: 9
Joined: Tue Jun 04, 2013 12:47 am
Location: Unknown

Re: Event manager

#2 Post 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.
User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Event manager

#3 Post 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.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan
wtsiwtf
Posts: 9
Joined: Tue Jun 04, 2013 12:47 am
Location: Unknown

Re: Event manager

#4 Post 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...
a polar bear is a rectangular bear after a coordinate transform
Post Reply