API reference
Handles
A handle is a script's reference to something in the game: a block, an object, a sound, a player. Read fields with handle.name and call methods with handle:method().
Every handle also has:
isValid(): boolean: False once the thing the handle refers to is gone. Every handle has it.
Block
A block in a creation.
| Field | Type | Description |
|---|---|---|
creation read-only | string | The id of the creation it belongs to. |
id read-only | string | The block's unique id, the same on every machine. |
mechanism read-only | string | The mechanism the block runs ("Thruster", "Servo" …), or an empty string for a plain block. This is the name building.raiseLimit takes. |
name read-only | string | The block type's display name. |
position read-only | vec3 | Where the block is, in world coordinates. |
rotation read-only | quat | Which way the block faces. |
type read-only | string | The block type's id: "Cube" for the game's own, "author.mod:sphere" for a mod's. |
velocity read-only | vec3 | How fast the block is moving, in metres per second. |
Block:get(name: string): number|boolean?
One setting's value, or nil.
Block:push(force: vec3, impulse?: boolean)
Pushes the moving part this block is on. Server scripts only; the creation must be simulating.
block:push(vec3(0, 9.81 * mass, 0))Block:set(name: string, value: any)
Changes one of the block's settings, exactly as its panel would. Client scripts, on creations you own; the server checks both.
for _, b in ipairs(building.selection()) do
b:set("Force", 2500000)
endBlock:settings(): table?
The block's mechanism settings as { name = { value, min, max } }, or nil when it has none.
Block:trySet(name: string, value: any): boolean, string?
Like Block:set, but a refusal returns false and the reason instead of raising an error. Wrong arguments, side and permission errors still raise.
Returns true and the result on success; false and the reason on a refusal.
for _, b in ipairs(building.selection()) do
b:set("Force", 2500000)
endBuildTool
A button your mod added to the building toolbar.
BuildTool:setActive(active: boolean)
Shows the tool as switched on or off.
Effect
A particle effect: sparks, smoke, fire …
| Field | Type | Description |
|---|---|---|
position | vec3 | Where the effect is. |
Effect:destroy()
Removes the effect at once.
Effect:emit(count?: integer): Effect
Emits a number of particles at once.
Effect:follow(target: Object, offset?: vec3): Effect
Makes the effect follow one of your objects.
Effect:play(): Effect
Starts emitting again after :stop().
Effect:stop(): Effect
Stops emitting; particles already in the air finish their flight.
Gear
A set of teeth on a block — a pitch circle, a rack's pitch line or a worm's thread — turning with a shaft or fixed to its part.
| Field | Type | Description |
|---|---|---|
block read-only | Block | The block the teeth are on. |
kind read-only | string | "circle", "line" (a rack) or "screw" (a worm). |
mine read-only | boolean | True when your mod made it — only then can you change it. |
radius read-only | number | Pitch radius, metres (circles and screws). |
shaft read-only | Shaft | The shaft it turns with, or nil when it is fixed to its part. |
teeth read-only | integer | Its number of teeth (0 when unknown). |
Gear:destroy()
Removes the teeth, and every mesh they were in.
Gear:meshes(): GearMesh[]
The meshes it is part of.
Gear:setShaft(shaft?: Shaft)
Moves it onto a shaft (it turns with it), or with nil fixes it to its block's part.
GearMesh
Two gears in mesh.
| Field | Type | Description |
|---|---|---|
a read-only | Gear | One of its gears. |
b read-only | Gear | The other gear. |
engaged read-only | boolean | Whether the teeth are in mesh right now (a pinion run off the end of its rack is not). |
force read-only | number | The force on the teeth last step, N (gears on different parts only; gears on one part share a train and carry no load of their own). |
skips read-only | integer | How many teeth have jumped since it was made (overload). |
slip read-only | number | How far the teeth have slipped from exact mesh, metres. |
GearMesh:destroy()
Removes the mesh.
HttpRequest
A web request on its way. The callback runs once, when it finishes, fails or is cancelled.
| Field | Type | Description |
|---|---|---|
done read-only | boolean | True once the callback has run (or will not, because the request was cancelled). |
url read-only | string | The address being requested. |
HttpRequest:cancel()
Stops the request. The callback runs with ok = false and error = "cancelled".
KeyBinding
A key your mod is listening for. Call :off() to stop.
| Field | Type | Description |
|---|---|---|
name read-only | string | The key this binding listens for. |
KeyBinding:off()
Stops listening. Safe to call more than once.
Material
A surface: a shader plus its settings. Put one on an object with object:setMaterial.
| Field | Type | Description |
|---|---|---|
shader read-only | string | The shader this material uses. |
Material:setColor(property: string, value: color): Material
Sets a colour on the material.
glow:setColor("_EmissionColor", color("#40c0ff"))Material:setNumber(property: string, value: number): Material
Sets a number on the material.
Material:setTexture(property: string, texture: Texture): Material
Sets a picture on the material.
Material:setVector(property: string, value: vec3, w?: number): Material
Sets a direction or a set of four numbers on the material.
ModExports
Another mod's exports (see mods.get). Read fields and call functions; it cannot be modified.
Model
A model your mod has loaded, with its meshes and materials.
| Field | Type | Description |
|---|---|---|
file read-only | string | The file it came from. |
meshes read-only | integer | How many separate meshes it has. |
triangles read-only | integer | How many triangles it has altogether. |
Model:bounds(): any
The size of the model in metres, as { min = vec3, max = vec3, size = vec3 }.
Model:materialNames(): string[]
The names of the materials the model uses.
Object
Something your mod put in the world.
| Field | Type | Description |
|---|---|---|
name | string | The object's name. |
position | vec3 | Where the object is, in world coordinates that mean the same place on every player's machine. |
rotation | quat | Which way the object is facing. |
scale | vec3 | How big the object is. |
visible | boolean | Whether the object is drawn. |
Object:addLight(options?: any): Object
Adds a light to the object.
lamp:addLight{ color = color("#ffd070"), range = 12, intensity = 3 }Object:destroy()
Removes the object from the world for good.
Object:follow(other: Object, offset?: vec3, matchRotation?: boolean): Object
Makes the object follow another of your objects.
Object:followPlayer(player: Player, offset?: vec3, faceCamera?: boolean): Object
Makes the object follow a player, keeping an offset in the player's own directions.
marker:followPlayer(host, vec3(0, 2.4, 0))Object:lookAt(target: vec3): Object
Points the object at a place.
marker:lookAt(player.position)Object:setColor(color: color): Object
Tints every part of the object.
marker:setColor(color("#40c0ff"))Object:setMaterial(material: Material): Object
Draws every part of the object with one of your materials.
local glow = assets.material{ color = color("#40c0ff"), emissive = color("#40c0ff") }
shell:setMaterial(glow)Object:unfollow(): Object
Stops the object following anything.
Player
A player in the session. Equal Player objects refer to the same person; a Player stays valid until they leave.
| Field | Type | Description |
|---|---|---|
id read-only | integer | The player's id: the host is 0. |
name read-only | string | Display name. |
Player:clearHighlight()
Removes your highlight from a player.
Player:highlight(look: any)
Highlights a player with a colour, which draws a soft glow, or with one of your materials, drawn over the player. Only you see it.
players.host():highlight(color("#ffcc40"))
players.host():highlight(assets.material{ emissive = color("#40c0ff"), alpha = "blend" })Player:isHost(): boolean
True for the player hosting the session.
Player:isLocal(): boolean
True for this machine's own player.
Player:position(): vec3
Where the player is.
Player:rotation(): quat
Which way the player faces.
Player:teleport(position: vec3, rotation?: quat?)
Moves the player. Server scripts only.
for _, p in ipairs(players.all()) do p:teleport(players.host():position() + vec3(2, 0, 0)) endShaft
An axle a block turns on, relative to the part it is on. Everything on it turns together; shafts meshed through gears form a gear train.
| Field | Type | Description |
|---|---|---|
angle read-only | number | How far it has turned since its creation was built, in degrees. Not wrapped: 720 is two turns. |
block read-only | Block | The block whose axle this is. |
jammed read-only | boolean | True when the gears it meshes with lock each other and nothing can turn. |
mine read-only | boolean | True when your mod made it — only then can you change it. |
rpm read-only | number | How fast it turns relative to the part it is on, in turns per minute (signed). |
simulated read-only | boolean | True while its gear train is being simulated (a creation in play, with gear simulation running). |
torque read-only | number | The torque its drive applied last physics step, N·m. |
Shaft:add(block: Block)
Makes another block turn with it (it must be on the same part). Its spinning meshes are drawn turning.
Shaft:blocks(): Block[]
The blocks turning with it.
Shaft:brake(torque: number)
Holds it still with up to torque N·m.
Shaft:destroy()
Removes the shaft. Gears on it stay, fixed to their blocks' parts.
Shaft:free()
Lets it turn freely: only its gears and friction act on it.
Shaft:motor(rpm: number, torque: number)
Drives it as a motor: towards rpm (signed), with at most torque N·m.
shaft:motor(120, 800)Shaft:remove(block: Block)
Stops a block turning with it.
Shaft:servo(degrees: number, rpm: number, torque: number)
Turns it to an angle (degrees since built, not wrapped) at up to rpm, then holds it with at most torque N·m.
Shaft:setFriction(friction: number, damping?: number)
Bearing friction (constant torque against turning, N·m) and damping (N·m per rad/s).
Shaft:setInertia(inertia: number)
Adds inertia to it (a flywheel), kg·m², on top of what its blocks have.
Sound
A sound that is playing, or about to.
| Field | Type | Description |
|---|---|---|
pitch | number | Playback speed, where 1 is normal. Changes the pitch too. |
position | vec3 | Where the sound comes from. Only matters for a sound placed in the world. |
ready read-only | boolean | True once the sound is loaded and can play. |
volume | number | How loud, from 0 to 1. |
Sound:fadeOut(seconds?: number)
Fades the sound out over a number of seconds, then stops it.
Sound:isPlaying(): boolean
True while the sound is playing.
Sound:stop()
Stops the sound and frees it. Safe to call more than once.
Sound:time(): number
How far through the sound is, in seconds.
Subscription
An event subscription. Call :off() to stop receiving the event.
Subscription:off()
Stops the subscription; the handler will not run again, not even for an event raised a moment ago that has not been delivered yet. Safe to call more than once.
Task
A background task started with task.spawn.
Task:cancel()
Stops the task before its next step. Safe to call more than once.
Task:isRunning(): boolean
True until the task finishes, fails or is cancelled.
Texture
A picture your mod has loaded.
| Field | Type | Description |
|---|---|---|
file read-only | string | The file it came from. |
height read-only | integer | How tall the picture is, in pixels. |
width read-only | integer | How wide the picture is, in pixels. |
Timer
A scheduled callback returned by timer.after or timer.every.
Timer:cancel()
Stops the timer. Safe to call more than once.
Timer:remaining(): number
Seconds until the timer next fires.
UiElement
A part of an interface: a panel, a button, a slider, a label.
| Field | Type | Description |
|---|---|---|
isGame read-only | boolean | True when this belongs to the game rather than to a mod. |
kind read-only | string | What kind of element this is: "Button", "Label", "Slider" … |
name read-only | string | The element's name, as written in name="…". |
UiElement:add(child: UiElement): UiElement
Puts another element inside this one.
panel:add(ui.element("Button", { text = "Apply" }))UiElement:addClass(name: string): UiElement
Adds a style class, the same names a .uss file uses.
UiElement:addNew(kind: string, options?: any): UiElement
Creates an element and puts it inside this one, in one step.
local apply = panel:addNew("Button", { text = "Apply", class = "primary" })UiElement:children(): UiElement[]
The elements directly inside this one.
UiElement:destroy()
Removes the element and everything in it for good. Only for elements this mod made.
UiElement:find(selector: string): UiElement
The first element under this one matching a selector, or nil.
local power = panel:find("#power")UiElement:findAll(selector: string, limit?: integer): UiElement[]
Every element under this one matching a selector.
UiElement:focus(): UiElement
Gives the element keyboard focus.
UiElement:get(property: string): any
Reads a property: "value", "label", "tooltip", "enabled", "visible", "choices" …
UiElement:hasClass(name: string): boolean
Whether the element has a style class.
UiElement:hide(): UiElement
Hides the element without removing it. The layout closes up around it.
UiElement:on(what: string, handler: function): UiSubscription
Runs a function when something happens: "click", "change", "input", "focus", "blur", "enter", "leave".
apply:on("click", function() applySettings() end)
slider:on("change", function(value) preview(value) end)UiElement:parent(): UiElement?
The element this one is inside, or nil.
UiElement:remove()
Takes the element out of the interface. A game element is put back when the mod stops.
UiElement:removeClass(name: string): UiElement
Removes a style class.
UiElement:set(property: string, value: any): UiElement
Sets a property: "value", "label", "tooltip", "enabled", "choices", "index".
slider:set("value", 42)UiElement:setText(text: string): UiElement
Sets the element's text.
title:setText("Thrusters")UiElement:show(): UiElement
Shows an element that was hidden.
UiElement:style(styles: any): UiElement
Sets styles, written the same way as in a .uss file.
panel:style{ width = "320px", ["background-color"] = "#202024e0" }UiElement:text(): string?
The element's text, or nil when it has none.
Video
A video that is playing on an interface element or on an object.
| Field | Type | Description |
|---|---|---|
length read-only | number | How long the video is, in seconds. 0 until it is ready. |
ready read-only | boolean | True once the video has been opened and its length is known. |
time | number | How far through the video is, in seconds. |
volume | number | The video's sound volume, from 0 to 1. |
Video:isPlaying(): boolean
True while the video is playing.
Video:onEnd(fn: function): Video
Calls fn each time the video reaches its end (every loop, when looping).
intro:onEnd(function() intro:stop() panel:destroy() end)Video:pause(): Video
Pauses the video.
Video:resume(): Video
Carries on after :pause().
Video:stop()
Stops the video and frees it. Safe to call more than once.