ProjectSky Posted January 7, 2022 Share Posted January 7, 2022 (edited) try load 300+ context menu every right click mouse game freeze ISContextMenu seems to have poor performance so i hope dev consider optimizing its performance local testFunc = {} testFunc.doTestMenu = function(player, context) local starttime = os.time() for i=1,300 do local firstMenu = context:addOption("test" .. i, nil, nil) local secondMenu = ISContextMenu:getNew(context) context:addSubMenu(firstMenu, secondMenu) end local endtime = os.time() print(string.format("cost time: %.4f", endtime - starttime)) -- cost time: 0.2440 end Events.OnFillWorldObjectContextMenu.Add(testFunc.doTestMenu) Edited September 30, 2022 by ProjectSky Link to comment Share on other sites More sharing options...
Aiteron Posted July 7, 2022 Share Posted July 7, 2022 Hi! Why you need so many submenu? Link to comment Share on other sites More sharing options...
ProjectSky Posted September 13, 2022 Author Share Posted September 13, 2022 On 7/8/2022 at 12:47 AM, Aiteron said: Hi! Why you need so many submenu? because my more buildings mod add lot of builds, it requires a lot of submenu Link to comment Share on other sites More sharing options...
EnigmaGrey Posted September 13, 2022 Share Posted September 13, 2022 Say it takes 0.002 seconds to add a single Option. if I add 300 of them, I'll spend 0.6 seconds adding options. If I add 600 of them, I'll spend 1.2 seconds adding options. That's what you'd expect from a for loop: it'll scale linearly based on the number of statements and number of elements it has to process. Instead of 300 menus and 300 submenus, it'd probably be better to use other gui elements that aren't created every time they're needed, like what's happening with the context menu. Link to comment Share on other sites More sharing options...
ProjectSky Posted September 13, 2022 Author Share Posted September 13, 2022 (edited) 12 hours ago, EnigmaGrey said: Say it takes 0.002 seconds to add a single Option. if I add 300 of them, I'll spend 0.6 seconds adding options. If I add 600 of them, I'll spend 1.2 seconds adding options. That's what you'd expect from a for loop: it'll scale linearly based on the number of statements and number of elements it has to process. Instead of 300 menus and 300 submenus, it'd probably be better to use other gui elements that aren't created every time they're needed, like what's happening with the context menu. if use other gui implemented i need to rewrite all the base code which is equivalent to make a new mod. so i hope dev consider optimizing its performance. Edited September 14, 2022 by ProjectSky Link to comment Share on other sites More sharing options...
Maris Posted September 15, 2022 Share Posted September 15, 2022 The solution might be to create a submenu on hover over the menu option. But that's a lot of refactoring work. Also some mods will be broken. Link to comment Share on other sites More sharing options...
ItsJiminy Posted November 23, 2023 Share Posted November 23, 2023 This isn't something that will fix the overall issue that Lua has not like, wait functionality, and every single file is required to implement it directly itself if it needs something like it, but. This handle of ontick, along with your iteration over the required number of options, removes any freezing. local testFunc = {starttime = false, totalOptionsToAdd = 300, optionsAdded = 0, context = false} testFunc.doTestMenu = function(player, context) testFunc.optionsAdded = 0 testFunc.context = context testFunc.starttime = os.time() end testFunc.doTestTick = function() if testFunc.starttime then if testFunc.optionsAdded ~= testFunc.totalOptionsToAdd then local maxVal = math.min(testFunc.optionsAdded+10, testFunc.totalOptionsToAdd) for i=testFunc.optionsAdded+1,maxVal do local firstMenu = testFunc.context:addOption("test" .. i, nil, nil) local secondMenu = ISContextMenu:getNew(testFunc.context) testFunc.context:addSubMenu(firstMenu, secondMenu) testFunc.optionsAdded = testFunc.optionsAdded + 1 end else local endtime = os.time() print(string.format("cost time: %.4f", endtime - testFunc.starttime)) -- cost time: 0.2440 testFunc.starttime = false testFunc.context = false end end end Events.OnTick.Add(testFunc.doTestTick) Events.OnFillWorldObjectContextMenu.Add(testFunc.doTestMenu) Link to comment Share on other sites More sharing options...
ItsJiminy Posted November 23, 2023 Share Posted November 23, 2023 Sorry for the second message to show the screenshot, and both need to be approved still, but this is the evidence of the options being displayed, and the time it takes, which given the player can't scroll through the context options in 1 second, no issue arises from the options taking a moment to fill. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now