Th3Angel Posted February 29, 2012 Share Posted February 29, 2012 Which one would be better in terms of efficiency, a loop in one event handler or many event handlers. For example, if I were to create many gui-images and store them in an table, would using a loop be more efficient, or would creating an individual event handler for each gui-image be more efficient. Link to comment
Dev Posted February 29, 2012 Share Posted February 29, 2012 I suggest using a loop for each element in the table, for example local images = { } images[1] = guiCreateStaticImage(x, y, width, height, text, relative, parent) images[2] = guiCreateStaticImage(x, y, width, height, text, relative, parent) for i = 1, #images do addEventHandler("onClientGUIClick", images[i], selectOption, false) end But regardless, you can do it either way, it is handy when you are not sure as to how many images will be made, for example, if you're populating the items in a player's inventory, you might not know how many items he has, in that case, attaching handlers using a loop is the only way. Link to comment
Th3Angel Posted February 29, 2012 Author Share Posted February 29, 2012 Actually, I was talking along the lines of something like this: local images = {} images[1] = guiCreateStaticImage(x, y, width, height, text, relative, parent) images[2] = guiCreateStaticImage(x, y, width, height, text, relative, parent) images[3] = guiCreateStaticImage(x, y, width, height, text, relative, parent) -- ... images[30] = guiCreateStaticImage(x, y, width, height, text, relative, parent) function onClientGUIClickHandler(button, state, absoluteX, absoluteY) for k, v in pairs(images) do if (source == v) then -- Code end end end addEventHandler("onClientGUIClick", root, onClientGUIClickHandler) versus your method in terms of which one would be more efficient. Link to comment
Castillo Posted February 29, 2012 Share Posted February 29, 2012 What would be the problem on adding all the event handlers at once? I think it would be more efficient than having to loop every time. Link to comment
Dev Posted February 29, 2012 Share Posted February 29, 2012 As Solidsnake said, also how can you attach 'onClientGUIClickHandler' to a handler without any base element? Link to comment
Th3Angel Posted February 29, 2012 Author Share Posted February 29, 2012 What would be the problem on adding all the event handlers at once? I think it would be more efficient than having to loop every time. That's the same thing I thought. But, I thought that triggering events take up more memory, as I read it on triggerEvent of the wiki. As Solidsnake said, also how can you attach 'onClientGUIClickHandler' to a handler without any base element? The base element is the root element. root is a predefined variable across all serverside and clientside scripts that returns the root element of the server. The full list is here: viewtopic.php?f=91&t=39678. Link to comment
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