Skip to content

timer functions

IS4 edited this page Jun 27, 2021 · 4 revisions

ms(func, interval, ...)

Registers a function to be called after interval milliseconds. The function will be called in the main Lua thread. If additional arguments are provided, they will be passed to the function.

tick(func, interval, ...)

Registers a function to be called after interval server ticks. The function will be called in the main Lua thread. If additional arguments are provided, they will be passed to the function.

new(func, scheduler, ...)

Creates a new timer object using scheduler (e.g. timer.ms or timer.tick), as if scheduler(func, ...) had been called. Returns a table with functions elapsed, cancel and cancelled. If cancel is called on the result, func will not be executed at all.

parallel([count, ] func, ...)

Calls func(...) but every count (default 100000) Lua VM instructions yields function(cont)tick(cont, 1)end from the Lua thread. If this is called inside async, it will register a function that resumes the coroutine. This creates the effect of running the code in parallel with the main thread; if the code takes too long to execute, it will be paused and scheduled on the next tick.

Non-Lua code cannot be interrupted this way, but most native functions in YALP can handle calling functions that may yield.

parallelex(count, regfunc, func, ...)

Like parallel, but it yields regfunc.

sleep(ms)

Performs a blocking sleep on the current system thread for the given number of milliseconds. The sleep is active, i.e. it contains a loop that checks if the duration has elapsed or not. However, the loop is actually performed in Lua code, so it can be interrupted by hooks or paused via parallel.

This function should only be used as a substitute for CPU-intensive code, to test async and parallel. Use wait to pause the script asynchronously.

wait(ms, ...)

Analogous to coroutine.yield(timer.ms, ms, ...). Usually, this function should be called inside async, which handles the yield and registers the continuation, making the wait asynchronous.

waitticks(ticks, ...)

Analogous to coroutine.yield(timer.tick, ticks, ...) with semantics similar to wait.

timeout(ms, func, ...)

Calls func(...) but interrupts it if it takes longer than ms milliseconds. The function must be executing Lua code in order to be interrupted. If there is a call to a C function that doesn't execute any Lua code in the same thread, or coroutine.yield, the function will be interrupted after it ends. func may be a coroutine, in which case it is resumed with ....

This function creates a new system thread that waits for the specified time and then asynchronously sets a count 1 hook in the Lua thread. The hook simply raises an error on the next Lua instruction. Use of pcall does intercept the error, but the hook is still active, and so the code that follows the call to pcall again invokes the hook, until the execution returns back to timeout, where the hook is unregistered.

The hook is only effective in the same Lua thread (if func is not a coroutine) that called timeout, and so calls to coroutine.resume will bypass it. You can use coroutine.resumehooked to resume a coroutine in a way that is aware of the parent coroutine's hooks.