Job System
Coroutine-based job system for running background tasks without blocking the game. Jobs yield control back to the engine periodically, allowing frame rendering to continue during long operations.
Coroutine-based job system for running background tasks without blocking the game. Jobs yield control back to the engine periodically, allowing frame rendering to continue during long operations.
Public Functions
| Function | Signature | Description |
|---|---|---|
M.create | (fct, maxdt, ...) → job | Creates and starts a new coroutine job |
M.wrap | (fct, maxdt) → wrappedFn | Returns a function that creates a job when called (prevents duplicate runs) |
M.getRunningJobCount | () → number | Returns the number of currently active jobs |
M.onUpdate | (dtReal, dtSim, dtRaw) | Resumes all active coroutines each frame |
Job Object
The job object is passed as the first argument to the work function:
| Method | Description |
|---|---|
job.yield() | Yields if maxdt seconds have elapsed since last yield |
job.sleep(secs) | Pauses execution for the given number of seconds |
job.setExitCallback(fct) | Registers a callback invoked when the job completes |
job.running | Boolean - true while the job is active |
Usage Example
-- Simple background task
local function myTask(job, arg1, arg2)
for i = 1, 100 do
-- Do work...
job.yield() -- Give the game time to render
end
job.sleep(2) -- Wait 2 seconds
-- More work...
end
-- Start with 1 second max between yields (good for background tasks)
extensions.core_jobsystem.create(myTask, 1, "hello", 42)
-- Wrapped version (prevents duplicate runs)
local doCheck = extensions.core_jobsystem.wrap(function(job)
-- Long operation...
job.yield()
end, 0.5)
doCheck() -- First call starts the job
doCheck() -- Ignored if already runningParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
fct | function | required | The work function to run as a coroutine |
maxdt | number | 0.01 | Max seconds between automatic yields (controls responsiveness) |
... | any | - | Additional arguments passed to fct |
Hooks
| Hook | Trigger |
|---|---|
onUpdate | Every frame - resumes all active coroutines |
onJobDone | Fired via extensions.hook when a job completes |
Notes
- If
disableBackgroundTasksis true, jobs run synchronously (foreground mode). wrapprevents the same function from running concurrently - a second call is silently ignored.- On error, the job logs the error with traceback and is removed.
- The
onUpdatehook passes{dtReal, dtSim, dtRaw}as the second argument when no custom args exist. - Used extensively by
core_modmanager.initDBandcore_repository.checkUpdate.
See Also
- modmanager - Uses jobsystem for background mod initialization
- globals - Extension lifecycle hooks
Inventory
Simple key-value inventory system for tracking items (vehicles, parts, currency) during campaigns/scenarios. Supports adding, removing, and querying items by type, with campaign save/resume hooks.
Lap Times
Lap and segment timing system for races. Tracks current time, lap/segment splits, best times, and diffs. Streams formatted timing data to the UI via `guihooks.queueStream`.