tibia-client/modules/core_scripts/dispatcher.lua

34 lines
659 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)
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