ui

Windows, buttons, sliders, tabs, tables: Dear ImGui for mods, and the mod menu.

Mods build menus with Dear ImGui widgets. Widgets go inside ui.window(title, fn) (a window drawn in the draw event) or inside ui.panel(title, fn) (a tab of the mod menu, F1). ImGui is immediate mode: your function runs every frame and draws the widgets anew; a widget's return value tells you what the player did this frame. Keep your state in your own variables:

local god = false
ui.panel("Cheats", function()
  god = ui.checkbox("God mode", god)
  world.god(god)
  if ui.button("Kill everything") then world.killAll() end
end)

During a hunt the mouse turns the view; a window the player should click needs ui.cursor(true) (shows the cursor, pauses mouse look) and ui.cursor(false) afterwards. Window titles are kept apart per mod automatically. Widgets with the same label in one window need ui.id or a "label##unique" suffix (ImGui shows the part before ##).

Functions

# ui.window(title, fn, options?) → boolean

A window. Call it from a draw handler every frame it should show.

ParameterType
titlestringwindow title (also its identity)
fnfunctiondraws the widgets
options optionaltablex, y, w, h (first placement; with fixed = true every frame), alpha, closable, noTitle, noResize, noMove, noScroll, autoResize, noBackground, noInput, noCollapse, noFocus
Returns
booleanfalse when the player closed it (closable windows)
local shopOpen = true
on("draw", function()
  if not shopOpen then return end
  shopOpen = ui.window("Shop", function()
    ui.text("Credits: 500")
    if ui.button("Buy ammo") then buyAmmo() end
  end, {x = 40, y = 120, w = 260, h = 160, closable = true})
end)

# ui.panel(title, fn)

Adds a tab to the mod menu (F1). Call once (e.g. at load); the function runs every frame the tab is shown. Calling again with the same title replaces it.

ParameterType
titlestringtab title
fnfunctiondraws the tab's widgets

# ui.removePanel(title)

Removes one of your mod menu tabs.

ParameterType
titlestring

# ui.cursor(on)

Shows the mouse cursor during a hunt (mouse look pauses) so your windows can be clicked.

ParameterType
onboolean

# ui.toast(text, seconds?, color?)

A short message at the bottom right.

ParameterType
textstring
seconds optionalnumberdefault 4
color optionalColor

# ui.menuOpen() → boolean

Whether the mod menu (F1) is open.

Returns
boolean

# ui.screen() → number, number

The window size in UI units.

Returns
numberwidth
numberheight

# ui.text(...)

A line of text.

ParameterType
...anyvalues joined by spaces
ui.text("Health:", math.floor(c.health))

# ui.textColored(color, text)

Coloured text.

ParameterType
colorColor
textstring

# ui.textWrapped(text)

Text wrapped at the window edge.

ParameterType
textstring

# ui.textDisabled(text)

Greyed-out text (hints).

ParameterType
textstring

# ui.bullet(text)

A bullet point.

ParameterType
textstring

# ui.heading(text)

A separator line with a title.

ParameterType
textstring

# ui.button(label, w?, h?) → boolean

A button.

ParameterType
labelstring
w optionalnumberwidth
h optionalnumberheight
Returns
booleantrue the frame it was clicked

# ui.smallButton(label) → boolean

A small button that fits into a line of text.

ParameterType
labelstring
Returns
booleanclicked

# ui.checkbox(label, value) → boolean, boolean

A checkbox.

ParameterType
labelstring
valuebooleancurrent value
Returns
booleanthe value (changed when clicked)
booleantrue when it changed
enabled = ui.checkbox("Enabled", enabled)

# ui.radio(label, active) → boolean

A radio button.

ParameterType
labelstring
activebooleanwhether this option is the chosen one
Returns
booleanclicked
if ui.radio("Easy", level == 1) then level = 1 end
ui.sameLine()
if ui.radio("Hard", level == 2) then level = 2 end

# ui.slider(label, value, min, max, format?) → number, boolean

A slider for numbers.

ParameterType
labelstring
valuenumber
minnumber
maxnumber
format optionalstringprintf format, default "%.2f"
Returns
numberthe value
booleanchanged
speed = ui.slider("Speed", speed, 0.5, 3)

# ui.sliderInt(label, value, min, max) → integer, boolean

A slider for whole numbers.

ParameterType
labelstring
valueinteger
mininteger
maxinteger
Returns
integervalue
booleanchanged

# ui.drag(label, value, speed?, min?, max?) → number, boolean

A number changed by dragging.

ParameterType
labelstring
valuenumber
speed optionalnumberper pixel dragged (default 1)
min optionalnumber
max optionalnumber
Returns
numbervalue
booleanchanged

# ui.input(label, text?, hint?) → string, boolean

A text field (up to 4095 bytes).

ParameterType
labelstring
text optionalstringcurrent text
hint optionalstringgrey text while empty
Returns
stringthe text
booleanchanged

# ui.inputMultiline(label, text?, w?, h?) → string, boolean

A multi-line text field (16 KB).

ParameterType
labelstring
text optionalstring
w optionalnumber
h optionalnumber
Returns
stringtext
booleanchanged

# ui.inputNumber(label, value, step?) → number, boolean

A number field.

ParameterType
labelstring
valuenumber
step optionalnumber+/- buttons step (default 1)
Returns
numbervalue
booleanchanged

# ui.inputInt(label, value, step?) → integer, boolean

A whole-number field.

ParameterType
labelstring
valueinteger
step optionalinteger
Returns
integervalue
booleanchanged

# ui.combo(label, index, items) → integer, boolean

A drop-down list.

ParameterType
labelstring
indexintegerthe chosen item, 1-based
itemstablea list of strings
Returns
integerthe chosen index
booleanchanged
mode = ui.combo("Mode", mode, {"Classic", "Survival", "Arena"})

# ui.selectable(label, selected?) → boolean

A clickable line (lists).

ParameterType
labelstring
selected optionalbooleanshown highlighted
Returns
booleanclicked

# ui.color(label, color) → table, boolean

A colour picker.

ParameterType
labelstring
colorColorcurrent colour
Returns
table{r, g, b, a} 0-1
booleanchanged

# ui.progress(fraction, label?, w?)

A progress bar.

ParameterType
fractionnumber0-1
label optionalstringtext on the bar
w optionalnumberwidth

# ui.separator()

A horizontal line.

# ui.sameLine(offset?, spacing?)

The next widget goes on the same line.

ParameterType
offset optionalnumberx position
spacing optionalnumber

# ui.spacing()

A little vertical space.

# ui.newLine()

An empty line.

# ui.indent(w?)

Indents the following widgets.

ParameterType
w optionalnumber

# ui.unindent(w?)

Undoes ui.indent.

ParameterType
w optionalnumber

# ui.dummy(w, h)

Empty space of a size.

ParameterType
wnumber
hnumber

# ui.collapsing(label, open?) → boolean

A collapsible section header.

ParameterType
labelstring
open optionalbooleanopen at first
Returns
booleantrue while open
if ui.collapsing("Advanced") then
  debug = ui.checkbox("Debug", debug)
end

# ui.tree(label, fn, open?) → boolean

A tree node.

ParameterType
labelstring
fnfunctiondraws the children
open optionalbooleanopen at first
Returns
booleanopen

# ui.tabs(id, fn)

A tab bar.

ParameterType
idstring
fnfunctioncalls ui.tab for each tab
ui.tabs("main", function()
  ui.tab("Stats", function() ui.text("Kills:", kills) end)
  ui.tab("Options", function() loud = ui.checkbox("Loud", loud) end)
end)

# ui.tab(label, fn) → boolean

One tab, inside ui.tabs.

ParameterType
labelstring
fnfunctiondraws the tab
Returns
booleantrue while this tab is shown

# ui.table(id, columns, fn, headers?)

A table.

ParameterType
idstring
columnsinteger1-64
fnfunctionfills it with ui.nextRow / ui.nextColumn
headers optionaltablecolumn titles
ui.table("dinos", 2, function()
  for _, c in ipairs(world.creatures()) do
    ui.nextRow(); ui.text(c.name)
    ui.nextColumn(); ui.text(math.floor(c.distance))
  end
end, {"Species", "Distance"})

# ui.nextRow()

Starts a table row (and its first cell).

# ui.nextColumn() → boolean

Moves to the next cell of the row.

Returns
booleanwhether the cell is visible

# ui.child(id, w?, h?, fn, border?)

A scrolling region inside a window.

ParameterType
idstring
w optionalnumber0 = all
h optionalnumber0 = all
fnfunction
border optionalboolean

# ui.group(fn)

Groups widgets (so ui.sameLine treats them as one).

ParameterType
fnfunction

# ui.id(id, fn)

Widgets inside get a separate identity (loops with repeated labels).

ParameterType
idany
fnfunction
for i, item in ipairs(items) do
  ui.id(i, function()
    if ui.button("Buy") then buy(item) end
  end)
end

# ui.popup(id, fn) → boolean

A popup, opened with ui.openPopup.

ParameterType
idstring
fnfunction
Returns
booleanopen

# ui.openPopup(id)

Opens a popup.

ParameterType
idstring

# ui.closePopup()

Closes the popup the code runs in.

# ui.tooltip(text)

A tooltip for the last widget when hovered.

ParameterType
textstring

# ui.hovered() → boolean

Whether the mouse is over the last widget.

Returns
boolean

# ui.clicked() → boolean

Whether the last widget was clicked.

Returns
boolean

# ui.width(w)

Width of the next widget.

ParameterType
wnumber

# ui.scale(factor)

Text size of the current window.

ParameterType
factornumber

# ui.available() → number, number

Space left in the window.

Returns
numberwidth
numberheight

# ui.image(file, w?, h?) → boolean

An image as a widget.

ParameterType
filestringa PNG or TGA file of your mod (path in its folder), or a game file by name
w optionalnumberwidth (default: the image's)
h optionalnumberheight (default: keeps the shape)
Returns
booleanfalse when the image could not be loaded (logged once)
ui.image("icons/rex.png", 64)