Deltanic Posted March 20, 2011 Share Posted March 20, 2011 Hi, i've tried to give buttons their own parent, but this doesnt seem to work. Atleast, not what I'm doing. Imagine I have 4 buttons, 2 menu buttons, and 2 others. I want to use them in the same GUI-window, but in another eventHandler. function whenResourceStart ( ) window = guiCreateWindow ( blahblah ) button1 = guiCreateButton ( blahblah, window ) button2 = guiCreateButton ( blahblah, window ) button3 = guiCreateButton ( blahblah, window ) button4 = guiCreateButton ( blahblah, window ) -- So I've create a window with 4 buttons. -- Now I'll give them their own parent. parent1 = createElement ( "parent1" ) -- First parent parent2 = createElement ( "parent2" ) -- Second parent setElementParent ( button1, parent1 ) setElementParent ( button2, parent1 ) setElementParent ( button3, parent2 ) setElementParent ( button4, parent2 ) -- Seems to be good, but, those eventHandlers won't work. addEventHandler ( "onClientGUIClick", parent1, onClickParent1 ) addEventHandler ( "onClientGUIClick", parent2, onClickParent2 ) -- And that's weird, because addEventHandler ( "onClientGUIClick", window, onClickParent1 ) will work! end Link to comment
Moderators Citizen Posted March 20, 2011 Moderators Share Posted March 20, 2011 Hi, parent1 and parent2 are not a GUIElement so onClientGUIClick doesn't work. Maybe replace it by onClientClick ? Also you can "say" to the server that is a GUIElement like (I'm not sure) parent1 = createElement ( "gui", "parent1" ) -- or parent1 = createElement ( "GUI", "parent1" ) -- or parent1 = createElement ( "guiElement", "parent1" ) -- etc Link to comment
Deltanic Posted March 20, 2011 Author Share Posted March 20, 2011 parent1 and parent2 are not a GUIElement so onClientGUIClick doesn't work.Maybe replace it by onClientClick ? Very good chance that that's the problem. But, just tried onClientClick and even that doesn't get triggered. parent1 = createElement ( "gui", "parent1" ) parent1 = createElement ( "GUI", "parent1" ) parent1 = createElement ( "guiElement", "parent1" ) Tried that too, negative result. And tried "gui-button", which is a valid gui-element, too. Link to comment
Moderators Citizen Posted March 20, 2011 Moderators Share Posted March 20, 2011 So only 1 solution: 1 addEventHandler( "onClientGUIClick", ... ) for each button Link to comment
Deltanic Posted March 20, 2011 Author Share Posted March 20, 2011 That's gonna be around 20-30 eventHandlers then. No, that's not code improvement at all O_O Link to comment
Callum Posted March 20, 2011 Share Posted March 20, 2011 Why not attach the event handler to the window, and check if the 'source' is a button? Link to comment
Deltanic Posted March 20, 2011 Author Share Posted March 20, 2011 Since all 4 (in the example) are buttons, and need to be handled separately. Otherwise I will need a lot of sh*tty code, or the eventHandler will be called twice for one button. I could use setElementData, but that's slower afaik. Or will thise cause no problem, if setElementData is done once for all buttons? Link to comment
Castillo Posted March 21, 2011 Share Posted March 21, 2011 Why don't you make 1 event handler and then check. addEventHandler("onClientGUIClick",getRootElement(), function () if (source == button1) then elseif (source == button2) then end end) Link to comment
Deltanic Posted March 21, 2011 Author Share Posted March 21, 2011 Because button 1 is just slightly different than button 2. So, if-elseif will create a mess in my code. I just need to know which group is clicked to get maximum functionality. Example: Here's your script SolidSnake: addEventHandler("onClientGUIClick",getRootElement(), function () if (source == button1) then DoSomething ( 1 ) -- Yes, this can litterally contain 1, or something else that's predefined. elseif (source == button2) then DoSomething ( 2 ) -- Almost the same, only the argument has changed! end end ) Imagine I want a new button. I need to create it, add a new elseif, and a third DoSomething. Now to the grouped script. addEventHandler("onClientGUIClick",getRootElement(), function () if (source == group1) then for k,v in ipairs ( button ) do if source == button[k] then DoSomething ( buttonValue[k] ) end -- that's all to check as much buttons as I want. end end end ) That's all the code I need then. If I want to add a new button, I'll just have to create on new button, and add a value for it. So easy is that. But I can't use that technique without grouping.. Link to comment
Moderators Citizen Posted March 21, 2011 Moderators Share Posted March 21, 2011 Why not some: setElementData(button1, "Group", "Group1" ) setElementData(button2, "Group", "Group1" ) Then you can make: addEventHandler("onClientGUIClick",getRootElement(), function () local group = getElementData( source, "Group" ) if ( group == "Group1") then for k,v in ipairs ( button ) do if source == button[k] then DoSomething ( buttonValue[k] ) end -- that's all to check as much buttons as I want. end end end ) Link to comment
Moderators Citizen Posted March 21, 2011 Moderators Share Posted March 21, 2011 And what's the problem ? It doesn't work ?! Link to comment
Deltanic Posted March 21, 2011 Author Share Posted March 21, 2011 Read my other replies: I could use setElementData, but that's slower afaik. Or will thise cause no problem, if setElementData is done once for all buttons? So, I'm just using setElementData now Link to comment
Moderators Citizen Posted March 21, 2011 Moderators Share Posted March 21, 2011 Oh sorry, my bad I already seen this last time but today, I forgot it 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