-
Notifications
You must be signed in to change notification settings - Fork 435
API Event
Vexatos edited this page Jul 13, 2014
·
6 revisions
For those that don't like images: the wiki has moved to a new place, http://ocdoc.cil.li/.
This wiki will no longer be updated.
This API provides a rudimentary event system. It is primarily intended to be used by libraries, to allow them to be notified of signals. For example, this is used to automatically bind the primary screen to the primary GPU, to check whether the terminal is available, and to execute autorun programs on newly installed file systems.
-
event.listen(name: string, callback: function): boolean
Register a new event listener that should be called for events with the specified name. When registering for signal events, this is the name of the signal. Returnstrue
if the listener was successfully registered,false
if it already was registered for this event type.
Note that event listeners may returnfalse
to unregister themselves (equivalent to callingevent.ignore
and passing the listener with the event name it was registered for). -
event.ignore(name: string, callback: function): boolean
Unregister a previously registered event listener. Returnstrue
if the event listener was removed,false
if the listener was not registered. -
event.timer(interval: number, callback: function[, times: number]): number
Starts a new timer that will be called after the time specified ininterval
. Per default, timers only fire once. Passtimes
with a value larger than one to have it fire as often as that number specifies. Passmath.huge
to create an infinitely repeating interval.
This returns a number that identifies the created timer, and can be used intimer.cancel
to destroy it, possibly before it even ran once.
Note: the timer resolution can vary. If the computer is idle and enters sleep mode, it will only be woken in a game tick, so the time the callback is called may be up to 0.05 seconds off.
Important: timers are driven by theevent.pull
function. If you always pull signals directly fromos.pullSignal
and never callevent.pull
, timers will not work! -
event.cancel(timerId: function): boolean
Cancels a timer previously created withevent.timer
. Returnstrue
if the timer was stopped,false
if there was no timer with the specified ID. -
event.pull([timeout: number], [name: string], ...): string, ...
This, besidesos.sleep()
should be the primary way for programs to "yield". This function can be used to await signals from the queue, while having unrelated signals dispatched as events. It also drives timers created withevent.timer
, so it should be called regularly anyway.
If the first parameter is a number, it is expected to be the time to wait for the signal, before returningnil
, the second parameter then has to be the event name. Otherwise the first parameter has to be the event name. The event name can benil
, however, to not filter for a specific event.
Any further parameters are used as a direct filter. The function will only return events that match this filter - meaning the parameters are equal to those provided in the filter (direct equality for all parameters, regular expressions are possible for the event name, seestring.match
).
For example, theclick
signal (when a player clicks on a tier two or three screen) has the signaturescreenX: number, screenY: number, playerName: string
. To only pull clicks by player "Steve" you'd do:local _, x, y = event.pull("click", _, _, "Steve")
-
event.shouldInterrupt(): boolean
This function is called byevent.pull
after each signal was processed, to check whether it should abort early. If this returnstrue
,event.pull
will throw aninterrupted
error.
Per default, this returnstrue
if the combination Ctrl+Alt+C is pressed. -
event.onError(message: any)
Global event callback error handler. If an event listener throws an error, we handle it in this function to avoid it bubbling into unrelated code (that only triggered the execution by callingevent.pull
). Per default, this logs errors into a file on the temporary file system.
This example will check if the computer has been running for over 300 seconds every 30 seconds, and if it has, it reboots it:
local computer = require("computer")
local event = require("event")
function onTimer()
computer.shutdown(true)
end
event.timer(30, onTimer, 50) --run 50 times, then stop. If it's still not past 300 seconds by then, we've got other things to worry about.