\$\begingroup\$
\$\endgroup\$
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()
andSetScript()
are all in-game API
user3204810user3204810
asked Jan 9, 2024 at 21:11
2 Answers 2
\$\begingroup\$
\$\endgroup\$
3
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
-
\$\begingroup\$ I forgot the
method
argument in my original code. \$\endgroup\$user3204810– user32048102024年01月09日 23:03:57 +00:00Commented Jan 9, 2024 at 23:03 -
\$\begingroup\$ My code should be adaptable accordingly. \$\endgroup\$Tobias Grothe– Tobias Grothe2024年01月09日 23:17:11 +00:00Commented Jan 9, 2024 at 23:17
-
\$\begingroup\$ For more explanation, see here. \$\endgroup\$Tobias Grothe– Tobias Grothe2024年01月09日 23:20:46 +00:00Commented Jan 9, 2024 at 23:20
\$\begingroup\$
\$\endgroup\$
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
lang-lua