warxander / warmenu

FiveM Lua Menu Framework
https://forum.cfx.re/t/free-warmenu-lua-menu-framework
ISC License
44 stars 31 forks source link

Dynamically create submenus #19

Closed matsn0w closed 11 months ago

matsn0w commented 2 years ago

Hey there,

I am creating a menu with your framework - it's working great so far!

One thing I am struggling with is the following: I want my menu resource to be 'dynamic'. This means that the server owner can define a menu structure using a config file. Each menu item is created through a function which defines the title, action and optionally a submenu it belongs to.

The menu structure is defined as follows:

SubMenus = {
    { id = 'vehicles', title = 'Vehicle options' },
    { id = 'ped', title = 'Ped options' },
}

Menu entries ('commands') are created using this function:

-- parms: entry title, id, submenu id, action
AddCommandToMenu('Spawn blista', 'spawn_blista', 'vehicles', function ()
    -- action code here...
end)

I want to loop over all submenus and dynamically create MenuButtons for them. In your example, you are using an if-elseif statement with the Begin() method. That asumes that you already now the menu structure beforehand, which is not the case for me.

Do you have an idea how my goal can be achieved? Maybe some change is needed? Am I missing something?

Thank you in advance, matsn0w

warxander commented 11 months ago

Hi!

What's the exact problem you have?

As it's mostly the question of defining a proper menu config structure. Send configs to a client, create all menus once and then use these configs to draw anything you need.

Abstract example below:

function displayMenuFromConfig(menuConfig)
  WarMenu.Begin(menuConfig.id)
  for _, control in ipairs(menuConfig.controls) do
    if control.button then
      if WarMenu.Button(control.button.text, control.button.subText) then
        if control.button.spawn then
          -- Spawn something
        end
       end
     end
  end
  WarMenu.End()
end