JRS COREDocs

Menu

Build list menus with rows, sliders and callbacks from the menu module inside jrs_core.

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#

Client

GetMenuData

#
exports.jrs_core:GetMenuData()

Hands you the menu module table. GetMenuAPI and GetMenu return the same table. For a single quick menu you can skip the module and call exports.jrs_core:OpenMenu instead, see Core Shortcuts.

lua
local MenuData = exports.jrs_core:GetMenuData()
ReturnstableThe module: Open, Close, CloseAll, GetOpened, GetOpenedMenus, IsOpen, IsAnyOpen, ReOpen.

Opening A Menu#

Client

Open

#
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.

lua
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)
ArgumentTypeMeaning
menuTypestringRenderer to use. Only 'default' is registered.
namespacestringUsually GetCurrentResourceName(). Falls back to the calling resource.
namestringYour id for this menu inside the namespace. Defaults to 'menu'.
datatableTitle, subtext, alignment and rows. See the menu data table below.
submitfunctionRuns as submit(data, menu) when the player confirms a row. The row is in data.current.
cancelfunctionRuns as cancel(data, menu) when the player goes back. Call menu.close() here if the menu should go away.
changefunctionRuns as change(data, menu) when the highlighted row moves or a slider value changes.
closefunctionRuns with no arguments after the menu has been closed.
Returnstable | nil, stringThe instance. On failure it returns nil plus 'unknown_menu_type' or 'ui_busy:<owner>' when another UI holds the screen.
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.

Client

refresh

#
menu.refresh()

Sends the current data to the screen again. Use it after any of the edit functions below.

lua
menu.setTitle('Saloon (closing soon)')
menu.refresh()
Client

setTitle

#
menu.setTitle(title)

Replaces the header text.

lua
menu.setTitle('Saloon - Happy hour')
menu.refresh()
ArgumentTypeMeaning
titlestringNew header.
Client

setElement

#
menu.setElement(index, key, value)

Writes one field on one row. Does nothing if the row index does not exist.

lua
menu.setElement(1, 'desc', 'Out of stock')
menu.refresh()
ArgumentTypeMeaning
indexnumberRow position, starting at 1.
keystringField to set, for example label or desc.
valueanyNew value for that field.
Client

setElements

#
menu.setElements(elements)

Swaps the whole row list for a new one.

lua
menu.setElements({
    { label = 'Beer', value = 'beer' },
    { label = 'Leave', value = 'leave' },
})
menu.refresh()
ArgumentTypeMeaning
elementstableThe new rows.
Client

update

#
menu.update(query, newData)

Finds every row whose fields equal all fields in query, then copies the fields of newData onto those rows.

lua
menu.update({ value = 'whiskey' }, { label = 'Whiskey (sold out)', disabled = true })
menu.refresh()
ArgumentTypeMeaning
querytableFields to match, for example { value = 'whiskey' }.
newDatatableFields to write on each match.
Client

removeElement

#
menu.removeElement(query)

Deletes every row that matches all fields in query.

lua
menu.removeElement({ value = 'whiskey' })
menu.refresh()
ArgumentTypeMeaning
querytableFields to match.
Client

close

#
menu.close()

Hides this menu, removes it from the open list and then runs your close callback if you gave one.

lua
menu.close()

Module Functions#

These live on the table from GetMenuData. The lookup functions take the same three strings you passed to Open.

Client

Close

#
MenuData.Close(menuType, namespace, name)

Closes one menu by its three ids.

lua
MenuData.Close('default', GetCurrentResourceName(), 'cook_pot')
ArgumentTypeMeaning
menuTypestringUsually 'default'.
namespacestringThe namespace used in Open.
namestringThe menu name used in Open.
Returnsbooleantrue when a menu was found and closed.
Client

CloseAll

#
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.

lua
MenuData.CloseAll()
Client

GetOpened

#
MenuData.GetOpened(menuType, namespace, name)

Looks up an open menu.

