Problem when refreshing a menu while deleting its items.
s1nyx opened this issue · 8 comments
Hi,
I've noticed a bug when I clear the items of a menu and re-add the buttons. If I do that 2 or 3 times It seems like the labels buttons "switch places". For a bit of context, I'm using this code in an admin menu to create a button for each connected player. So I make it refresh each time I come back to the menu named subMenu.
The piece of my code with the bug:
`
local subMenu = MenuV:CreateMenu("test", "", "topleft", 255, 0, 0, 'size-125', 'default', 'menuv', 'stl_test', 'native')
subMenu:On("open", function (subMenu)
subMenu:ClearItems()
ESX.TriggerServerCallback('stl:test:getAllPlayers', function(players)
for k, player in pairs(players) do
local item = subMenu:AddButton({ label = player.name .. " [ID: " .. k .. "]", value = targetMenu})
item:On("select", function(item)
print(item.Label)
end)
end
end)
end)
`
So, every other time when I select a button the label print belongs to another button.
You can also add an event
while creating an item, like:
subMenu:On("open", function (subMenu)
ESX.TriggerServerCallback('stl:test:getAllPlayers', function(players)
subMenu:ClearItems()
for k, player in pairs(players) do
subMenu:AddButton({ label = ('%s [ID: %s]'):format(player.name, k), value = targetMenu, select = function(i)
print(i.Label)
end })
end
end)
end)
Won't recommend naming a parameter same as a "global/local" defined item, can you try the above code and check if your issue still occur?
Yes I know, i was just trying different approaches to see if i could fix it by any way. The issue still occur with your code.
I also noticed that when the problem arises, the active button is not the first (at the very top) but the second. (I only had time to test for 2 entries, I would test on more tomorrow to see if it's still the second or if it's random pick).
Which version are you using, because your issue seems like issue #4 , was fixed in version 1.3
I also made an similar list and had 0 issues https://streamable.com/4sd59b
I use the last version 1.3 and i've tried to re-install it but it was the same. Here is a video that demonstrate the bug : https://streamable.com/3o3fim
I'll see if I can reproduce your issue, I'll get back to you
Right now it seems like calling :ClearItems()
before a TriggerServerCallback
cause the issue
calling :ClearItems()
inside the TriggerServerCallback
Will also make changes to MenuV to solve this problem in the future.
Resource used to reproduce the issue:
menuv_example.zip
I've just test it, works perfectly.
Thanks for taking the time to solve this issue.
Your issue will be resolved in the next release 1.4 (coming end this week)