VERTEXDocsGitHub

Rules reference

Rules are game logic as data. Normative definition: SPEC.md §6.

[
  { "on": "start", "do": "state.score = 0" },
  { "each": "player", "when": "held('jump') && self.body.ground", "do": "self.vy = -500; sound('jump')" },
  { "on": "hit", "tags": ["player", "coin"], "do": "despawn(b); state.score += 1" },
  { "do": "state.hud = 'SCORE ' + str(state.score)" }
]

Rule kinds#

Rules run in array order, once per step, where "rules" sits in systems.

RuleRunsBinds
{ do }every step—
{ each: tag, do }once per entity with tagself
{ on: "start", do }first step only—
{ on: "hit", tags?: [a, b], do }per hit event so far this step, matching tag ordera, b, event
{ on: "contact", tags?: [a, b], do }per physics contact, tags in either ordera (has tag a), b, event
{ on: "<type>", do }per event of that type so far this stepevent

Any rule may add when: "<expression>"; do is a string or an array of strings (joined by ;).

Language#

SyntaxExample
statementsa; b; c
local variablelet n = count('coin')
assignment=, +=, -=, *=, /= on locals or fields: self.data.hp -= 1
if / elseif (state.hp <= 0) { goto('over') } else state.hp -= 1
bounded looprepeat (5) spawn('rock', {x: i * 40, y: 20}) (i = 0…n−1, max 10,000)
operators+ - * / %, == != < <= > >= (strict), && || !, c ? a : b
literals1.5, 'text' or "text", true, false, null, [1, 2], {x: 1, tags: ['a']}
accessself.x, state.items[0], event.name; missing fields are undefined

Values follow JavaScript semantics restricted to JSON data plus entities. + concatenates when either side is a string. Forbidden: __proto__, prototype, constructor, unknown names/functions.

Variables#

NameValue
dtseconds per step (1/60 by default)
frame, timestep count, seconds elapsed
stateworld.state, game-wide JSON
W, Hworld width, height
PIπ
pointer{x, y} in world units (per player online)
cameraworld.camera or null
self, a, b, eventbound by the rule kind (above)

Builtins#

Input#

held(action)action is down. Online: the current player's input
pressed(action)action went down this step
axis(name)analog value in [−1, 1] ('x', 'y' from sticks/joystick), 0 if unset

Randomness and noise#

random()seeded, [0, 1)
rand(lo, hi)seeded, [lo, hi)
hash(a, b, c)deterministic [0, 1) from integers: use for procedural content
noise(x, y, seed)smooth value noise in [0, 1)

Entities#

spawn(shape, fields?)create an entity, returns it. shape may be null (text, emitters, invisible)
despawn(e)remove an entity (or id)
get(id)entity or null
alive(e)still in the world
first(tag) · all(tag) · count(tag)query by tag
has(e, tag) · tag(e, t) · untag(e, t)tag tests and edits
nearest(x, y, tag)closest entity with a tag, or null
at(x, y, tag?)topmost entity whose outline contains the point
raycast(x, y, dx, dy, max?, tag?){entity, x, y, distance} of the first outline hit, or null
dist(a, b)distance between entities

Events, time, flow#

emit(type, payload?)emit an event (visible to later systems this step)
after(seconds, type, payload?)emit an event later (saved with the world)
goto(scene)switch scene at the end of this step
sound(name)play a sound from sounds
burst(e, n)make an entity's emitter spray n particles
talk(dialogueId, npc?, actor?)start a conversation
inDialogue(actor?)a conversation is open

Math and text#

abs min max floor ceil sqrt signas in JavaScript
round(v)floor(v + 0.5)
clamp(v, lo, hi) · lerp(a, b, t)
sin cos atan2radians (implementation-tolerant: see SPEC §8)
str(v) · len(v)to string · length of string/array (0 for null)

Events emitted by the engine#

EventFieldsFrom
hita, b, tagscollide, collideOutline, collide3d
contacta, b, nx, nyphysics
scenenamegoto (delivered next step)
chunkEnter / chunkLeavecx, cy, key, fresh, x, y, sizestreaming
interacta (actor), b (target)interact
dialogue · choice · dialogueEndid, node, who, actor, (choice)dialogue
join / leaveplayer, entitymultiplayer server (delivered next step)
soundnamesound()
your timersyour payloadafter()

Edit this page