19. Dates, time and timed jobs
Use wait for a pause in a sequence. Use a small waiting loop for a countdown. Use every for a detached repeated job. Date/time helpers are for the computer’s calendar and clock, not a game-loop stopwatch.
global countdown_running = false
global seconds_left = 0
function run()
if countdown_running
return
end
countdown_running = true
seconds_left = 10
while countdown_running and seconds_left > 0
wait 1 second
if countdown_running
seconds_left -= 1
end
end
countdown_running = false
endSet countdown_running to false from another script to stop future ticks. A waiting loop counts its requested waits and can drift slightly under load. A saved countdown should save a deliberate game value and have a clear restart policy after load.
global pulse_active = true
function run()
every 1 second
if not pulse_active
break
end
log("tick")
end
endAn every block starts a separate periodic job. Its body runs once immediately, then waits the interval before the next pass. It does not hold up the following instruction in run. There is no named timer handle builtin. Guard against starting duplicates, and stop or deactivate the job when its owner no longer needs it. Script/cache invalidation cancels its execution generation.
now_unix() returns whole Unix seconds. date_time_now() returns a calendar dictionary.
date_time_string() returns a display string. A system-clock adjustment can move wall-clock time, so avoid using it as an unquestioned physics timer.
