Games and multiplayer
Script the game, and the server it lives on.
Embed SCUA in a C or C++ engine for gameplay logic, the way you would embed Lua. Then run the same language on the server for rooms, matches and player sessions, where its state can be saved, moved and put to sleep without you writing the code to do it.
-- One frame of a turret: aim, check range, check cover, then fire.
let turret = vec3(0, 1, 0)
let rock = sphere(vec3(4, 1, 3), 1.5) -- cover on the field
fn decide(target)
let to_target = target - turret
if to_target.length() > 12 then return "out of range" end
let aim = ray(turret, to_target.normalize())
if intersect(aim, rock) then return "blocked by the rock" end
return "fire"
end
for target in [vec3(8, 1, 6), vec3(2, 1, 9), vec3(30, 1, 0)] do
print(`{target} {decide(target)}`)
end
$ scua turret.scua
vec3(8, 1, 6) blocked by the rock
vec3(2, 1, 9) fire
vec3(30, 1, 0) out of rangeInside your engine
Gameplay code, driven by your frame loop
SCUA embeds through one C header, and the host stays in charge of time. Vectors and shapes are part of the language, as in the sample above.
Your loop drives the scripts
The host loads a script, calls into it each frame and runs pending turns when it chooses. Host functions say which thread they may be called from.
Drive scripts from an engine →Game maths is built in
Vectors, matrices, quaternions and colours are value types, with shapes such as rays, spheres and planes. intersect(ray, sphere) needs no library.
Entity loops compile
Typed arrays such as { f32 } and { vec2 } are stored unboxed, and the JIT compiles the loops over them.
Saves that survive patches
A typed save is checked all the way down when it loads, and upgraded by the migrate hooks you wrote for older versions.
Save the player's progress →UDP for real-time play
Datagrams are a capability like any other, granted by address, with a batch receive for servers that take many packets a tick.
Send datagrams with UDP →Plurals for about 100 languages
Message formatting with the plural rules of about 100 languages, so "1 item" and "3 items" come out right in each.
Render and localization →Multiplayer state
Each client is sent only the fields it may see
An online game keeps the real state on the server and sends each client a cut-down view. The usual way is a hand-written function per message that copies the safe fields, and a leak the day somebody adds a field and forgets one of them.
In SCUA the rule sits on the field. You mark who may see it, and project builds
the view for an audience. A field the audience may not see is not in the bytes you send, and
the rule reaches into nested records and lists.
enum Audience { Server, Client, Spectator }
gate Owner { Audience.Server, Audience.Client }
gate Internal { Audience.Server }
gate Visible { Audience.Server, Audience.Client, Audience.Spectator }
record Player {
name: string, -- everyone sees it
health: int gated Visible,
inventory: { int } gated Owner, -- the owning client and the server
bonus_timer: int gated Internal, -- never leaves the server
}
let aria: Player = { name = "Aria", health = 80,
inventory = [3, 7, 12], bonus_timer = 4213 }
let mine = project(aria, Player, Audience.Client)
let other = project(aria, Player, Audience.Spectator)
print(`owner sees {mine.health} {mine.inventory} {mine.bonus_timer}`)
print(`spectator sees {other.health} {other.inventory} {other.bonus_timer}`)
$ scua project.scua
owner sees 80 [3, 7, 12] nil
spectator sees 80 nil nilRooms, matches, players
Rooms and matches that never need a lock
A partition owns its state and handles one message at a time, so a room, a match or a shop
never needs a lock. tell sends a message and moves on. ask waits
for the reply, with a timeout.
Partitions run on several cores by default. A new one costs about 2.3 KB, and one that has handled a message about 12 KB, so a server can hold a great many rooms that are mostly waiting.
Partitions and actors →partition Room
state scores = {}
on Join(name)
scores[name] = 0
end
on Hit(name, points)
scores[name] = scores[name] + points
end
on Score(name)
reply(scores[name])
end
end
let room = Room()
tell room.Join("ana")
tell room.Hit("ana", 30)
tell room.Hit("ana", 12)
print(ask room.Score("ana") timeout 1s)
$ scua room.scua
42Live sessions
Move a live player to another server
A player's session is a block of bytes that loads at any address. To rebalance, or to drain a machine for a deploy, the host freezes the session between two messages, sends the bytes and wakes it on the other side, where the same script is loaded. A fencing step in your store makes sure a stale copy can never overwrite the session.
The same mechanism puts an idle player to sleep in object storage. A save that must still load after you change the game is a separate, typed format: a durable save carries a version, and migrate hooks upgrade old ones as they load.
Freeze, claim and wake a session →Mods and player-made content
Run scripts your players wrote
sandbox.actor compiles a string of source into an actor that holds no grant
at all. It can compute and answer messages. It cannot read a file, open a
socket or read the clock.
Each sandbox has a budget of operations per turn and a memory cap. A script that loops forever is stopped, the game gets an error value naming the reason, and every other actor keeps running.
Run untrusted code →Game data
Patches and mods replace files by name
A game's own data is read through res: paths. In development that is a folder
beside your script. For release, scua res build writes a pack file, and each
pack you mount after it sits above the last, so a patch or a mod replaces a file by
carrying a file of the same name.
Resources are read-only. Saves and settings go to user: paths, which resolve
to the right per-user folder on each system. A snapshot of a partition records which packs it ran with.
Packages for games
Try the game tutorial
It builds a small working game from nothing, in your browser.