Jump to content

createElement and GUI's


Deltanic

Recommended Posts

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

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
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

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

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

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

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...