GDScript Functions
If your game is built in Godot, you can write your server rules in the language you already use. Pick GDScript in the language menu of the Functions tab. NetStack compiles your script on the server and runs it in the same sandbox as JavaScript, with the same limits and the same all-or-nothing behaviour.
Anatomy
Define a function called run. It receives ctx and args, and whatever it returns is sent back to the caller.
func run(ctx, args):
return { "ok": true }
argsis a Dictionary with whatever your game passed tocall(). Read it withargs.itemId,args["itemId"]orargs.get("itemId", default).- Return any value that converts to JSON: Dictionaries, Arrays, Strings, numbers,
true/falseandnull. Returning nothing sendsnull. extendsandclass_namelines are allowed and ignored, so you can paste a script straight from your project.- Type hints (
var gold: int = 0,func run(ctx, args) -> Dictionary:) are accepted. They are not checked at runtime.
You can add top-level const, var, enum and helper func declarations in the same file:
const MAX_STACK = 99
enum Rarity { COMMON, RARE, EPIC }
func clamp_stack(n):
return clamp(n, 0, MAX_STACK)
func run(ctx, args):
var count = ctx.player.get("potions", 0)
ctx.player.set("potions", clamp_stack(count + 1))
return { "potions": clamp_stack(count + 1), "rarity": Rarity.RARE }
The API in GDScript
The API is the same as in JavaScript, with Godot-style names:
| GDScript | What it does |
|---|---|
ctx.player.id |
ID of the calling player. |
ctx.player.get(key, default) |
Read a Player Property. Returns default (or null) when it is missing. |
ctx.player.set(key, value) |
Write a Player Property. |
ctx.player.erase(key) |
Remove a Player Property. |
ctx.player.has(key), ctx.player.keys() |
Check for a property, list property names. |
ctx.players |
Array of everyone in the room. Each has id, get, set, erase, has and keys. |
ctx.room.get / set / erase / has / keys |
Room Properties. |
ctx.room.broadcast(event, data) |
Send an event to every player in the room. |
ctx.room.state(player_id), ctx.room.state_at(player_id, time) |
A player's realtime state, now or at a recent server time. |
ctx.global.get / set / erase / has / keys |
Global Properties shared by every room. |
ctx.now |
Server time in milliseconds when the call started. |
print(...), ctx.log(...) |
Write a line that shows up in test runs. |
fail(code, message) |
Reject the call. The caller receives the code and message. |
get() returns a copy. Change the copy, then set() it back:
func run(ctx, args):
var inventory = ctx.player.get("inventory", [])
if not inventory.has(args.itemId):
fail("ITEM_NOT_OWNED", "You do not own that item.")
inventory.erase(args.itemId)
ctx.player.set("inventory", inventory)
ctx.room.broadcast("itemUsed", { "playerId": ctx.player.id, "itemId": args.itemId })
return { "remaining": inventory.size() }
Rejecting a call
Call fail(code, message). It stops the Function, the caller receives the code and message, and none of the changes made so far are kept. Any other runtime error (for example indexing null) is reported as FUNCTION_ERROR with the line number, which you can see in the Test run panel.
func run(ctx, args):
if typeof(args.get("amount")) != TYPE_INT or args.amount <= 0 or args.amount > 100:
fail("BAD_AMOUNT", "Amount must be between 1 and 100.")
var gold = ctx.player.get("gold", 0)
ctx.player.set("gold", gold + args.amount)
return { "gold": gold + args.amount }
Working with other players
func run(ctx, args):
var target = null
for p in ctx.players:
if p.id == args.toPlayerId:
target = p
if target == null:
fail("PLAYER_NOT_IN_ROOM", "That player is not here.")
target.set("gold", target.get("gold", 0) + 5)
return { "ok": true }
What is supported
Most everyday GDScript works:
var,const,enum,func, lambdas (func(x): return x * 2) and.call().if/elif/else,forover arrays, dictionaries andrange(),while,break,continue,match(includingvarbindings,whenguards and_).- Arrays and Dictionaries with their common methods (
append,erase,has,find,sort,size,keys,values,merge, and more),d.keyaccess on Dictionaries. - Strings with their common methods,
%formatting ("%s has %d hp" % [name, hp]) andstr(). - Math helpers such as
clamp,min,max,abs,floor,round,randi,randf_range,lerp, plusPI,TAU,INF. Vector2andVector3with arithmetic,length(),normalized(),distance_to()anddot().
Because there is no scene tree on the server, nodes, $Node paths, signals, resources (load, preload), inner classes and super are not available. You get a clear error with a line number if you use one. Use ctx.room.broadcast() where you would emit a signal.
Numbers
All numbers are 64-bit floating point on the server, like JSON. Whole numbers still count as int for typeof() and is int, but one thing differs from Godot: 7 / 2 is 3.5, not 3. Use int(7 / 2) or floori(7 / 2) when you need whole-number division. Properties are stored as JSON, so 2.0 comes back as 2.
Limits
The same as every Function: 150 ms of CPU and 32 MB of memory per call, no network, filesystem or timers, and up to 20,000 characters of source. See Limits and plans.