View Single Post
04-02-24, 03:51 PM   #1
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
First Button Hook Attempt

This adds a button next to the quest "Accept" button labeled "Accept Track". When pressed the quest is accepted and quest tracking is turned on. It seems to be working ok, but thought I would put it here for review and see if there was something different I should have done.

AcceptAndTrack.toc
Lua Code:
  1. ## Interface: 100206
  2. ## Title: Accept And Track
  3. ## Notes: Adds an 'Accept and Track' button to quest frames.
  4. ## Author: Codger
  5. ## Version: 10.2.6
  6. AcceptAndTrack.lua

AcceptAndTrack.lua
Lua Code:
  1. local function AcceptAndTrackQuest()
  2.     AcceptQuest();
  3.  
  4.     C_Timer.After(1, function() -- Delay to ensure the quest log is updated
  5.         local questID = GetQuestID();
  6.         if questID and not C_QuestLog.GetQuestWatchType(questID) then
  7.             C_QuestLog.AddQuestWatch(questID, Enum.QuestWatchType.Automatic);
  8.             -- No need to manually update the objective tracker anymore,
  9.             -- as the quest watch system handles it.
  10.         end
  11.     end)
  12. end
  13.  
  14. local function CreateButton()
  15.     if not QuestFrameAcceptButton then return end -- Ensures the Accept button is loaded
  16.  
  17.     local trackButton = CreateFrame("Button", "AcceptAndTrackButton", QuestFrame, "UIPanelButtonTemplate")
  18.     trackButton:SetSize(120, 22) -- Button size
  19.     trackButton:SetText("Accept And Track")
  20.     trackButton:SetPoint("TOPRIGHT", QuestFrameAcceptButton, "TOPRIGHT", 150, 0) -- Adjusted positioning
  21.     trackButton:SetScript("OnClick", AcceptAndTrackQuest)
  22.     trackButton:SetShown(QuestFrame:IsShown())
  23.  
  24.     -- Debug print to chat to confirm the button was created
  25.     print("AcceptAndTrack button created.")
  26. end
  27.  
  28. -- Delay button creation to ensure the QuestFrame is fully loaded
  29. C_Timer.After(2, CreateButton)
  30.  
  31. QuestFrame:HookScript("OnShow", function() if AcceptAndTrackButton then AcceptAndTrackButton:SetShown(true) end end)
  32. QuestFrame:HookScript("OnHide", function() if AcceptAndTrackButton then AcceptAndTrackButton:SetShown(false) end end)
  Reply With Quote