Async / waiting

Do stuff later without blocking

Summary

Published: Feb 18 2024 by mana vortex Last documented edit: Feb 18 2024 by mana vortex

Where to get cron

You can get Cron.lua from psiberx's cp2077-cet-kit on github. Add it to your sources, and then define it as a global:

-- don't overwrite it if it's already defined in the global scope. Or do, up to you.
Cron = Cron or require('External/Cron.lua')

How to use cron

Cron offers a bunch of functions for executing code later. The code you want to execute has to be inside a function (=> callback) and is passed among the arguments.

For periodically running functions, you need to include logic when to stop the execution.

Performance impact

To save processor load, it is best practice to define your callback outside of the function call. This can have a noticable performance impact, so you're gonna want to do this.

Good example:

local callback = function()
    -- do a little dance, make a little love
end
Cron.Every(0.1, callback)

The callback is defined once.

Bad example:

The callback is defined every time cron runs.

Passing function arguments

To pass arguments to your callback function, you pass them as arguments to cron:

Cron functions

Delay

Execute the callback after X seconds

Timer

Executes a function every X seconds.

Pausing and resuming a timer

You can pause a timer by passing its reference to cron:

Last updated