Thread Tools Display Modes
08-21-14, 01:37 PM   #1
MaLarsson
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 13
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
  Reply With Quote
08-21-14, 01:49 PM   #2
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
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.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Button:SetScript

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off