I have been trying to make an addon that announces when I trade and either receive or give gold but cannot figure out how to make it work.
Here is the code:
local f = CreateFrame(“Frame”)
f:RegisterEvent(“TRADE_MONEY_CHANGED”)
f:RegisterEvent(“TRADE_ACCEPT_UPDATE”)
f:SetScript(“OnEvent”, function(self, event, playerAgreed, targetAgreed)
if event == “TRADE_MONEY_CHANGED” then
currentOffer = tonumber(GetTargetTradeMoney())
myOffer = tonumber(GetPlayerTradeMoney())
target = UnitName(“target”)
end
elseif event == “TRADE_ACCEPT_UPDATE” and playerAgreed == 1 and targetAgreed == 1 then
if currentOffer > 0 then
DEFAULT_CHAT_FRAME:AddMessage(‘Trade with ‘target’ was COMPLETED. got(currentOffer)’, “EMOTE”, “COMMON”, 1)
elseif myOffer > 0 then
DEFAULT_CHAT_FRAME:AddMessage(‘Trade with ‘target’ was COMPLETED. lost(myOffer)’, “EMOTE”, “COMMON”, 1)
end
end
end)
Any help would be appreciated.
Install BugSack and BugGrabber so you can better see the errors in your code.
https://www.curseforge.com/wow/addons/bugsack
https://www.curseforge.com/wow/addons/bug-grabber
If the code you posted is actual, then the first end needs to be removed (or the next line reduced to an if, not an elseif). Your message strings aren’t concatinated properly and the use of non-unique globals that could be used by another addon.
Maybe something a bit more like:
local currentOffer = 0
local myOffer = 0
local f = CreateFrame("Frame")
f:RegisterEvent("TRADE_MONEY_CHANGED")
f:RegisterEvent("TRADE_ACCEPT_UPDATE")
f:SetScript("OnEvent", function(self, event, playerAgreed, targetAgreed)
if event == "TRADE_MONEY_CHANGED" then
currentOffer = tonumber(GetTargetTradeMoney())
myOffer = tonumber(GetPlayerTradeMoney())
end
if event == "TRADE_ACCEPT_UPDATE" and playerAgreed == 1 and targetAgreed == 1 then
local target = UnitName("target")
if currentOffer > 0 then
DEFAULT_CHAT_FRAME:AddMessage("Trade with ".. target.. " was COMPLETED. Got(".. currentOffer ..")", "EMOTE", "COMMON", 1)
elseif myOffer > 0 then
DEFAULT_CHAT_FRAME:AddMessage("Trade with ".. target .. " was COMPLETED. Lost(" .. myOffer .. ")", "EMOTE", "COMMON", 1)
end
end
end)
I appreciate the response. I see what you mean. The code you posted did not cause any errors, however it did not do anything when I completed a trade. Is it possible that the AddMessage() is not the correct thing to call for this?
The TRADE_ACCEPT_UPDATE event code only does anything under specific conditions, you could test those conditions by seeing what you are working with.
Before the last end) insert
print(event, playerAgreed, targetAgreed, currentOffer, myOffer)
Thank you, its very close. I think if I can figure out how to convert the number that it spit out from the copper value to gold and how to output to a channel, I’ll be good.
"Trade with ".. target.. " was COMPLETED. Got(".. GetMoneyString(currentOffer, true) ..")"
I’m not sure what you mean by “output to a channel” unless you want to:
https://wow.gamepedia.com/API_SendChatMessage
Instead of just adding the message for only you to see.