Wizards of Lua

The Art of Spell Crafting


EventQueue - Collecting Events

The EventQueue class collects events when it is connected to the event source.

The EventQueue class is a subtype of the Object class and inherits all its properties and functions.

Here is an overview of the EventQueue properties:

Property Type read / write
names table r

Here is an overview of the EventQueue functions:

Function Parameters Results
 isEmpty  boolean
 latest  Event
 next  timeout Event
 stop  nil

Properties

Below you find short descriptions about each of the properties and some examples about how to use them in your spells.


names : table

These are the names of all events this queue is collecting.


Functions

Below you find short descriptions about each of the functions and some examples about how to use them in your spells.


isEmpty () -> boolean

The ‘isEmpty’ function returns true if this queue is empty, false otherwise.

Example

Busy-waiting for a chat event and printing the message when it occurs.

local queue = Events.collect("ChatEvent")
while queue:isEmpty() do
  sleep(20)
  print("still waiting...")
end
local event = queue:next(0)
print("You said "..event.message)

latest () -> Event

The ‘latest’ function returns the newest event in this queue and discards all older events. If the queue is empty then nil is returned. This is useful for update events where you are only interested in the most recent change.

Example

Echo the last chat message every 5 seconds.

local queue = Events.collect("ChatEvent")
while true do
  local event = queue:latest()
  if event ~= nil then
    spell:execute("say %s", event.message)
  end
  sleep(5 * 20)
end

next (timeout) -> Event

The ‘next’ function returns the next event in this queue, if any. This function blocks until an event is available or the given timeout (measured in game ticks) is reached. If no timeout is specified, this function blocks forever.

Example

Echoing all chat messages.

local queue = Events.collect("ChatEvent")
while true do
  local event = queue:next()
  spell:execute("say %s", event.message)
end

stop () -> nil

The ‘stop’ function stops collecting events into this queue.

Example

Collecting chat events and stopping it after the first event occurs.

local queue = Events.collect("ChatEvent")
local event = queue:next()
print(str(event))
queue:stop()