Quantcast
Help fixing my addon with new API - WoWInterface
Thread Tools Display Modes
10-16-20, 07:57 PM   #1
Krevlorn
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 13
Help fixing my addon with new API

Hello all! I maintain a simple Broker data addon for displaying backpack-tracked currencies called Broker_Wallet. I haven't really had to touch it in like, 10 years? I am very a very amateur coder and I am completely baffled as to how to call the new currency functions. Can anyone push me in the right direction?

Here is the main code:

Code:
local LDB = LibStub:GetLibrary("LibDataBroker-1.1")

Wallet = LDB:NewDataObject( "Broker_Wallet", {
		type = "data source",
		icon = "Interface\\Icons\\Inv_Misc_Armorkit_18",
		text = "",
	} )

local frame = CreateFrame("Frame")
frame:RegisterEvent( "CURRENCY_DISPLAY_UPDATE", "LISTEN_CURRENCY_UPDATE" )
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function( ) Wallet:Update( ) end )

function Wallet:ADDON_LOADED( )
    Wallet:Update( )
end

function Wallet.OnClick( frame, button )
	_G.CharacterFrame_OnLoad(_G.CharacterFrame)
    ToggleCharacter("TokenFrame")
end

function Wallet.OnTooltipShow(tooltip)
	tooltip:AddLine("Broker_Wallet", 0,1,0, 0,1,0)
	local numTokenTypes = GetCurrencyListSize()
	if numTokenTypes == 0 then return end
	-- expand all token headers
	for j = numTokenTypes, 1, -1 do
		local _, isHeader, isExpanded = GetCurrencyListInfo( j )
		if isHeader and not isExpanded then
		ExpandCurrencyList( j, 1 )
		end
	end
	local numTokenTypes = GetCurrencyListSize( )
	for j = 1, numTokenTypes do
		local name, isHeader, isExpanded, isUnused, isWatched, count, currencyType, icon, item = GetCurrencyListInfo( j )
		if isHeader then
			tooltip:AddLine( " " )
			tooltip:AddLine( name )
		else
			tooltip:AddDoubleLine( name, count, 1, 1, 1, 1, 1, 1 )
		end
	end
	tooltip:AddLine( " " )
	tooltip:AddLine( " " )
	tooltip:AddLine("Click to toggle currency frame.", 0,1,0, 0,1,0)
	tooltip:AddLine("Track up to 3 tokens on the bar", 0,1,0, 0,1,0)
	tooltip:AddLine("using the default in-game tracker.", 0,1,0, 0,1,0)
end

function Wallet:Update( )
	self.text = ""
	
	local numTokenTypes = GetCurrencyListSize( )	
	if numTokenTypes == 0 then return end
	local name, isHeader, isExpanded, isUnused, isWatched, count, currencyType, icon	
	for i = numTokenTypes, 1, -1 do
		name, isHeader, isExpanded = GetCurrencyListInfo( i )
		if isHeader and not isExpanded then
			ExpandCurrencyList( i, 1 )
		end
	end
	local numTracked = 0
	numTokenTypes = GetCurrencyListSize( )
	local size = ( 0 ) + 1
	
	for i = 1, numTokenTypes do
		name, isHeader, isExpanded, isUnused, isWatched, count, icon = GetCurrencyListInfo( i )
		if not isHeader and isWatched then
			if extraCurrencyType == 1 then
				icon = [[Interface\PVPFrame\PVP-ArenaPoints-Icon]]
			elseif extraCurrencyType == 2 then
				local factionGroup = UnitFactionGroup( "player" )
				if factionGroup then
					icon = [[Interface\PVPFrame\PVP-Currency-]] .. factionGroup
				end
			end
			self.text = string.format( "%s |T%s:0|t %d", self.text, icon or [[Interface\Icons\Temp]], count or 0 )
		end
		self.text = string.trim( self.text )
	end
end

function Wallet.BackpackUpdate( )
    Wallet:Update( )
end

hooksecurefunc( "BackpackTokenFrame_Update", Wallet.BackpackUpdate)
  Reply With Quote
10-16-20, 09:37 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 503
Many API functions that have many returns are being changed into returning a table.

Instead of the following:
local first, second, third = GetDataStuff()
-- first == "1st" and second == "2nd" and third == "3rd"
We get a table:
local table = GetDataStuff()
And the table looks like this:
table = {
first = "1st",
second = "2nd",
third = "3rd",
}
In your code, instead of:
local name, isHeader, isExpanded = C_Currency.GetCurrencyListInfo( i )
You use:
local table = C_Currency.GetCurrencyListInfo( i )
Then you can use the following:
table.name
table.isHeader
table.isHeaderExpanded
Because we no longer have control over the variable names returned, you'll have to consult API documentation ingame or on third party websites, or use the command /dump to print out the table. The following command will dump currency info on Coalescing Visions:
/dump C_Currency.GetCurrencyInfo(1755)
A negative side effect of returning tables is memory leaks. If you are using these table-returning functions a lot, such as every frame, each previous table is still there, just waiting to be garbage collected. Too much of these tables in a short time frame will cause the game to temporarily freeze or just slow down for less than a second in order for Lua to release some of this memory. It's not really an issue on beefy CPUs, but it is a very noticeable problem on weaker computers. Keep this in mind when using these functions.
  Reply With Quote
10-17-20, 10:24 PM   #3
Krevlorn
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 13
Thank you for the reply. I am not sure if I can make it work like it used to without a ton of work, sadly. But I think this can get me started. Cheers.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help fixing my addon with new API

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off