JRS CORE文档
API 参考

menu

The JRS Core menu, themed like the stores, part of jrs_core

Note

Client side

The menu is built into jrs_core (modules/menu). No extra resource is needed. Old scripts that call exports.jrs_menu:GetMenuData() keep working because jrs_core provides jrs_menu.

GetMenuData#

GetMenuData#

Returns the menu API table.

returntable

The menu API: Open, Close, CloseAll, GetOpened, GetOpenedMenus, IsOpen, IsAnyOpen, ReOpen.

lua
local MenuData = exports.jrs_core:GetMenuData()

Open#

Open#

Opens a menu and returns the menu instance (see Instance functions). Opening a menu with the same type, namespace and name closes the old one first.

typestringrequired

Always "default".

namespacestringrequired

Your resource name, GetCurrentResourceName().

namestringrequired

A unique name for this menu inside your resource.

datatablerequired

The menu data: title, subtext, align, elements (see Menu data).

submitfunction

Called with (data, menu) when the player presses Enter on an element. data.current is the selected element.

cancelfunction

Called with (data, menu) when the player presses Backspace. Close the menu here with menu.close().

changefunction

Called with (data, menu) when a slider value changes.

closefunction

Called when the menu is closed.

returntable | nil

The menu instance, or nil and a reason (ui_busy:<owner>, unknown_menu_type) when it could not open.

lua
local MenuData = exports.jrs_core:GetMenuData()

MenuData.CloseAll()
MenuData.Open('default', GetCurrentResourceName(), 'shop_menu', {
    title = 'General Store',
    subtext = 'Choose an option',
    align = 'top-left',
    elements = {
        { label = 'Buy bread', value = 'bread', desc = '$0.50' },
        { label = 'Amount', value = 1, type = 'slider', min = 1, max = 10, hop = 1 },
    },
}, function(data, menu)
    if data.current.value == 'bread' then
        print('bought bread')
    end
end, function(data, menu)
    menu.close()
end)

Instance functions#

The table returned by Open (and passed as the second argument of your callbacks).

close#

Closes this menu and calls your close callback.

lua
menu.close()
refresh#

Redraws the menu after you changed the data.

lua
menu.setTitle('New title')
menu.refresh()
setTitle#
titlestringrequired

The new title.

lua
menu.setTitle('New title')
setElement#

Changes one field of one row, then call refresh.

indexnumberrequired

Row index, starting at 1.

keystringrequired

The field name, for example label.

valueanyrequired

The new value.

lua
menu.setElement(1, 'label', 'Sold out')
menu.refresh()
setElements#

Replaces all rows.

lua
menu.setElements({ { label = 'Back', value = 'back' } })
menu.refresh()
update#

Updates every row that matches the query.

querytablerequired

Fields a row must have, for example { value = 'bread' }.

newDatatablerequired

Fields to set on the matching rows.

lua
menu.update({ value = 'bread' }, { label = 'Bread (sold out)' })
menu.refresh()
removeElement#

Removes every row that matches the query.

lua
menu.removeElement({ value = 'bread' })
menu.refresh()
CloseAll#

Closes every open menu.

lua
MenuData.CloseAll()
Close#
typestringrequired

"default".

namespacestringrequired

The resource name.

namestringrequired

The menu name.

lua
MenuData.Close('default', GetCurrentResourceName(), 'shop_menu')
IsOpen#
lua
local open = MenuData.IsOpen('default', GetCurrentResourceName(), 'shop_menu')
IsAnyOpen#
lua
if MenuData.IsAnyOpen() then return end
GetOpened#

Returns the instance of an open menu, or nil.

lua
local menu = MenuData.GetOpened('default', GetCurrentResourceName(), 'shop_menu')
GetOpenedMenus#

Returns the list of all open menu instances.

lua
local list = MenuData.GetOpenedMenus()
ReOpen#

Opens an instance again with its old data and callbacks. The menu does this itself after the pause menu closes.

lua
MenuData.ReOpen(menu)

Controls#

The menu reads the keyboard itself: arrows move, Enter selects, Backspace goes back. The pause menu closes open menus and brings them back afterwards.

RegisterControls and UnregisterControls exist for compatibility with older scripts and do nothing.

Events#

Fired on the client when the first menu opens and when the last one closes. Useful for pausing your own prompts.

lua
AddEventHandler('jrs_menu:openmenu', function() end)
AddEventHandler('jrs_menu:closemenu', function() end)
JRS Core · 免费开源的 RedM 框架 · JRS TEAM 出品