Function API
A Function file has one default export that receives ctx and args. This page uses JavaScript names. GDScript, Python and Lua use the same API with their own conventions (for example get(key, default), erase in GDScript, state_at and ctx.player:get in Lua); see GDScript, Python and Lua.
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. |
state(playerId) |
The player's newest realtime state, or undefined. |
stateAt(playerId, time) |
The player's realtime state at a server time within the last second, blended between updates. Use it to check hits. |
ctx.now
The server time in milliseconds when the call started.
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. GDScript, Python and Lua Functions are compiled to JavaScript on the server and run in the same engine with the same limits. JavaScript Functions get 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 |