WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Button:SetScript (https://www.wowinterface.com/forums/showthread.php?t=49766)

MaLarsson 08-21-14 01:37 PM

Button:SetScript
 
I've been scratcing my head for a while trying to figure this out.
This code works:
Lua Code:
  1. local btn = CreateFrame("Button", "DoTrackerLockBtn", menu, "UIPanelButtonTemplate")
  2. btnLock:SetText("Lock")
  3. btnLock:SetWidth(80)
  4. btnLock:ClearAllPoints()
  5. btnLock:SetPoint("TOPRIGHT", -110, -16)
  6. btnLock:SetScript("OnClick", function() print("test") end)
This code works aswell:
Lua Code:
  1. local function printTest()
  2.     print("test")
  3. end
  4.  
  5. local btn = CreateFrame("Button", "DoTrackerLockBtn", menu, "UIPanelButtonTemplate")
  6. btnLock:SetText("Lock")
  7. btnLock:SetWidth(80)
  8. btnLock:ClearAllPoints()
  9. btnLock:SetPoint("TOPRIGHT", -110, -16)
  10. btnLock:SetScript("OnClick", function() printTest() end)
But this code does not work:
Lua Code:
  1. local function printTest()
  2.     print("test")
  3. end
  4.  
  5. local btn = CreateFrame("Button", "DoTrackerLockBtn", menu, "UIPanelButtonTemplate")
  6. btnLock:SetText("Lock")
  7. btnLock:SetWidth(80)
  8. btnLock:ClearAllPoints()
  9. btnLock:SetPoint("TOPRIGHT", -110, -16)
  10. btnLock:SetScript("OnClick", printTest())
Why is this? It seems to me that in all three cases I send a function to the SetScript Method but the last on does not work. What am I missing?

Thanks in advance
Marcus Larsson

Vrul 08-21-14 01:49 PM

That last line should just be:
Code:

btnLock:SetScript("OnClick", printTest)
Using printTest() is trying to use the return of the function call where just printTest is passing the function itself.


All times are GMT -6. The time now is 09:46 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI