View Single Post
04-02-24, 06:34 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,334
  • Unless you're hooking into a Blizzard LoD "addon", the UI code is already loaded before yours. There's no need to delay creating your button.
  • Unless the inherited template requires it, frame names are optional. This ends up creating a global with the name of your frame. This can introduce name collisions when you have a generic name like "AcceptAndTrackButton".
  • Manually calling show/hide depending on parent visibility is redundant as children will never show when the parent is hidden.
  • Calling GetQuestID() after AcceptQuest() can have unpredictable results as it's only supposed to be valid while the quest detail is shown.
  • Using a time delay to set your tracking creates a race condition for the server to respond that you have successfully accepted a quest. It's better to respond to the event signifying this instead.

With these in mind, here's with the changes I've made.
Lua Code:
  1. local TrackButton = CreateFrame("Button", nil, QuestFrameDetailPanel, "UIPanelButtonTemplate");
  2. TrackButton:SetSize(120, 22);
  3. TrackButton:SetText("Accept And Track");
  4. TrackButton:SetPoint("BOTTOMLEFT", 113, 4);
  5. TrackButton:SetScript("OnClick", function(self)
  6.     self.QuestID = GetQuestID();
  7.     AcceptQuest();
  8. end);
  9.  
  10. TrackButton:RegisterEvent("QUEST_ACCEPTED");
  11. TrackButton:SetScript("OnEvent", function(self, event, questid)
  12.     if questid == self.QuestID then
  13.         if not C_QuestLog.GetQuestWatchType(questid) then
  14.             C_QuestLog.AddQuestWatch(questid, Enum.QuestWatchType.Automatic);
  15.         end
  16.         self.QuestID = nil;
  17.     end
  18. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 04-04-24 at 02:27 AM.
  Reply With Quote