- 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:
local TrackButton = CreateFrame("Button", nil, QuestFrameDetailPanel, "UIPanelButtonTemplate");
TrackButton:SetSize(120, 22);
TrackButton:SetText("Accept And Track");
TrackButton:SetPoint("BOTTOMLEFT", 113, 4);
TrackButton:SetScript("OnClick", function(self)
self.QuestID = GetQuestID();
AcceptQuest();
end);
TrackButton:RegisterEvent("QUEST_ACCEPTED");
TrackButton:SetScript("OnEvent", function(self, event, questid)
if questid == self.QuestID then
if not C_QuestLog.GetQuestWatchType(questid) then
C_QuestLog.AddQuestWatch(questid, Enum.QuestWatchType.Automatic);
end
self.QuestID = nil;
end
end);