Lua API documentation
How your script runs
Your script can define these optional functions:
setup(): runs once when you press Run.update(delta_time): runs many times per second, once per frame.delta_timeis elapsed time since last frame in seconds.draw(): runs many times per second, once per frame, afterupdateto render the current frame.
Think of it like this:
setup= initialize your variables.update= change your game state.draw= show that state on the screen.
Your first tiny script
Paste this and press Run:
local x = 0
local y = 4
function update(delta_time)
x = x + 6 * delta_time
if x > SCREEN_W then
x = 0
end
end
function draw()
clear()
set_pixel(math.floor(x), y, rgb(255, 100, 40))
end
What you should see: one bright pixel moving left-to-right, then restarting.
Input basics
on_press(button): runs once when the button goes down.on_release(button): runs once when the button goes up.on_repeat(button): runs while held, after a short delay, then at a fixed interval. Does not fire on the initial press; useon_pressfor that.is_pressed(button): true every frame while the button is held.
Smooth movement (slide while held): poll is_pressed in update:
local x = 10
function update(dt)
if is_pressed("L_LEFT") then
x = x - 10 * dt
end
if is_pressed("L_RIGHT") then
x = x + 10 * dt
end
end
Stepped movement (one tile per repeat, e.g. grid cursor): on_press for the first step, on_repeat for held repeats:
function on_press(button)
move_cursor(button)
end
function on_repeat(button)
move_cursor(button)
end
Coordinates and screen model
- Coordinates are
0-based. - Top-left is
(0, 0). - Valid
x:0 .. 19 - Valid
y:0 .. 9
Input key map
These are the default keyboard keys and button names used by is_pressed,
on_press, on_repeat, and on_release.
| Logical button | Keyboard key |
|---|---|
L_UP |
w |
L_LEFT |
a |
L_DOWN |
s |
L_RIGHT |
d |
R_UP |
Arrow Up |
R_LEFT |
Arrow Left |
R_DOWN |
Arrow Down |
R_RIGHT |
Arrow Right |
L_BUMP |
q |
R_BUMP |
e |
MENU |
1 |
ESC |
2 |
Project files
In the browser SDK, your project lives in a virtual file system stored locally in the browser.
- Use the file explorer on the right to create, upload, rename, and delete files.
- Save the file you are editing with Ctrl+S (Windows/Linux) or Cmd+S (macOS).
- Press Run to execute whatever is currently open in the editor.
Loading other Lua files
You can split a game or library across multiple .lua files and load them with require.
How to call require
Pass the full path to the file in the project, replacing / with . and omitting the .lua extension:
local mylib = require("mygame.lib.mylib") -- Will load /mygame/lib/mylib.lua
What require does
- Loads the file, runs it once, and caches the result (standard Lua
package.loadedbehavior). - If the file ends with
return something, that value is whatrequiregives you (tables are common for shared libraries). - If the file does not return a value,
requirestill succeeds; you mainly get side effects (e.g. defining globals; preferreturnfor libraries).
Example: main + library
/mygame/lib/colors.lua:
local M = {}
function M.player()
return rgb(255, 100, 40)
end
return M
/mygame/main.lua:
local colors = require("mygame.lib.colors")
function draw()
clear()
set_pixel(10, 4, colors.player())
end
Create both files in the explorer, open /main.lua and press Run.
What is not available
For sandboxing, only require can load other Lua files. The following are not available in scripts:
dofileloadfileloadloadstring
Use the editor + Run for your entry script, and require("path.to.luafile") for any other lua script file you might need. To read other kinds of data files, use read_file().
Common libraries
Common libraries are available in the built-in files, under the lib folder. This directory contains many useful shared libraries, such as:
- Vector2 / Vector3
- Set
- Enum
- Tween
- Text
- Math utils
- Music sequencer
- Line / Anti-aliased line
These libraries are currently not documented; explore the Lua scripts and examples to understand how they work !
API reference
Constants
| Name | Type | Value | Description |
|---|---|---|---|
SCREEN_W |
number | 20 |
Screen width in pixels |
SCREEN_H |
number | 10 |
Screen height in pixels |
Color
rgb(r, g, b)
Create an RGB color table.
Parameters:
r(number): red channel (0-255)g(number): green channel (0-255)b(number): blue channel (0-255)
Returns: table RGB color table {r, g, b}
Example:
local red = rgb(255, 0, 0)
hsl(h, s, l)
Convert HSL values to an RGB color table.
h is wrapped over 360 degrees, s and l are clamped to 0..1.
Parameters:
h(number): hue in degrees (any number, wrapped mod 360)s(number): saturation (0.0..1.0)l(number): lightness (0.0..1.0)
Returns: table RGB color table {r, g, b}
Example:
local cyan = hsl(180, 1.0, 0.5)
Console
print(messages)
Write a line to the SDK console panel.
Handy for quick debugging while you are iterating on game logic.
(This replaces the default Lua print)
Parameters:
messages(variable): Lua variables to print
Returns: nil
Example:
print("Hello, world!") -- Output: Hello, world!
print("Hello", 1, true, nil) -- Output: Hello 1 true nil
Display
get_pixel(x, y)
Read the current color of one pixel.
Useful for effects that react to what is already drawn (sampling, simple collision checks against color, post-processing tricks, etc.).
Parameters:
x(number): integer pixel x coordinate (0-based)y(number): integer pixel y coordinate (0-based)
Returns: table RGB color table {r, g, b}
Example:
set_pixel(0, 0, rgb(42, 0, 134))
local my_color = get_pixel(0, 0)
print(my_color[1]) -- 42
print(my_color[2]) -- 0
print(my_color[3]) -- 134
set_pixel(x, y, rgb_color)
Set one pixel to an RGB color.
This is the most direct drawing primitive: great for point effects, particles, and any algorithm that draws pixel by pixel.
Parameters:
x(number): pixel x coordinate (0-based)y(number): pixel y coordinate (0-based)rgb_color(table): {r, g, b} color table with components in the range 0..255
Returns: nil
Example:
set_pixel(3, 2, rgb(255, 0, 0))
set_pixel_blend(x, y, rgb_color, alpha)
Blend a color onto one pixel instead of replacing it.
Use this for transparency and softer visuals (trails, fades...) by mixing with the color that is already on screen.
Parameters:
x(number): integer pixel x coordinate (0-based)y(number): integer pixel y coordinate (0-based)rgb_color(table): {r, g, b} color table with components in the range 0..255alpha(number): blend amount in range0.0..1.0(0=keep old, 1=replace)
Returns: nil
Example:
set_pixel_blend(3, 2, rgb(255, 0, 0), 0.5)
set_pixel_f(x, y, rgb_color)
Draw a point using floating-point coordinates.
Applies bilinear interpolation to spread out brightness on the surrounding pixels based on floating-point coordinates. Useful for smooth movement/animation where object positions are in floating-point coordinates.
Parameters:
x(number): subpixel x coordinate (float, 0-based)y(number): subpixel y coordinate (float, 0-based)rgb_color(table): {r, g, b} color table with components in the range 0..255
Returns: nil
Example:
set_pixel_f(3.25, 2.75, rgb(0, 255, 0))
set_unsafe_pixel_brightness(x, y, brightness)
UNSAFE !!! Set the brightness of a pixel.
This is useful for setting the brightness of a pixel to a value that is different from the default brightness, for short-term effects like flashing. In the web SDK, shows a corona effect on the display to simulate the brightness effect. If the total current exceeds the limit, an error checkerboard pattern will be displayed (currently not supported in the web SDK). Use with moderation.
Parameters:
x(number): pixel x coordinate (0-based)y(number): pixel y coordinate (0-based)brightness(number): brightness value (0..9)
Returns: nil
Example:
set_unsafe_pixel_brightness(3, 2, 5)
rect(x, y, w, h, rgb_color)
Draw a filled axis-aligned rectangle.
Great for UI blocks, paddles, bars, and simple game objects. Notes:
- The host accepts floats for
x,y,w,hand floors geometry internally.
Parameters:
x(number): rectangle x coordinate (float accepted; floored)y(number): rectangle y coordinate (float accepted; floored)w(number): rectangle width (float accepted; floored)h(number): rectangle height (float accepted; floored)rgb_color(table): {r, g, b} color table with components in the range 0..255
Returns: nil
Example:
rect(1, 1, 5, 3, rgb(255, 255, 255))
rect_blend(x, y, w, h, rgb_color, alpha)
Blend a filled rectangle with what is already on screen.
Useful for overlays, tint zones, and soft UI panels.
Parameters:
x(number): rectangle x coordinate (float, 0-based)y(number): rectangle y coordinate (float, 0-based)w(number): rectangle width (float)h(number): rectangle height (float)rgb_color(table): {r, g, b} color table with components in the range 0..255alpha(number): blend amount in range0.0..1.0(0=keep old, 1=replace)
Returns: nil
Example:
rect_blend(0, 0, SCREEN_W, SCREEN_H, rgb(255, 0, 0), 0.5)
rect_f(x, y, w, h, rgb_color)
Draw a rectangle using floating-point coordinates.
Applies bilinear interpolation to spread out brightness on the surrounding pixels based on floating-point coordinates. Useful for smooth movement/animation where object positions are in floating-point coordinates.
Parameters:
x(number): rectangle x coordinate (float, 0-based)y(number): rectangle y coordinate (float, 0-based)w(number): rectangle width (float)h(number): rectangle height (float)rgb_color(table): {r, g, b} color table with components in the range 0..255
Returns: nil
Example:
rect_f(1.2, 1.2, 5.5, 3.5, rgb(0, 0, 255))
fill(rgb_color)
Fill the entire screen with one color.
Most scripts call this at the start of draw() to clear the previous frame
and paint a background color in one call.
Parameters:
rgb_color(table): {r, g, b} color table with components in the range 0..255
Returns: nil
Example:
fill(rgb(255, 0, 0))
fill_blend(rgb_color, alpha)
Blend one color over the whole screen.
This keeps existing pixels visible while tinting the frame, which is useful for fades, flashes, and mood/color shifts.
Parameters:
rgb_color(table): {r, g, b} color table with components in the range 0..255alpha(number): blend amount in range0.0..1.0(0=keep old, 1=replace)
Returns: nil
Example:
fill_blend(rgb(255, 0, 0), 0.5)
clear()
Clear the screen to black (rgb(0, 0, 0)).
Equivalent to fill(rgb(0, 0, 0)) and commonly used at the start of draw().
Returns: nil
Example:
clear()
File system
read_file(path)
Read a complete file at the given path
Parameters:
path(string): path to the file
Returns: string: binary string representing the file contents or errors if the file couldn't be found
Example:
local my_file = read_file("/file.bin")
read_file_chunk(path, offset, size)
Read a chunk of data from the file at the given path, offset and size. If the size at the given offset exceeds the file size, only the remaining bytes will be returned.
Parameters:
path(string): path to the fileoffset(int): offset since the start of the file in bytessize(int): size of the chunk to read in bytes
Returns: string: binary string representing the file contents or nil if the file couldn't be found
Example:
local my_chunk = read_file_chunk("/file.bin", 0, 512) -- reads the 512 first bytes of the file
file_size(path)
Get the size of the file at path
Parameters:
path(string): path to the file
Returns: int: size of the file in bytes
Example:
size = file_size("/file.bin")
list_directory(path)
Get a list of the contents of the directory at the given path.
Parameters:
path(string): path to the file
Returns: table:<string,bool> a table with paths as keys, and a boolean representing if the path is a file (true) or a directory(false)
Example:
local items = list_directory("/my/directory/path")
Input
is_pressed(key)
Check whether a mapped button is currently held down.
Use this inside update() for continuous input (movement while a key is
held), as opposed to one-shot input events from on_press/on_release.
The key string must match the host's key map, e.g.:
L_UP/L_LEFT/L_DOWN/L_RIGHTR_UP/R_LEFT/R_DOWN/R_RIGHT
Parameters:
key(string): Logical button name (host key map)
Returns: boolean true if pressed, otherwise false
Example:
if is_pressed("L_UP") then ... end
set_repeat_delay(delay)
Set the repeat delay which is used to configure the behavior of on_repeat
Parameters:
delay(number): delay in seconds before triggering on_repeat when a key is held
Returns: nil
Example:
set_repeat_delay(0.5)
set_repeat_interval(interval)
Set the repeat interval which is used to configure the behavior of on_repeat
Parameters:
interval(number): interval in seconds between on_repeat triggers when a key is held
Returns: nil
Example:
set_repeat_interval(0.5)
Sound
buzz(frequency, duration)
Play a tone at the given frequency for the given duration.
Useful for beeps, notifications, and simple audio effects.
Parameters:
frequency(number): frequency in Hzduration(number): duration in milliseconds (max 30s)
Returns: nil
Example:
buzz(440, 1000) -- Play a 440Hz tone for 1 second
Time
get_time()
Get elapsed runtime in seconds since the script started.
Useful for timers, cooldowns, oscillations, and any time-based animation.
Returns: number Seconds (floating point) since the Lua state was created.
Example:
local t = get_time()
Lifecycle callbacks
The host calls these globals by name if they exist as Lua functions.
setup()
Called once after your script is loaded and before the first frame starts.
Use this to initialize game state and set up any values that should persist across frames.
Returns: nil
Example:
function setup()
clear()
end
update(delta_time)
Called every frame before draw().
Use this for simulation/state updates (movement, timers, collisions, input
handling). Keep rendering out of update() and perform drawing in draw().
Parameters:
delta_time(number): Seconds elapsed since the previous frame (float)
Returns: nil
Example:
function update(delta_time)
-- movement logic
end
draw()
Called every frame after update().
Use this to write pixels and render the current state for the frame. This
function should focus on drawing, while state changes stay in update().
Returns: nil
Example:
function draw()
clear()
set_pixel(1, 1, rgb(255, 0, 0))
end
process(delta_time)
Called on every main loop tick (faster than framerate)
Use this for specific things which need a higher resolution than the framerate, like for example a music sequencer.
Parameters:
delta_time(number): Seconds elapsed since the previous tick (float)
Returns: nil
Example:
function process(delta_time)
-- stuff
end
on_press(button_name)
Called when a mapped button transitions from up to down (key press edge).
Use this for one-shot actions like firing, toggling, or the first step of movement.
For smooth hold (e.g. sliding), poll is_pressed() in update(). For stepped
autorepeat while held, also define on_repeat.
Parameters:
button_name(string): Host key map name (e.g. "L_UP")
Returns: nil
Example:
function on_press(button_name)
if button_name == "L_UP" then
set_pixel(2, 2, rgb(0, 255, 0))
end
end
on_release(button_name)
Called when a mapped button transitions from down to up (key release edge).
Use this for release-driven actions or cleanup that should happen exactly once when a button is released.
Parameters:
button_name(string): Host key map name (e.g. "L_UP")
Returns: nil
Example:
function on_release(button_name)
if button_name == "L_UP" then
clear()
end
end
on_repeat(button_name)
Called while a mapped button is held, after an initial delay, then at a fixed interval.
Does not fire on the initial press; use on_press for the first action when the
button goes down. Use this for stepped input (grid cursors, menus), not smooth motion.
Repeat delay and interval can be configured using set_repeat_delay() and set_repeat_interval()
Parameters:
button_name(string): Host key map name (e.g. "L_UP")
Returns: nil
Example:
function on_repeat(button_name)
move_cursor(button_name)
end