Functions called by GameServer
GameServer calls the following functions (defined in Data\LuaAPI\System\Timers.lua) according to the server's clock:
| Function | When |
|---|---|
onTimerProcessSecond() | Every second. |
onTimerProcessMinute() | At the start of a new minute. |
onTimerProcessHour() | At the start of a new hour. |
onTimerProcessDayChange() | When a new day begins (00:00). |
Right after GameServer starts, it only records the current time marks; the minute / hour / day functions are called only when the mark actually changes, not immediately at startup.
The onTimerProcessSecond function in Timers.lua does 3 things in order: (1) runs the modules attached to OnTimerThread; (2) runs Timer.Process() (the timers below); (3) runs Scheduler.Process (the event schedule, see Ready-made library) inside pcall so that an event schedule error does not stop the timers.
onTimerProcessSecond forwards to modules (OnTimerThread). The three minute / hour / day functions in Timers.lua are empty functions: write code directly in them, or add BridgeDispatch("onTimerProcessMinute") to the function. Attaching with BridgeFunctionAttach without editing these functions will not run.Timer.* (schedule work to run after N seconds)
The unit is seconds. Time is rounded down to an integer, with a minimum of 1 second. Each timer has a name (string); creating a new timer with the same name replaces the old one.
| Function | Effect | Returns |
|---|---|---|
Timer.Create(seconds, name, func) | Runs func once after seconds seconds. | true; false (and writes a log) if func is not a function |
Timer.CreateRepeating(seconds, name, func) | Runs func repeatedly every seconds seconds, without stopping. | as above |
Timer.RepeatNTimes(seconds, name, count, func) | Runs func exactly count times, with seconds seconds between runs. The function receives the parameter n = the run number (1, 2, 3...). | as above |
Timer.Remove(name) | Removes the timer. | none |
Timer.Exists(name) | Whether the timer exists. | true / false |
Timer.GetRemaining(name) | Seconds remaining until the next run. | number of seconds; 0 if none |
Timer.Stop(name) | Pauses (keeping the remaining time). | true; false if there is no such timer |
Timer.Start(name) | Resumes a paused timer. | true; false if none |
Timer.IsActive(name) | Whether the timer exists and is not paused. | true / false |
-- Notice after 5 seconds
Timer.Create(5, "Notice", function()
Log.Add("5 seconds have passed")
end)
-- Give a reward once every 60 seconds
Timer.CreateRepeating(60, "Reward", function()
-- ...
end)
-- Count down 10 times, 1 second apart
Timer.RepeatNTimes(1, "Countdown", 10, function(n)
Log.Add("Run " .. n .. " / 10")
end)
Notes
- Timers run on GameServer's main thread. A timer function that runs longer than 5 seconds in a single call is interrupted (see Error handling and safety notes).
- If a function in a timer errors, the log
[Timer] name: error messageis written; other timers still run. A one-shot timer /RepeatNTimesis removed before the function runs, so it does not run again even if the function errors. - Timers are not saved: reloading LuaAPI or restarting GameServer loses them all. If you need repeating work, recreate it in
onGameServerStart(or attachOnReadScript). - If several timers come due within the same second, they run one after another (order is not guaranteed).