Dialogs and the HUD
The code on this page comes from the demo resource and is covered by the server's tests.
A list dialog
dlmp.show_dialog opens a dialog for one player and calls your callback with the button pressed and the item selected. This is the demo's city menu, shown when a player connects and on /city:
local function city_menu(player)
local items = {}
for i, city in ipairs(CITIES) do
items[i] = city.name
end
local spec = { kind = "list", title = "Choose a city", items = items, button1 = "Spawn", button2 = "Random" }
report(
player,
dlmp.show_dialog(player, spec, function(who, button, item)
go_to_city(who, button == 1 and CITIES[item] or CITIES[math.random(#CITIES)])
end)
)
endbuttonis1for the first button and2for the second button or Esc;itemis the selected row, starting at 1.- The callback receives the player as its first argument (
whohere); use it rather than the handle you opened the dialog with. - A player sees one dialog at a time: a new dialog replaces the current one, and
dlmp.hide_dialogcloses it.
Input, password, table and form dialogs use the same call with a different kind; see DialogSpec. Menus, offers and resource views with your own HTML are documented in Dialogs, menus and views.
Status lines and actions
A HUD widget can target one player or dlmp.root for everyone. The demo shows a server-wide total, a personal line for each player and registers a key that players can rebind:
-- The personal status line; the total above it belongs to the root.
local function show_score(player)
local id = player:id()
dlmp.set_status_line(player, 2, { label = "You", text = string.format("%d / %d", kills[id] or 0, deaths[id] or 0) })
end
dlmp.add_event_handler("resource_start", dlmp.resource_root, function()
dlmp.register_action("demo_score", "Demo: score (hold)", "O")
dlmp.set_status_line(dlmp.root, 1, { label = "Total kills", text = "0" })
end)
dlmp.add_event_handler("player_connect", dlmp.root, function(player)
kills[player:id()] = 0
deaths[player:id()] = 0
show_score(player)
end)dlmp.register_action declares an action and its default key; a player_action handler then receives press and release events for it.
The kill feed
player_death passes the killer, or nil when no player caused the death, as in a fall:
-- The killer is nil for an unattributed death, e.g. a fall.
dlmp.add_event_handler("player_death", dlmp.root, function(player, killer, weapon)
local id = player:id()
deaths[id] = (deaths[id] or 0) + 1
show_score(player)
dlmp.add_kill_feed(dlmp.root, { killer = killer, victim = player, cause = weapon })
if killer then
local kid = killer:id()
kills[kid] = (kills[kid] or 0) + 1
total_kills = total_kills + 1
dlmp.set_status_line(dlmp.root, 1, { label = "Total kills", text = tostring(total_kills) })
show_score(killer)
killer:send_message(string.format("You killed %s (weapon %d)", player:name(), weapon))
player:send_message(string.format("Killed by %s", killer:name()))
end
end)The other widgets (announcements, countdowns, progress bars, notifications and gauges) and the styling of the built-in UI are documented in HUD and theme.
