Function API
A Function file has one default export that receives ctx and args.
export default async function (ctx, args) {
// ...
}
ctx.player
The player who made the call.
| Member | Description |
|---|---|
id |
The player's ID. |
get(key) |
Read a property. Returns a copy, or undefined. |
set(key, value) |
Write a property. |
delete(key) |
Remove a property. |
has(key) |
Whether the property exists. |
keys() |
List all property names. |
ctx.players
An array of everyone in the room, including the caller. Each entry has id, get, set, delete, has and keys, with the same behaviour as ctx.player.
ctx.room
| Member | Description |
|---|---|
id |
The room's ID. |
get, set, delete, has, keys |
Room properties. |
broadcast(event, data) |
Send an event to every player in the room. |
ctx.global
get, set, delete, has and keys for project-wide properties.
ctx.log(...values)
Records values that appear in test runs. Strings are logged as-is and other values as JSON. Up to 50 lines of 500 characters are kept.
NetStackError
throw new NetStackError("CODE", "Message for the player.");
The code and message are returned to the caller and all pending changes are discarded.
Return value
Return any JSON value. Returning nothing sends null. The serialized result can be up to 32 KB.
The sandbox
Functions run in an isolated JavaScript engine with modern language features such as async/await, destructuring, classes and Map/Set. There is no network, filesystem, process, require, import, fetch or timers. Math.random() and Date.now() are available.
Limits
| Limit | Value |
|---|---|
| CPU time per call | 150 ms |
| Memory per call | 32 MB |
| Property value size | 64 KB |
| Property writes per call | 100 |
| Broadcasts per call | 50 |
| Broadcast data size | 16 KB |
| Function source size | 20,000 characters |
| Functions per project | 50 |