The menu module ships inside jrs_core (modules/menu) and runs on the client only. There is no menu resource to start and no extra dependency line: if your script already talks to jrs_core, menus are there. Every Open hands back an instance table you can edit and redraw while it is on screen, and the menu holds the mouse and keyboard focus until it closes.
Getting The Module#
Opening A Menu#
MenuData.Open(menuType, namespace, name, data, submit?, cancel?, change?, close?)Draws a menu and returns its instance. A menu is identified by the three strings menuType, namespace and name. If one with the same three strings is already up, it is closed first, so calling Open again simply rebuilds it.
A camp cook pot opened from a chat command. The portion row is an options slider, and the change callback rewrites the price line every time the player steps it.
local MenuData = exports.jrs_core:GetMenuData()
local PORTIONS = { 'One bowl', 'Two bowls', 'The whole pot' }
local PRICES = { 0.50, 0.90, 2.00 }
RegisterCommand('cookpot', function()
local pot, why = MenuData.Open('default', GetCurrentResourceName(), 'cook_pot', {
title = 'Cook Pot',
subtext = 'Rabbit stew',
align = 'center',
elements = {
{ label = 'Portion', type = 'slider', options = PORTIONS, value = 0, desc = 'Price $0.50' },
{ label = 'Serve it', value = 'serve' },
},
}, function(data, menu)
if data.current.value ~= 'serve' then return end
local size = menu.data.elements[1].value + 1 -- options count from 0
TriggerServerEvent('my_camp:serveStew', size)
menu.close()
end, function(_, menu)
menu.close()
end, function(_, menu)
local size = menu.data.elements[1].value + 1
menu.setElement(1, 'desc', ('Price $%.2f'):format(PRICES[size]))
menu.refresh()
end)
if not pot then
print('cook pot not shown: ' .. why) -- e.g. ui_busy:<owner>
end
end, false)| Argument | Type | Meaning |
|---|---|---|
menuType | string | Renderer to use. Only 'default' is registered. |
namespace | string | Usually GetCurrentResourceName(). Falls back to the calling resource. |
name | string | Your id for this menu inside the namespace. Defaults to 'menu'. |
data | table | Title, subtext, alignment and rows. See the menu data table below. |
submit | function | Runs as submit(data, menu) when the player confirms a row. The row is in data.current. |
cancel | function | Runs as cancel(data, menu) when the player goes back. Call menu.close() here if the menu should go away. |
change | function | Runs as change(data, menu) when the highlighted row moves or a slider value changes. |
close | function | Runs with no arguments after the menu has been closed. |
nil plus 'unknown_menu_type' or 'ui_busy:<owner>' when another UI holds the screen.Menu Data#
| Field | Type | What it does |
|---|---|---|
title |
string | Header text. |
subtext |
string | Small line under the header. |
align |
string | Screen side. Anything with right sits right, center, top or bottom sits in the middle, the rest sits left. Default top-left. |
elements |
table | List of rows, top to bottom. An empty list shows an empty-state line. |
Row Fields#
| Field | Type | What it does |
|---|---|---|
label |
string | Row text. title is read when label is missing. |
value |
any | Your data. Read it back as data.current.value. For sliders it holds the current number. |
desc |
string | Text in the description box while the row is highlighted. |
type |
string | 'default' or 'slider'. Missing means 'default'. |
min, max, hop |
number | Slider range and step. min defaults to 0, hop to 1, max has no limit. |
options |
table | Slider labels. The slider shows options[value] with value counted from 0, so 0 is the first entry and the range runs to the list size minus one. Entries can be strings or tables with label. |
image |
string | Icon on the left. A bare name loads that item picture from jrs_inventory; a path or URL is used as is. img also works. |
disabled |
boolean | Greys the row out and blocks confirm. isNotSelectable and unselectable do the same. |
selected |
boolean | Highlights this row when the menu opens. |
Player text: the title, subtext, row label and description are inserted as HTML. Escape anything a player typed before it goes into a menu.
Changing An Open Menu#
Open returns an instance, and the same instance is passed as menu to every callback. The edit functions only change the data in memory. Call refresh afterwards to redraw.
menu.refresh()Sends the current data to the screen again. Use it after any of the edit functions below.
menu.setTitle('Saloon (closing soon)')
menu.refresh()menu.setTitle(title)Replaces the header text.
menu.setTitle('Saloon - Happy hour')
menu.refresh()| Argument | Type | Meaning |
|---|---|---|
title | string | New header. |
menu.setElement(index, key, value)Writes one field on one row. Does nothing if the row index does not exist.
menu.setElement(1, 'desc', 'Out of stock')
menu.refresh()| Argument | Type | Meaning |
|---|---|---|
index | number | Row position, starting at 1. |
key | string | Field to set, for example label or desc. |
value | any | New value for that field. |
menu.setElements(elements)Swaps the whole row list for a new one.
menu.setElements({
{ label = 'Beer', value = 'beer' },
{ label = 'Leave', value = 'leave' },
})
menu.refresh()| Argument | Type | Meaning |
|---|---|---|
elements | table | The new rows. |
menu.update(query, newData)Finds every row whose fields equal all fields in query, then copies the fields of newData onto those rows.
menu.update({ value = 'whiskey' }, { label = 'Whiskey (sold out)', disabled = true })
menu.refresh()| Argument | Type | Meaning |
|---|---|---|
query | table | Fields to match, for example { value = 'whiskey' }. |
newData | table | Fields to write on each match. |
menu.removeElement(query)Deletes every row that matches all fields in query.
menu.removeElement({ value = 'whiskey' })
menu.refresh()| Argument | Type | Meaning |
|---|---|---|
query | table | Fields to match. |
menu.close()Hides this menu, removes it from the open list and then runs your close callback if you gave one.
menu.close()Module Functions#
These live on the table from GetMenuData. The lookup functions take the same three strings you passed to Open.
MenuData.Close(menuType, namespace, name)Closes one menu by its three ids.
MenuData.Close('default', GetCurrentResourceName(), 'cook_pot')| Argument | Type | Meaning |
|---|---|---|
menuType | string | Usually 'default'. |
namespace | string | The namespace used in Open. |
name | string | The menu name used in Open. |
MenuData.CloseAll()Closes every open menu from every resource, newest first, and runs each close callback. Menus stack, so a second Open with a different name sits on top of the first. Open with the same three ids replaces the old one by itself, so you only need this when you want to clear menus you did not open, for example before a cutscene.
MenuData.CloseAll()MenuData.GetOpened(menuType, namespace, name)Looks up an open menu.
local pot = MenuData.GetOpened('default', GetCurrentResourceName(), 'cook_pot')
if pot then
pot.setTitle('Cook Pot (empty)')
pot.refresh()
end| Argument | Type | Meaning |
|---|---|---|
menuType | string | Usually 'default'. |
namespace | string | The namespace used in Open. |
name | string | The menu name used in Open. |
MenuData.IsOpen(menuType, namespace, name)Checks one menu by its three ids.
if not MenuData.IsOpen('default', GetCurrentResourceName(), 'cook_pot') then
ExecuteCommand('cookpot')
end| Argument | Type | Meaning |
|---|---|---|
menuType | string | Usually 'default'. |
namespace | string | The namespace used in Open. |
name | string | The menu name used in Open. |
MenuData.IsAnyOpen()Tells you if any menu at all is up. Handy to skip your own prompts or key binds while the player is in a menu.
if MenuData.IsAnyOpen() then return endMenuData.ReOpen(instance)Opens a closed instance again with its saved data and callbacks.
local saved = MenuData.GetOpened('default', GetCurrentResourceName(), 'cook_pot')
saved.close()
-- later
MenuData.ReOpen(saved)| Argument | Type | Meaning |
|---|---|---|
instance | table | An instance you kept from Open or GetOpened. |
RegisterControls, UnregisterControls and IsInputActive are also on the table so older scripts do not crash. They do nothing; IsInputActive always returns false.
Core Shortcuts#
The getCoreApi() client table and plain jrs_core exports wrap the same module, so you can skip GetMenuData for simple cases.
Input And Pause Menu#
The player can drive a menu with the mouse or the keyboard:
| Action | Keys |
|---|---|
| Move up / down | Arrow keys, W / S, mouse |
| Change a slider | Left / Right, A / D, or click the arrows |
| Confirm | Enter, Space, or the Select button |
| Back | Backspace, Escape, or the Back button |
| Jump | Home / End, Page Up / Page Down (5 rows) |
When the pause menu opens, every open menu is closed and remembered. Once the pause menu is gone they are opened again with ReOpen, so your callbacks keep working. If another dialog such as a text input takes the mouse, the menu takes it back after that dialog closes.