A simple scheduler
This is another polling example, like ChangeActionBarAutomaticallyBasedOnTargetDistance. The two can coexist as-is just fine.
To schedule an action, put your code in a function and call: stkSchedule(<secondsFromNow>, <function>). Very simple but it works great. Remember: Blizzard does not allow you to cast spells, attack, or execute many of the other WoW API functions in a scheduled fashion like this--they must come from keyboard or mouse events (this is to prevent people from writing bots).
if (stkSchedulerQueue == nil) then
stkSchedulerQueue = {}
end
function stkPlayerFrameOnUpdate(elapsed)
if (GetTime() >= stkLastSchedulerCheck + .3) then
stkLastSchedulerCheck = GetTime()
stkCheckSchedulerQueue()
end
originalPlayerFrameOnUpdate(elapsed)
end
if (originalPlayerFrameOnUpdate == nil) then
-- only do this once
originalPlayerFrameOnUpdate = PlayerFrame_OnUpdate
stkLastSchedulerCheck = GetTime()
end
PlayerFrame_OnUpdate = stkPlayerFrameOnUpdate
function stkSchedule(secsFromNow, func)
local job = {
when = GetTime() + secsFromNow,
func = func
}
--p("scheduling job: now: "..GetTime()..", when: "..job.when)
table.insert(stkSchedulerQueue, job)
end
function stkCheckSchedulerQueue()
local i = 1
while i <= table.getn(stkSchedulerQueue) do
local job = stkSchedulerQueue[i]
if (job.when <= GetTime()) then
--p("executing job, now: "..GetTime()..", when: "..job.when)
job.func()
table.remove(stkSchedulerQueue, i)
else
i = i + 1
end
end
end