lua
local pot = MenuData.GetOpened('default', GetCurrentResourceName(), 'cook_pot')
if pot then
    pot.setTitle('Cook Pot (empty)')
    pot.refresh()
end
ArgumentTypeMeaning
menuTypestringUsually 'default'.
namespacestringThe namespace used in Open.
namestringThe menu name used in Open.
Returnstable | nilThe instance, or nil when that menu is not open.
Client

GetOpenedMenus

#
MenuData.GetOpenedMenus()

Gives the live list of open instances, oldest first. Treat it as read only.

lua
print(('%d menus open'):format(#MenuData.GetOpenedMenus()))
ReturnstableArray of instances.
Client

IsOpen

#
MenuData.IsOpen(menuType, namespace, name)

Checks one menu by its three ids.

lua
if not MenuData.IsOpen('default', GetCurrentResourceName(), 'cook_pot') then
    ExecuteCommand('cookpot')
end
ArgumentTypeMeaning
menuTypestringUsually 'default'.
namespacestringThe namespace used in Open.
namestringThe menu name used in Open.
Returnsbooleantrue while that menu is on screen.
Client

IsAnyOpen

#
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.

lua
if MenuData.IsAnyOpen() then return end
Returnsbooleantrue when at least one menu is open.
Client

ReOpen

#
MenuData.ReOpen(instance)

Opens a closed instance again with its saved data and callbacks.

lua
local saved = MenuData.GetOpened('default', GetCurrentResourceName(), 'cook_pot')
saved.close()
-- later
MenuData.ReOpen(saved)
ArgumentTypeMeaning
instancetableAn instance you kept from Open or GetOpened.
Returnstable | nilThe new instance.

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.

Client

OpenMenu

#
exports.jrs_core:OpenMenu(menuType, namespace, name, data, submit?, cancel?, change?, close?)

Same as Open. menuType defaults to 'default' and namespace defaults to the calling resource. Also on the core table as JRS.OpenMenu.

lua
exports.jrs_core:OpenMenu(nil, nil, 'camp_menu', {
    title = 'Camp',
    elements = { { label = 'Light fire', value = 'fire' } },
}, function(data, menu) menu.close() end)
ArgumentTypeMeaning
namestringMenu id.
datatableMenu data, as above.
Returnstable | nil, stringThe instance, or nil and 'menu_unavailable' when the module is not loaded.
Client

CloseMenu

#
exports.jrs_core:CloseMenu(menuType, namespace, name)

Same as Close, with the same defaults as OpenMenu.

lua
exports.jrs_core:CloseMenu(nil, nil, 'camp_menu')
Returnsbooleantrue when a menu was closed.
Client

CloseAllMenus

#
exports.jrs_core:CloseAllMenus()

Same as CloseAll.

lua
exports.jrs_core:CloseAllMenus()
Returnsbooleanfalse only when the module is missing.
Client

IsAnyMenuOpen

#
exports.jrs_core:IsAnyMenuOpen()

Same as IsAnyOpen.

lua
local busy = exports.jrs_core:IsAnyMenuOpen()
Returnsbooleantrue when a menu is up.
Client

GetMenu

#
exports.jrs_core:GetMenu()

Returns the same module table as GetMenuData. Also on the core table as JRS.GetMenu.

lua
local MenuData = exports.jrs_core:GetMenu()
ReturnstableThe menu module.

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.

Events#

Client

jrs_core:menu:opened

#
AddEventHandler('jrs_core:menu:opened', function() end)

Local event raised when the first menu appears after none were open. Use it to hide your own HUD or pause a prompt loop.

lua
AddEventHandler('jrs_core:menu:opened', function()
    showCampPrompt = false
end)
Client

jrs_core:menu:closed

#
AddEventHandler('jrs_core:menu:closed', function() end)

Local event raised when the last open menu goes away.

lua
AddEventHandler('jrs_core:menu:closed', function()
    showCampPrompt = true
end)