SCUA

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.

turret.scua
-- 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 range

Inside 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 engine C or C++, owns the clock read input call the script run its turns draw every frame SCUA scripts loaded once, kept between frames your updategameplay code actors' turnswithin a budget calls the functions your engine exposes
One frame. The engine calls into the script, then runs the scripts' pending turns when it chooses; scripts call back into functions the engine exposes.

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.

Maths in the manual →

Entity loops compile

Typed arrays such as { f32 } and { vec2 } are stored unboxed, and the JIT compiles the loops over them.

Make scripts fast →

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.

Audience gates in the manual →
project.scua
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 nil
Player on the server name"Aria" health80Visible inventory[3, 7, 12]Owner bonus_timer4213Internal each field says who may see it project(aria, Player, audience) Audience.Client the owner name"Aria" health80 inventory[3, 7, 12] bonus_timernot sent Audience.Spectator name"Aria" health80 inventorynot sent bonus_timernot sent
project(aria, Player, audience) Player on the server name"Aria" health80Visible inventory[3, 7, 12]Owner bonus_timer4213Internal Audience.Clientthe owner name"Aria" health80 inventory[3, 7, 12] bonus_timernot sent Audience.Spectator name"Aria" health80 inventorynot sent bonus_timernot sent
The same record, projected for the owning client and for a spectator. A field an audience may not see is left out of what is sent.

Rooms, 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 →
room.scua
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
42
thread 1 thread 2 thread 3 thread 4 reply Room 12 Player Ana Scores Shop Player Ben Room 13, idle Room 14, idle tell room.Move(3, 1) ask shop.Buy("sword") tell scores.Add Each partition owns its state and handles one message at a time. Nothing is shared, so nothing is locked.
Players, rooms and a shop as partitions spread over threads. The runtime chooses where each one runs, and an idle room costs almost nothing until a message arrives.

Live 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 →
Server A Server B the session has left its memory is free again the bytes, as they are no save code, no load code player 4821 running frozen hp80 position(12, 4) quest"the keep" same script, same state 1Freeze between two messages 2Send the bytes 3Load them and carry on
Server A the session has left its memory is free again the bytes, as they are no save or load code Server B player 4821 running frozen hp80 position(12, 4) same script, same state 1Freeze between two messages 2Send the bytes 3Load them and carry on
A session is frozen between two messages, its bytes are sent, and the other server loads them and carries on.

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 →
code a player or a model wrote fn main(input) while true do input = input + 1 end end stopped here, between two operations Operations this turn 200,000 allowed 0 of 200,000 left Memory 4 MiB allowed Error(e) e.kind == "out_of_fuel" Your program gets a value back and carries on. The code inside cannot catch the stop, and it held no capability to begin with.
The default sandbox allows 200,000 operations a turn and 4 MiB of memory. Both are yours to set.

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.

Ship data in packs →
your code reads res:maps/keep.json res:sprites/hero.png res:sprites/tree.png winter.paka mod, mounted last hero.png patch1.paka later patch keep.json base.pakwhat you shipped keep.json hero.png tree.png user:saves/slot1.json The player's own folder, for saves and settings. Every res: layer is read-only.
Each read takes the file from the highest layer that has one. Lower copies are still there, and are used again if the mod is removed.

Packages for games

Try the game tutorial

It builds a small working game from nothing, in your browser.