@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.CanvasElementMakes a canvas width pixels wide and lines lines tall; draw places what it shows.
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.DialogMakes 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)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.HudMakes 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.
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) -> numberMeasures 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)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
- ui.CanvasA grid of 9-pixel lines that a
ui.canvasdraw function places text and images on, at a line and a pixel x. - ui.CanvasElementA canvas made by
ui.canvas, placed in a dialog's body. - ui.CanvasImageOptionsHow a canvas image is drawn.
- ui.CanvasSkinOptionsHow a canvas skin is drawn.
- ui.CanvasTextOptionsHow canvas text is placed.
- ui.DialogA dialog drawn for each player it is open for.
- ui.DialogButtonA button under a dialog's body.
- ui.DialogElementOne element of a dialog's body: text, at most
widthpixels wide when given; an item, clickable whenclickis set; or aui.canvas. - ui.DialogOptionsWhat a dialog shows.
- ui.HudHUD elements drawn for each player it is shown to.
- ui.HudElementOne element of a HUD in a region of its layout: text in a HUD font, coloured by its MiniMessage and otherwise unstyled, or one cell of a HUD image; x and y are pixels inside the region.
- ui.LiveA constant, a value, or a function of the viewer that runs again when what it read changes.
- ui.PopupA popup that slides into a stack.
- ui.PopupsA stack of popups in a HUD region with slots; the oldest goes when it is full.
- ui.ReadableSomething live UI can read; reading it inside a live function redraws that UI when it changes.
- ui.SharedWhat every viewer sees alike: a constant, a value, or a function that runs again when what it read changes.
- ui.SidebarA sidebar drawn for each player it is shown to.
- ui.ValueState live UI reads.