MenuPage
A tab of a menu window: adding headers, texts, toggles, sliders, lists and buttons.
Tabs are created with menu:addPage. In the examples page is any MenuPage.
Every add... method returns the new control - keep it in a variable to react to the player
(control:onChange, see each method's example) or to read and change it later (getValue, setValue).
Methods
addHeader
page:addHeader(text)Adds a section header - a title separating groups of controls, like the headers in the game's Settings.
| Parameter | Type | Description |
|---|---|---|
text | string | Text of the header. |
Returns: MenuControl | nil - The header, or nil when the tab was removed.
addText
page:addText(text)Adds a paragraph of text (wrapped to the width of the tab; \n starts a new line) - rules,
information, help. The text can be changed later with setText.
| Parameter | Type | Description |
|---|---|---|
text | string | The text (UTF-8). |
Returns: MenuControl | nil - The text, or nil when the tab was removed.
addToggle
page:addToggle(text, value, options?)Adds an on/off switch. getValue returns a boolean; onChange gets the new value when the player
switches it.
local toggle = page:addToggle("Lorem ipsum", true, { save = "loremToggle" })
toggle:onChange(function(on)
if on then
addChatMessage("Lorem ipsum: on")
else
addChatMessage("Lorem ipsum: off")
end
end)| Parameter | Type | Description |
|---|---|---|
text | string | Name shown in the row. |
value | boolean | Initial value (and the value restored by the window's RESET button). |
options (optional) | MenuControlOptions | Description, remembered value (save), enabled state. |
Returns: MenuControl | nil - The switch, or nil when the tab was removed.
addSlider
page:addSlider(text, value, min, max, options?)Adds a slider for a number between min and max. getValue returns a number; onChange gets
the new number at every step the player moves it.
local slider = page:addSlider("Dolor sit amet", 50, 0, 100, { step = 5 })
slider:onChange(function(value)
triggerServerEvent("loremSlider", value) -- the server gets it with addEventHandler("loremSlider", function(pid, value) ... end)
end)| Parameter | Type | Description |
|---|---|---|
text | string | Name shown in the row. |
value | number | Initial value (clamped to min-max; also the value restored by RESET). |
min | number | Lowest value. |
max | number | Highest value (greater than min). |
options (optional) | MenuControlOptions | step sets the step of the slider. |
Returns: MenuControl | nil - The slider, or nil when the tab was removed.
addList
page:addList(text, items, selected?, options?)Adds a choice from a list of options, shown with arrows (or as a drop-down with dropdown = true).
getValue returns the index of the selected item (1 = the first one); onChange gets the new index.
local items = { "Lorem", "Ipsum", "Dolor" }
local list = page:addList("Consectetur", items, 1)
list:onChange(function(index)
addChatMessage("Consectetur: " .. items[index]) -- index -> the item's name
end)| Parameter | Type | Description |
|---|---|---|
text | string | Name shown in the row. |
items | string[] | Names of the options (1-64). |
selected (optional) | integer | Index of the initially selected option. Default 1. |
options (optional) | MenuControlOptions | Description, remembered value (save), enabled state, drop-down. |
Returns: MenuControl | nil - The list, or nil when the tab was removed.
addButton
page:addButton(text, action, options?)Adds a button. Clicking it calls the function, or opens the given window - a submenu.
page:addButton("Lorem ipsum", submenu) -- opens another window
page:addButton("Dolor sit amet", function()
triggerServerEvent("loremIpsum") -- runs on every click
closeMenu() -- and back to the game
end)| Parameter | Type | Description |
|---|---|---|
text | string | Text of the button. |
action | fun() | Menu | Function called on click (no arguments), or a window to open. |
options (optional) | MenuControlOptions | Only enabled applies. |
Returns: MenuControl | nil - The button, or nil when the tab was removed.
setTitle
page:setTitle(title)Changes the name of the tab. It is shown the next time the window opens.
| Parameter | Type | Description |
|---|---|---|
title | string | New name. |
Returns: boolean - false when the tab was removed.
remove
page:remove()Removes the tab with all its controls. A tab of an open window disappears the next time it opens.
Returns: boolean - false when it was already removed.
getMenu
page:getMenu()Returns the window the tab belongs to.
Returns: Menu | nil - nil when the tab was removed.
isValid
page:isValid()Tells whether the tab still exists.
Returns: boolean
Types
MenuControlOptions
Options of the add... methods. All fields are optional.
| Field | Type | Description |
|---|---|---|
description? | string | Text in the description panel on the right while the control is selected (hovered with the mouse, focused with the keyboard or a gamepad). |
save? | string | Key under which the value is remembered on this computer, separately for every server (1-64 characters, no = or line breaks). The next time the control is created with the same key it starts with the remembered value instead of the given one. Toggles, sliders and lists only. |
step? | number | Slider step (greater than 0). Default 1. |
enabled? | boolean | false = the control starts greyed out (see MenuControl:setEnabled). Default true. |
dropdown? | boolean | List only: true = a drop-down list instead of arrows. Default false. |
