-
Notifications
You must be signed in to change notification settings - Fork 0
spawn
ffredyk edited this page Jul 23, 2026
·
1 revision
Code Execution
Unary or Binary.
// Unary: no arguments
spawn { code }
// Binary: with arguments
<args> spawn { code }
Executes a code block asynchronously in a new fiber on the current scheduler. Returns a ScriptHandle immediately — the code runs concurrently. The calling fiber continues without waiting.
Unlike call, spawn-executed code can suspend — sleep and await work inside spawned code.
This is a compiler-level construct. The compiler:
- Creates a new fiber on the current scheduler.
- Copies the arguments (if binary) into the fiber's context.
- Starts executing the code block.
- Returns a
ScriptHandleto the caller.
The handle can be used to await completion, check status, or terminate the spawned code.
_handle = spawn {
sleep 5;
systemChat "5 seconds passed";
};
// code continues immediately — does NOT wait for 5 seconds[100, "player"] spawn {
params ["_hp", "_name"];
sleep 2;
systemChat f"{_name} has {_hp} HP";
};_handle = spawn { sleep 3; return "done"; };
_result = await _handle; // waits for handle, returns "done"spawn { diag_log "background task"; }; // ignore handle_handles = [];
_handles pushBack (spawn { task1(); });
_handles pushBack (spawn { task2(); });
_handles pushBack (spawn { task3(); });
// Wait for all
{ await _x } forEach _handles;
systemChat "All tasks complete";The spawned code's return value is stored in the ScriptHandle. Retrieve with await:
_handle = spawn { sleep 1; return 42; };
_val = await _handle; // 42Creates fiber on the current scheduler. The handle is owned by the spawning scheduler. Cross-scheduler spawn: use spawnOn.
- Getting Started
- Language Guide
- Type System
- Control Flow
- Functions & Code
- Strings & Text
- Arrays & Collections
- Concurrency
- Promise System
- Thread Safety
- Host API & Embedding
- CLI Tool
- Bytecode Reference
- Multiplayer
- Syntax Sugar
- Optimization Guide
- Benchmarks
- count
- select
- pushBack
- append
- deleteAt
- deleteRange
- resize
- reverse
- sort
- find
- in
- forEach
- freeze
- thaw
- isFrozen
- currentScheduler
- clientOwner
- allSchedulers
- schedulerName
- schedulerExists
- schedulerBudget
- setSchedulerBudget
- fiberCount
- readyFiberCount
- waitingFiberCount
- schedulerLoad
- sendTo