Jump to content

ISContextMenu poor performance


ProjectSky

Recommended Posts

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 by ProjectSky
Link to comment
Share on other sites

  • 6 months later...
  • 2 months later...

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

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 by ProjectSky
Link to comment
Share on other sites

  • 1 year later...

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

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.image.thumb.png.7f8032cffbf5751caf513e21616c2898.png

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...