tibia-client/modules/core_lib/dispatcher.lua

35 lines
689 B
Lua
Raw Normal View History

2011-11-07 01:14:51 +01:00
local eventId = 0
2011-12-07 01:31:55 +01:00
local eventList = {}
function scheduleEvent(func, delay)
2012-01-06 09:48:59 +01:00
if not func then return end
2011-11-07 01:14:51 +01:00
eventId = eventId + 1
local id = eventId
local function proxyFunc()
2011-12-07 01:31:55 +01:00
if eventList[id] then
if eventList[id].active then
func()
end
eventList[id] = nil
2011-11-07 18:20:13 +01:00
end
2011-11-07 01:14:51 +01:00
end
2011-12-07 01:31:55 +01:00
eventList[id] = { func = proxyFunc, active = true }
if delay and delay > 0 then
g_dispatcher.scheduleEvent(proxyFunc, delay)
else
g_dispatcher.addEvent(proxyFunc, false)
end
2011-11-07 01:14:51 +01:00
return id
end
2011-12-07 01:31:55 +01:00
function addEvent(func)
return scheduleEvent(func, 0)
end
2011-11-07 01:14:51 +01:00
function removeEvent(id)
2011-12-07 01:31:55 +01:00
if id and eventList[id] then
eventList[id].active = false
2011-11-07 18:20:13 +01:00
return true
2011-11-07 01:14:51 +01:00
end
end