Jump to content

table problem


Zcraks

Recommended Posts

local button = {}

function createMenu()
  button[1] = ...
  button[2] = ...

  -- either add handlers yourself
  addEventHandler("onClientGUIClick", button[1], click, false)
  addEventHandler("onClientGUIClick", button[2], click, false) -- if you add a false after the function name (getPropagated parameter), the function won't trigger for the elements parents (e.g. the parent gui window)

  -- or use a loop here
  for i = 1, #button do -- start at i = 1 and perform the body of the block and increment i until i = #button (number of elements in button table)
    addEventHandler("onClientGUIClick", button[i], click, false)
  end
end


function click()
  -- beacuse getPropagated is set to false on all handlers, you don't need to test whether this is a valid source (that is, source will never be your guiwindow), all you need to do is branch depending on which button it is
  if source == button[1] then
    ...
  elseif source == button[2] then
    ...
  end
end

Would be a much better way of doing this, imo.

I'm not sure but you may need to put the click function above the function with addEventHandlers.

Edited by MrTasty
Link to comment
  • Moderators
1 hour ago, Zcraks said:

How to check for all buttons?

You can also attach the eventHandler to the parent GUI, if you have at least one GUI parent.

 

 

local myWindow = guiCreateWindow ( 0, 0, 0.5, 0.4, "window", true )

local button = {}

function click ()
	if source ~= myWindow then
		if source == button[1] then
    
		elseif source == button[2] then

		end
	end
end



button[1] = guiCreateButton( 0.3, 0.1, 0.2, 0.1, "button 1!", true, myWindow ) -- 1
button[2] = guiCreateButton( 0.7, 0.1, 0.2, 0.1, "button 2!", true, myWindow ) -- 2

addEventHandler("onClientGUIClick", myWindow, click, true) -- true = propagation enabled

 

 

 

 

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...