Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem
Module

@ember/ui

Titles, the action bar, HUDs and live UI that redraws itself when the values it read change.

local ui = require("@ember/ui")

Functions

ui.actionBar(to: types.Player, text: types.MiniMessage, seconds: number?)ui.actionBar(to: { types.Player }, text: types.MiniMessage, seconds: number?)
#

Shows text above the hotbar of a player or a list of players, kept up for seconds when given.

local ui = require("@ember/ui")
local players = require("@ember/players")

-- Text above the hotbar; the last argument keeps it up for that many seconds.
players.joined:connect(function(player)
	ui.actionBar(player, "<aqua>Find the hidden button!", 5)
end)
ui.canvas(options: { width: number, lines: number }, draw: (canvas: ui.Canvas) -> ()) -> ui.CanvasElement
#

Makes a canvas width pixels wide and lines lines tall; draw places what it shows.

ui.computed<T>(compute: () -> T) -> ui.Readable<T>
#

Makes a readable that runs compute again once a value it read changes.

local players = require("@ember/players")
local types = require("@ember/types")
local ui = require("@ember/ui")
local world = require("@ember/world")

-- Points per player name. `computed` derives a value from it, worked out again only when the
-- points change, and anything live that reads it redraws with it.
local points = ui.value({} :: { [string]: number })

local leader = ui.computed(function()
	local best, top = "nobody", 0
	for name, score in points:get() do
		if score > top then
			best, top = name, score
		end
	end
	return `{best} ({top})`
end)

local board = ui.sidebar({
	title = "<gold><bold>Capture",
	lines = function(viewer: types.Player)
		return { `<yellow>Leader: {leader:get()}`, `<gray>You: {points:get()[viewer.name] or 0}` }
	end,
})

players.joined:connect(function(player)
	board:show(player)
end)

world.regions.single("flag").entered:connect(function(player)
	local current = points:get()
	current[player.name] = (current[player.name] or 0) + 1
	points:set(current)
end)
ui.dialog(options: ui.DialogOptions) -> ui.Dialog
#

Makes a dialog; title and body are live, drawn again for a viewer when what they read changes.

local items = require("@ember/items")
local ui = require("@ember/ui")
local world = require("@ember/world")

-- A shop window. Typed locals keep each element's `kind` exact under strict mode.
local body: { ui.DialogElement } = {
	{ kind = "text", text = "<gray>Spend your coins:" },
	{ kind = "item", item = "minecraft:golden_apple", text = "<yellow>Golden Apple - 5 coins" },
}

-- Each button runs `click` with the player who pressed it. The dialog closes after a click unless
-- the button sets `stay`; a button with no `click` just closes it.
local buttons: { ui.DialogButton } = {
	{
		label = "<green>Buy one",
		stay = true,
		click = function(player)
			if player.inventory:count("coin") < 5 then
				player:send("<red>Not enough coins.")
				return
			end
			player.inventory:remove("coin", 5)
			player.inventory:add(items.create("minecraft:golden_apple"))
		end,
	},
	{ label = "<gray>Close" },
}

local shop = ui.dialog({ title = "<gold>Shop", body = body, buttons = buttons })

-- Open it at the shop counter.
world.regions.single("shop").entered:connect(function(player)
	shop:open(player)
end)
ui.from<T, A...>(changed: types.Signal<A...>, read: () -> T) -> ui.Readable<T>
#

Makes a readable that calls read again each time changed fires: the bridge from host signals to live UI.

local players = require("@ember/players")
local ui = require("@ember/ui")

-- Host state such as an inventory isn't a `ui.value`, so live UI can't see it change by itself.
-- `from` reads it again whenever a signal fires: here each player's coin count, every time their
-- inventory changes.
local boards: { [string]: ui.Sidebar } = {}

players.joined:connect(function(player)
	local inventory = player.inventory
	local coins = ui.from(inventory.changed, function()
		return inventory:count("coin")
	end)
	local board = ui.sidebar({
		title = "<gold><bold>Wallet",
		lines = function()
			return { `<yellow>Coins: {coins:get()}` }
		end,
	})
	board:show(player)
	boards[player.id] = board
end)

-- One sidebar per player, so take theirs down when they leave.
players.left:connect(function(player)
	local board = boards[player.id]
	if board then
		board:remove()
		boards[player.id] = nil
	end
end)
ui.hud(layout: string, elements: { ui.HudElement }, options: { z: number? }?) -> ui.Hud
#

Makes a HUD layer of elements in the regions of a content.hud layout; each text and sprite is live, drawn for each viewer; z orders it among layers.

ui.popups(layout: string, region: string, options: { max: number? }?) -> ui.Popups
#

Makes a popup stack in a region of a content.hud layout, at most max deep; default the region's slots.

ui.textWidth(text: types.MiniMessage) -> number
#

Measures MiniMessage in pixels in the vanilla default font; bold counts one more pixel a glyph.

ui.title(to: types.Player, title: types.MiniMessage, options: types.TitleOptions?)ui.title(to: { types.Player }, title: types.MiniMessage, options: types.TitleOptions?)
#

Shows a title to a player or a list of players.

local ui = require("@ember/ui")
local players = require("@ember/players")

-- Count everyone down, then start. A list of players shows it to all of them at once.
task.spawn(function()
	for seconds = 3, 1, -1 do
		ui.title(players.all(), `<yellow>{seconds}`, { fadeIn = 0, stay = 1, fadeOut = 0 })
		task.wait(1)
	end
	ui.title(players.all(), "<green><bold>GO!", { subtitle = "<gray>Grab the most coins", stay = 2 })
end)
ui.value<T>(initial: T) -> ui.Value<T>
#

Makes a value holding initial; a Live function tracks its top-level keys, and nested tables come back as themselves.

local ui = require("@ember/ui")
local players = require("@ember/players")

-- A value that live UI reads. Setting it redraws every sidebar line that read it.
local timeLeft = ui.value(120)

local board = ui.sidebar({
	title = "<gold><bold>Hide and Seek",
	lines = function()
		return { `Time left: {timeLeft:get()}s` }
	end,
})

players.joined:connect(function(player)
	board:show(player)
end)

task.spawn(function()
	while timeLeft:get() > 0 do
		task.wait(1)
		timeLeft:update(function(old)
			return old - 1
		end)
	end
end)

Types