0
\$\begingroup\$

There is a way to not rewrite the same part (the cycle for i = 1...end) twice?

local function myfunc(frame,method)
    if frame:IsShown() then
        for i = 1, #frame.buttons do
            frame.buttons[i]:HookScript("OnMouseDown", method)
        end
    else
        self:SetScript(
            "OnShow",
            function(frame)
                for i = 1, #frame.buttons do
                    frame.buttons[i]:HookScript("OnMouseDown", method)
                end
            end
        )
    end
end

Where

  • IsShown(), HookScript() and SetScript() are all in-game API
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Not a completely review, but this should work as intended:

local function myfunc(frame)
    function hookFunc(frame)
        for i = 1, #frame.buttons do
            frame.buttons[i]:HookScript("OnMouseDown", method)
        end
    end
    if frame:IsShown() then
        hookFunc(frame)
    else
        self:SetScript(
            "OnShow",
            hookFunc(frame)
        )
    end
end
\$\endgroup\$
3
  • \$\begingroup\$ I forgot the method argument in my original code. \$\endgroup\$ Commented Jan 9 at 23:03
  • \$\begingroup\$ My code should be adaptable accordingly. \$\endgroup\$ Commented Jan 9 at 23:17
  • \$\begingroup\$ For more explanation, see here. \$\endgroup\$ Commented Jan 9 at 23:20
1
\$\begingroup\$

Maybe a recursive function? It seems to work:

local function myfunc(frame,method)
    if frame:IsShown() then
        for i = 1, #frame.buttons do
            frame.buttons[i]:HookScript("OnMouseDown", method)
        end
    else
        frame:SetScript(
            "OnShow", --when "OnShow" is fired, IsShown() become true
            function()
                myfunc(frame,method)
            end
        )
    end
end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.