Ember is discontinued. It still works, but it gets no more updates or support. Back to uayi.org
Home · Terrified Gorillas · Glimmerwood · Starfall Hollow · ‹ Prev Next ›
Ember
Download

Scripting

Scripting with Lua

Behaviours are Lua 5.4 scripts attached to objects. A script is a table of functions that Ember calls:

local M = {
    speed = 5,          -- defaults; Script Fields in the Inspector override them per object
}

function M:start()
    self.spin = 0
end

function M:update(dt)   -- every fixed step (60 per second); dt is the step length in seconds
    self.spin = self.spin + 90 * dt
    self.entity.rotation = vec3(0, self.spin, 0)
end

function M:on_collision(other, hit)   -- hit = { point, normal, speed }
    if other.name == "Player" then audio.play("sounds/coin.wav", self.entity.position) end
end

function M:on_trigger_enter(other) end   -- something entered a trigger collider
function M:on_trigger_exit(other) end
function M:on_animation_event(name) end  -- an event key in a keyframe clip
function M:on_destroy() end

return M

self is the script's own table for this object, so every object keeps its own state. self.entity is the object. All of these functions are optional.

Making a script

Script settings in the Inspector (like Unity)

The plain values in the table your script returns are its settings, and the Inspector shows each one as a proper control: numbers get a drag field, true / false a checkbox, text a text box, vec3(...) three fields, colours a colour picker, and asset paths a picker:

local M = {
    -- @header Movement
    speed = 5,              -- @range(0, 20) How fast it walks
    lives = 3,              -- @int
    jump = true,
    label = "Bank",
    offset = vec3(0, 2, 0),
    tint = {1, 0.5, 0.2},   -- names with "color" / "colour", or @color, get a colour picker
    secret = 42,            -- @hide
}

Comments on a value's line set how it's shown: @range(min, max) (a slider), @int, @color, @hide, @header Text (a heading above it); any other text becomes the tooltip. Changing a value on an object stores only what differs from the script's default, so editing the script's default later still reaches every object you didn't change. The editor reads the defaults by running the file in a small sandbox, so it needs no running game.

An object can have several scripts (Add Component > Script, more than once), for example a Blocks behaviour and a Lua script side by side. Each gets its own settings. From Lua, self.entity:script_named("gun") finds a sibling script and self.entity:scripts() lists them.

Script Fields (as text)

The same settings can be typed as text in Inspector > Script > Fields, like the inside of a Lua table:

speed = 4, label = "Bank", color = {1, 0.5, 0}

They replace the script's defaults on that object only, so one door.lua can serve every door with a different key. Spawned objects take them too: scene.spawn{ ..., script = "scripts/door.lua", fields = "key = 'red'" }.

Talking to other objects

local player = scene.find("Player")             -- nil if there is none
for _, coin in ipairs(scene.entities("Coin")) do coin:destroy() end
local hp = player.script.health                 -- another object's script table
player.script:hurt(1)                           -- call a function in it

Hot reload

Save a script (Ctrl+S in the Code window) while the game is playing and it reloads in the running game. Objects keep their state (self.*) and use the new functions from their next call. If the new code has a syntax error, the old code keeps running and the error is shown in the Code window and the Console.

require

require("name") looks in your project's scripts/, then in installed packages' scripts/, then in Ember's stock scripts. Stock modules you can reuse include:

Module What it is
character animated character controller, third or first person (view = "first"): walk, sprint, jump, camera, kick, get into cars, lava respawn
gun a hitscan gun for the character: fire, ammo, reload, crosshair, muzzle flash, a blaster in first person
anim_body plays idle / walk / sprint / jump / fall on a rigged model from how its body moves (next to any controller)
player2d 2D platformer hero with a sprite sheet
maze, proc_terrain, road_builder the procedural templates' builders
third_person, fps_player, platformer_player, topdown_player, runner_player other player controllers
car, boat drivable vehicles with sound
footsteps footstep sounds that match the ground (steps.play(pos, self.entity, volume))
ambience a looping world ambience with gusts
enemy, pickup, checkpoint, goal, spawner, mover, spinner, hud game pieces

Sandbox

Scripts can't run programs or reach files outside the game: io, os.execute, os.remove, os.rename, os.exit, dofile and loadfile aren't available, and load only accepts source text. Use file.* for the game's own files, prefs for settings and http for the web.

Errors

Errors in a script show in the Console with the file and line, and in the player's corner while testing. One failing script doesn't stop the others.

Other ways to script

The full list of functions is in the Lua API reference.

Ember 1.1 documentation. Found a mistake? The source of this page is site/docs/scripting.md in the Ember source.