(Updated and Solved): Addon Assistance (Development): WoW Classic Anniversary - Quest XP Addon

UPDATE: My issue has been resolved.

Good afternoon everyone,

BLUF:
Code is not allowing proper mirroring of Quest Reward Value displayed in two separate panels.


I am looking for assistance in identifying a frustrating code complication that I can’t seem to fix and ChatGPT can’t seem to fix either.

The intent for this code is to display accurately the quest reward value in the quest log (next to quest titles), AND in the information panel attached to the Quest Reward Title Panel.

For reference, I am using Visual Studio Code

Below is the code I have so far, and would love for any insight on adjustment to reflect both values correctly and accurately:

– Basic Addon Setup
local QuestXPAddon = CreateFrame(“Frame”, “QuestXPAddonFrame”, UIParent)

– Register Events
QuestXPAddon:RegisterEvent(“ADDON_LOADED”)
QuestXPAddon:RegisterEvent(“QUEST_LOG_UPDATE”)
QuestXPAddon:RegisterEvent(“QUEST_DETAIL”)

– Function to Retrieve Adjusted XP from Quest Log Titles
local function GetAdjustedXPForQuest(questIndex)
local titleButton = _G[“QuestLogTitle” … questIndex]
if titleButton and titleButton.xpText then
local xpText = titleButton.xpText:GetText()
if xpText then
local xpValue = tonumber(xpText:match(“%d+”)) – Extract XP number from “XP: 1400”
return xpValue or 0
end
end
return 0 – Default to 0 if no XP is found
end

– Function to Display XP Reward Next to QuestLogRewardTitleText
local function DisplayXPNextToRewardTitle()
local questIndex = GetQuestLogSelection()
local adjustedXP = GetAdjustedXPForQuest(questIndex)

if not QuestLogRewardTitleText then
    print("QuestLogRewardTitleText frame not found!") -- Debug message
    return
end

if adjustedXP > 0 then
    if not QuestLogFrame.XPRewardText then
        -- Attach font string to QuestLogFrame and align relative to QuestLogRewardTitleText
        local xpText = QuestLogFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        xpText:SetPoint("LEFT", QuestLogRewardTitleText, "RIGHT", -80, 0) -- Adjust position
        xpText:SetTextColor(0.0, 0.8, 1.0)
        QuestLogFrame.XPRewardText = xpText
    end

    -- Update and show the XP reward text
    QuestLogFrame.XPRewardText:SetText("XP: " .. adjustedXP)
    QuestLogFrame.XPRewardText:Show()
elseif QuestLogFrame.XPRewardText then
    -- Hide the XP text if no reward is available
    QuestLogFrame.XPRewardText:Hide()
end

end

– Handle XP Display in Quest Titles
local function HandleXPInQuestTitles()
local numEntries = GetNumQuestLogEntries()

for questIndex = 1, numEntries do
    local title, level, _, isComplete, _, _, _, questID = GetQuestLogTitle(questIndex)
    local questButton = _G["QuestLogTitle" .. questIndex]

    -- Clear any existing XP text to prevent residual display issues
    if questButton and questButton.xpText then
        questButton.xpText:SetText("") -- Clear text
        questButton.xpText:Hide()      -- Hide XP display
    end

    -- Check if this is a quest
    if questID > 0 and level > 0 then
        SelectQuestLogEntry(questIndex) -- Ensure quest data is loaded
        local xpReward = GetQuestLogRewardXP()

        if isComplete then
            -- Skip adding XP to the title for completed quests
            if questButton and questButton.xpText then
                questButton.xpText:Hide()
            end
        else
            if xpReward > 0 then
                -- Add XP display for incomplete quests
                if questButton then
                    if not questButton.xpText then
                        -- Create a font string for XP display
                        local xpText = questButton:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
                        xpText:SetPoint("RIGHT", questButton, "RIGHT", -10, 0) -- Adjust position
                        xpText:SetTextColor(0.0, 0.8, 1.0)
                        questButton.xpText = xpText
                    end

                    -- Update XP text
                    questButton.xpText:SetText("XP: " .. xpReward)
                    questButton.xpText:Show()
                end
            end
        end
    end
end

end

– Event Handler
QuestXPAddon:SetScript(“OnEvent”, function(self, event, …)
if event == “ADDON_LOADED” and … == “QuestXPAddon” then
print(“QuestXPAddon loaded! XP rewards will now be displayed.”)
elseif event == “QUEST_LOG_UPDATE” then
HandleXPInQuestTitles()
elseif event == “QUEST_DETAIL” then
DisplayXPNextToRewardTitle()
end
end)


Thank you so much in advance.

Maybe something like (not extensivley tested):

local function GetXP(questIndex)
	if not questIndex then
		return
	end
	local questLogTitleText, level, questTag, isHeader, isCollapsed, isComplete, frequency, questID, startEvent, displayQuestID, isOnMap, hasLocalPOI, isTask, isBounty, isStory, isHidden, isScaling = GetQuestLogTitle(questIndex)
	if not isComplete and questID > 0 and level > 0 then
		SelectQuestLogEntry(questIndex)
		local xpReward = GetQuestLogRewardXP()
		if xpReward > 0 then
			return xpReward
		end
	end
end

hooksecurefunc("QuestLog_Update", function(self, event, ...)
	local numEntries, numQuests = GetNumQuestLogEntries();
	for i=1, QUESTS_DISPLAYED, 1 do
		questIndex = i + FauxScrollFrame_GetOffset(QuestLogListScrollFrame);
		if questIndex <= numEntries then
			local xpReward = GetXP(questIndex)
			if xpReward then
				_G["QuestLogTitle"..i.."Tag"]:SetText(format("\124cff00ccffXP: %s\124r", xpReward))
			end
		end
	end
end)

QuestLogRewardTitleText.XP = QuestLogDetailScrollChildFrame:CreateFontString()
QuestLogRewardTitleText.XP:SetDrawLayer(QuestLogRewardTitleText:GetDrawLayer())
QuestLogRewardTitleText.XP:SetFontObject(QuestTitleFont)
QuestLogRewardTitleText.XP:SetTextColor(0.0, 0.8, 1.0)
QuestLogRewardTitleText.XP:SetPoint("LEFT", QuestLogRewardTitleText, "LEFT", 80, 0)
hooksecurefunc("QuestLogTitleButton_OnClick", function(self)
	QuestLogRewardTitleText.XP:SetText("")
	local questIndex = self:GetID() + FauxScrollFrame_GetOffset(QuestLogListScrollFrame)
	local xpReward = GetXP(questIndex)
	if xpReward then
		local xp = UnitXPMax("player")
		local pc = format("%.1f", (xpReward /xp) * 100)
		QuestLogRewardTitleText.XP:SetText(format("XP: %s [%s%%]", xpReward, pc))
	end
end)

Edited to add the XP percentage of level to the Rewards text.

Oh shoot, I forgot to come back here to update. I managed to fix my problem and I officially deployed it as my very first addon creation!

If you’re still using code from your OP, it doesn’t account for the possabilty of there being more entries in the log than there are rows in the list. You will get get the same XP numbers top-to-bottom each scroll and the display in the details panel probably won’t sync. to the selected quest.

1 Like