Skip to reading
PCA Script Programming

19. Dates, time and timed jobs

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page

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.

Code example
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
end

Set 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.

Code example
global pulse_active = true
function run()
    every 1 second
        if not pulse_active
            break
        end
        log("tick")
    end
end

An 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.

Page 26 of 357Permalink
Go to page