Example
// If you have an entity named enemy
if (enemy.exists) {
const d = distanceTo(enemy.x, enemy.y);
}Developer Docs
Built-in scripting functions, variables, alias access, and audio control available in the Voidless game engine runtime.
At a glance
Use these APIs inside entity events to handle input, movement, collisions, rendering, audio playback, and runtime state.
Core Scripting
Any entity name can be used directly in scripts when the name is a valid variable, such as enemy.x or enemy.vars.hp.
Example
// If you have an entity named enemy
if (enemy.exists) {
const d = distanceTo(enemy.x, enemy.y);
}Example
// Read/write vars on another entity instance
enemy.vars.alert = true;
enemy.hp = (enemy.hp ?? 10) - 1;Example
// Filter collision with alias (no quotes)
if (checkCollision(x, y + 1, enemy)) {
// touching enemy
}Example
// String still works too
const nearEnemy = checkCollision(x, y + 1, 'enemy');Runtime API
Functions available in entity events for input handling, movement, collision checks, drawing, spawning, view changes, and gameplay control.
| Name | Signature | Description |
|---|---|---|
| keyDown | keyDown(key: string) | True while the key is held. For arrow keys, use KeyboardEvent.key names like ArrowUp, ArrowDown, ArrowLeft, and ArrowRight. |
| keyPressed | keyPressed(key: string) | True only on the frame the key is pressed. For arrow keys, use ArrowUp, ArrowDown, ArrowLeft, and ArrowRight. |
| keyReleased | keyReleased(key: string) | True only on the frame the key is released. For arrow keys, use ArrowUp, ArrowDown, ArrowLeft, and ArrowRight. |
| move | move(dx: number, dy: number) | Move the current instance by an offset. |
| setPosition | setPosition(x: number, y: number) | Set absolute position of the current instance. |
| setColor | setColor(hex: string) | Set tint color for the current instance sprite. |
| clamp | clamp(value: number, min: number, max: number) | Clamp a value into a range. |
| log | log(...args: unknown[]) | Write values to the runtime console. |
| drawText | drawText(x: number, y: number, text: string) | Queue text to render at a position for the current frame (call from Step). |
| drawRect | drawRect(x: number, y: number, width: number, height: number, color?: string, filled?: boolean, lineWidth?: number) | Draw a rectangle for the current frame. Uses top-left coordinates. Set filled to false for an outline. |
| drawCircle | drawCircle(x: number, y: number, radius: number, color?: string, filled?: boolean, lineWidth?: number) | Draw a circle for the current frame. Uses center coordinates. Set filled to false for an outline. |
| drawLine | drawLine(x1: number, y1: number, x2: number, y2: number, color?: string, lineWidth?: number) | Draw a line for the current frame with an optional stroke width. |
| drawSprite | drawSprite(x: number, y: number, entityRef: entityName | entityAlias, stateName?: string, frameIndex?: number) | Queue an entity sprite/visual at a position for the current frame, with optional state and frame override. |
| playAudio | playAudio(name: string, volume?: number, pitch?: number, loop?: boolean) | Play an audio resource by name and return an audio handle. Volume and pitch default to 1. Loop defaults to false. |
| stopAudio | stopAudio(audio: audioHandle | string | null | undefined) | Stop a playing audio handle and reset it to the start. |
| mouseDown | mouseDown() | True while mouse button is held. |
| mousePressed | mousePressed() | True on the frame mouse button is pressed. |
| mouseReleased | mouseReleased() | True on the frame mouse button is released. |
| entityCreate | entityCreate(entityRef: entityName | entityAlias, x: number, y: number, layer?: string) => instanceRef | Spawn a new instance by entity alias/name. Optionally pass a layer name to place it into that view layer folder. Unknown or omitted layers spawn ungrouped. Returns an instanceRef (x/y/hspeed/vspeed/vars/exists/instanceId). |
| entityDestroy | entityDestroy(instanceRef?: string | instanceRef) | Destroy a target instance by instance id or instanceRef. If omitted, destroys self. |
| entityExists | entityExists(instanceRef: string | instanceRef) | Check if an instance id or instanceRef is still alive. |
| entityCount | entityCount(entityRef?: entityName | entityAlias) | Count all instances, or one entity. Accepts alias (enemy) or string ('enemy'). |
| checkCollision | checkCollision(x: number, y: number, entityRef?: entityName | entityAlias) | Test collision overlap at a position, optionally filtered by entity alias/name. |
| moveAndCollide | moveAndCollide(dx: number, dy: number, entityRef?: entityName | entityAlias) | Move with simple collision resolution and optional entity alias/name filter. |
| setVelocity | setVelocity(vx: number, vy: number) | Set physics velocity for the current instance. |
| applyGravity | applyGravity(amount: number, maxFall?: number) | Apply vertical acceleration with optional terminal speed. |
| distanceTo | distanceTo(x: number, y: number) | Distance from instance position to target point. |
| setSpriteState | setSpriteState(stateName: string) | Switch active sprite state by state name. |
| setSpriteFrame | setSpriteFrame(frameIndex: number) | Set current frame index for the active sprite state. |
| setSpriteFps | setSpriteFps(fps: number) | Override animation playback speed for the current instance. |
| setView | setView(viewName: string, effect?: 'none' | 'fade') | Switch to another view by view name. The optional effect defaults to none for an instant switch, or use fade to fade out and back in. |
| gameRestart | gameRestart() | Restart the current game scene (preview/export runtime). |
Runtime State
Per-instance and room-level state exposed automatically every frame, including positions, animation state, timing, and shared vars.
| Name | Type | Description |
|---|---|---|
| mouseX | number | Mouse x position in room coordinates. |
| mouseY | number | Mouse y position in room coordinates. |
| self | string | Current instance id. |
| entity | string | Current entity name. |
| x | number | Current instance x position. |
| y | number | Current instance y position. |
| hspeed | number | Current instance horizontal speed. |
| vspeed | number | Current instance vertical speed. |
| originX | number | Current sprite origin x value, typically between 0 and 1. |
| originY | number | Current sprite origin y value, typically between 0 and 1. |
| color | string | Current tint color hex string for the instance sprite. |
| roomWidth | number | Current room width in pixels. |
| roomHeight | number | Current room height in pixels. |
| delta | number | Delta time in milliseconds for current frame. |
| time | number | Elapsed room time in milliseconds. |
| spriteState | string | Current sprite state name. |
| spriteFrame | number | Current frame index in active state. |
| spriteFps | number | Current animation FPS for active state. |
| vars | Record<string, unknown> | Instance-scoped variables shared across scripts for this instance. |
| globalThis | Record<string, unknown> | Shared variables accessible from all instance scripts. |
| Math | typeof Math | Built-in JavaScript math helpers such as Math.random, Math.floor, Math.sin, and Math.PI. |
Audio Handles
playAudio(...) returns a handle you can store in a variable, vars, or globalThis to inspect and control playback later.
Example
const jumpSfx = playAudio('jump');
if (jumpSfx && jumpSfx.isPlaying) {
log('jump is playing');
}Example
let bgm = globalThis.bgm;
if (!bgm || !bgm.isPlaying) {
bgm = playAudio('music_main', 0.6, 1, true);
globalThis.bgm = bgm;
}Example
if (globalThis.bgm?.isPlaying) {
globalThis.bgm.pitch = 0.9;
}
stopAudio(globalThis.bgm);| Member | Signature | Description |
|---|---|---|
| id | id: string | Unique runtime id for this playback instance. |
| name | name: string | Audio resource name used to create this playback instance. |
| isPlaying | isPlaying: boolean | True while the audio is actively playing. |
| isPaused | isPaused: boolean | True when the audio element is paused. |
| duration | duration: number | Playback length in seconds when metadata is available. |
| ended | ended: boolean | True after playback reaches the end. |
| currentTime | currentTime: number | Current playback position in seconds. You can also assign to seek. |
| volume | volume: number | Playback volume from 0 to 1. You can read or assign it. |
| pitch | pitch: number | Playback rate/pitch. `1` is normal speed. |
| loop | loop: boolean | Whether the current playback loops. |
| play | play(): void | Resume playback or restart it if it had ended. |
| pause | pause(): void | Pause playback without resetting currentTime. |
| stop | stop(): void | Stop playback and reset it to the beginning. |