1
\$\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
asked Jan 9, 2024 at 21:11
\$\endgroup\$

2 Answers 2

2
\$\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
answered Jan 9, 2024 at 22:29
\$\endgroup\$
3
  • \$\begingroup\$ I forgot the method argument in my original code. \$\endgroup\$ Commented Jan 9, 2024 at 23:03
  • \$\begingroup\$ My code should be adaptable accordingly. \$\endgroup\$ Commented Jan 9, 2024 at 23:17
  • \$\begingroup\$ For more explanation, see here. \$\endgroup\$ Commented Jan 9, 2024 at 23:20
2
\$\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
answered Jan 9, 2024 at 23:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.