Repository navigation
LuaAPI Timers
In System\Timers.lua:
| Function | Called when |
|---|---|
onTimerProcessSecond() |
Every second. |
onTimerProcessMinute() |
When a new minute starts. |
onTimerProcessHour() |
When a new hour starts. |
onTimerProcessDayChange() |
When a new day starts (00:00). |
Unit: seconds (whole seconds, at least 1). Creating a timer with an existing name replaces the old one. An error in one timer is written to the log and does not affect the others. Timers are cleared when LuaAPI is reloaded.
The callback receives n, the number of times the timer has run so far (1, 2, 3 ...). If the callback is not a function, the timer is not created, the error is logged and the function returns false.
Daily / weekly events from an XML schedule: see Scheduler.
Timer.Create(seconds, name, function() ... end) → boolean
Runs the function once after seconds, then the timer is removed.
Message.SendAll(0, "The server will save in 30 seconds")
Timer.Create(30, "SaveNotice", function()
Message.SendAll(0, "Saved. Thank you for waiting")
end)Timer.CreateRepeating(seconds, name, function(n) ... end) → boolean
Runs the function every seconds until the timer is removed.
-- every 10 minutes: a reminder for everyone
Timer.CreateRepeating(600, "Reminder", function(n)
Message.SendAll(0, "Remember to vote for the server! (" .. n .. ")")
end)Timer.RepeatNTimes(seconds, name, times, function(n) ... end) → boolean
Runs the function every seconds, times times (n = 1..times), then the timer is removed.
-- 10 second countdown
Timer.RepeatNTimes(1, "Countdown", 10, function(n)
Message.SendAll(0, "The event starts in " .. (11 - n) .. " seconds")
end)Timer.Remove(name)
Removes the timer. Nothing happens if it does not exist.
function Invasion_End()
Timer.Remove("InvasionWave")
Message.SendAll(0, "The invasion is over")
endTimer.Exists(name) → boolean
true if the timer still exists (paused timers included).
if Timer.Exists("Countdown") then
Message.Send(aIndex, 1, "The countdown is already running")
return 1
endTimer.GetRemaining(name) → number
Seconds until the next run, 0 if the timer does not exist.
if Timer.Exists("SaveNotice") then
Message.Send(aIndex, 1, "Save in " .. Timer.GetRemaining("SaveNotice") .. " seconds")
endTimer.Stop(name) → boolean
Pauses the timer (it keeps its remaining time). Returns false if the timer does not exist.
if Timer.Stop("InvasionWave") then
Message.SendAll(0, "The invasion is paused")
endTimer.Start(name) → boolean
Resumes a paused timer. Returns false if the timer does not exist.
if Timer.Start("InvasionWave") then
Message.SendAll(0, "The invasion goes on in " .. Timer.GetRemaining("InvasionWave") .. " seconds")
endTimer.IsActive(name) → boolean
true if the timer exists and is not paused.
if Timer.Exists("InvasionWave") and not Timer.IsActive("InvasionWave") then
Message.Send(aIndex, 1, "The invasion is paused")
endNext: Safety and languages