So I've decided to start tackling a currency tracking addon for Broker. Nothing usable as of yet, but I plan to have it display BG marks, Badges, Honor and Arena points on the tooltip and to toggle between them all to display and track.
That said, I am a coding n00b and I am having the darnedest time getting the currency frame to toggle on click. Any ideas?
Can you guys take a look at this and tell me a good way to get the tracking to save to save across sessions? I think it's in the first part with I initialize the addon, but my novice coding skills are failing me.
What happens now is you pick a currency to track, log out, and when you log back in, it shows the correct amoutn of what you were tracking, but the icon is the default and the suffix says "Honor." What can I do?
local frame = CreateFrame("frame")
local OnEvent = function(self, event, ...) self[event](self, event, ...) end
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local dataobj = ldb:NewDataObject("Broker_Wallet", {
value = "0",
icon = "Interface\\Icons\\Inv_Misc_Coin_02",
suffix = " Honor",
})
local getSuffix = function(i)
return (i == 1 and " Honor") or (i == 2 and " Arena") or (i == 3 and " AV") or (i == 4 and " AB") or (i == 5 and " EotS") or (i == 6 and " SotA") or (i == 7 and " WSG") or (i == 8 and " LWG") or (i == 9 and " SKShards") or (i == 10 and " BoJ") or (i == 11 and " EoH") or (i == 12 and " EoV") or (i == 13 and " JCTokens") or (i == 14 and " Cooking")
end
function dataobj.OnTooltipShow(tooltip)
tooltip:AddLine("Broker_Wallet", 0,1,0, 0,1,0)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Honor Points:", GetHonorCurrency(), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Arena Points:", GetArenaCurrency(), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Alterac Valley Marks:", GetItemCount("20560"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Arathi Basin Marks:", GetItemCount("20559"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Eye of the Storm Marks:", GetItemCount("29024"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Strand of the Ancients Marks:", GetItemCount("42525"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Warsong Gulch Marks:", GetItemCount("20558"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Wintergrasp Marks:", GetItemCount("43589"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Stone Keeper's Shards:", GetItemCount("43228"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Badges of Justice:", GetItemCount("29434"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Emblems of Heroism:", GetItemCount("40752"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Emblems of Valor:", GetItemCount("40753"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Dalaran Jewelcrafter's Token:", GetItemCount("41596"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Dalaran Cooking Token:", GetItemCount("43016"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddLine("Click to toggle display.")
end
local session
function frame:HONOR_CURRENCY_UPDATE()
if(Broker_Wallet.displ == 1) then
dataobj.value = GetHonorCurrency()
elseif(Broker_Wallet.displ == 2) then
dataobj.value = GetArenaCurrency()
elseif(Broker_Wallet.displ == 3) then
dataobj.value = GetItemCount("20560")
elseif(Broker_Wallet.displ == 4) then
dataobj.value = GetItemCount("20559")
elseif(Broker_Wallet.displ == 5) then
dataobj.value = GetItemCount("29024")
elseif(Broker_Wallet.displ == 6) then
dataobj.value = GetItemCount("42525")
elseif(Broker_Wallet.displ == 7) then
dataobj.value = GetItemCount("20558")
elseif(Broker_Wallet.displ == 8) then
dataobj.value = GetItemCount("43589")
elseif(Broker_Wallet.displ == 9) then
dataobj.value = GetItemCount("43228")
elseif(Broker_Wallet.displ == 10) then
dataobj.value = GetItemCount("29434")
elseif(Broker_Wallet.displ == 11) then
dataobj.value = GetItemCount("40752")
elseif(Broker_Wallet.displ == 12) then
dataobj.value = GetItemCount("40753")
elseif(Broker_Wallet.displ == 13) then
dataobj.value = GetItemCount("41596")
elseif(Broker_Wallet.displ == 14) then
dataobj.value = GetItemCount("43016")
end
dataobj.text = dataobj.value.." "..dataobj.suffix
end
function frame:PLAYER_ENTERING_WORLD()
if(not Broker_Wallet) then Broker_Wallet = {} end
session = select(1)
self:HONOR_CURRENCY_UPDATE()
end
function frame:BAG_UPDATE()
self:HONOR_CURRENCY_UPDATE()
end
Thank you for the code cleanup, Torhal. That looks a heck of a lot better. In reference to the saved variables, I have it referenced in the .toc file, but I must have lost it from an older version somewhere along the line. Now I just need to figure out how to get it to work.
local dataobj = ldb:NewDataObject("Broker_Wallet", {
value = "0",
icon = "Interface\\Icons\\Inv_Misc_Coin_02",
suffix = " Honor",
})
This seems to be my problem. The value is saving to the variables file just fine, but how do I use that value to set the correct suffix and icon when the addon loads in?
function frame:ADDON_LOADED(event, addon)
if addon ~= "Broker_Wallet" then return end
dataobj.value = savedvariable.value
dataobj.icon = savedvariable.icon
dataobj.suffix = savedvariable.suffix
end
Also, with getSuffix(); why not just make a table and do:
local function getSuffix(i)
return sufword[i]
end
...or just get rid of that function altogether and directly use the table. A function that only returns the value in a table is less efficient because of the overhead of the function call.
EDIT: Gah. I shouldn't read forums when I wake up. There was an error in the original post. :)
Alright, the little part of my brain that understands coding is going nuts now. And hurts. ;)
I got the suffix function out of the way and I am using a table and it works beautifully. Thank you!
My problems are still stemming from my lack of understanding saved variables. This is what I see in the saved variables file:
Broker_Wallet = {
["displ"] = 1 --or some other value that is saved
}
Now, that display value is saving correctly and the addon is calling it correctly, but I am not entirely sure how or why it is doing that, but I digress.
In my OnClick function, i use Broker_Wallet.displ value to determine the icon, value, and suffix. Is there a way to make dataobj.icon and dataobj.suffix to fire when the addon is loading in for the first time? Do I really need to save the icon and suffix values to savedvariables?
In the Broker_Wallet:ADDON_LOADED() function, just do:
function Broker_Wallet:ADDON_LOADED(event, addon)
if addon ~= "Broker_Wallet" then return end
self.displ = SavedVariables.displ
dataobj.suffix = sufword[self.displ]
dataobj.icon = icons[self.displ]
end
...replacing SavedVariables with whatever name you assigned to them in the TOC.
I am getting this error from Bugsack with that string placed as you gave it to me:
[2008/11/27 11:34:28-1851-x1]: Broker_Wallet-1.3\Broker_Wallet.lua:126: attempt to index global 'Broker_Wallet' (a nil value)
Here is the code:
function Broker_Wallet:ADDON_LOADED(event, addon)
if addon ~= "Broker_Wallet" then return end
self.displ = Broker_Wallet.displ
dataobj.suffix = sufword[self.displ]
dataobj.icon = icons[self.displ]
end
Are you giving your savedvariables the same name as your addon?
function Broker_Wallet:ADDON_LOADED(event, addon)
if addon ~= "Broker_Wallet" then return end
self.displ = SavedVariableName.displ -- This should NOT be Broker_Wallet
dataobj.suffix = sufword[self.displ]
dataobj.icon = icons[self.displ]
end
The "self" in Broker_Wallet:ADDON_LOADED() is referring to Broker_Wallet. Therefore, "self.displ = Broker_Wallet.displ" is the same as "self.displ = self.displ" and since Broker_Wallet.displ hasn't been defined at that point you get an error.
Ok, still getting errors. I don't know what I am doing wrong. I think its now not saving the value as it did before as it now shows nil. But the errors are really throwing me off. Here is Broker-Wallet.lua:
function ADDON_LOADED(event, addon)
if addon ~= "Broker_Wallet" then return end
self.displ = Broker_WalletDB.displ
dataobj.suffix = suffix[self.displ]
dataobj.icon = icons[self.displ]
end
local frame = CreateFrame("frame")
local OnEvent = function(self, event, ...) self[event](self, event, ...) end
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local dataobj = ldb:NewDataObject("Broker_Wallet")
frame:SetScript("OnEvent", OnEvent)
frame:RegisterEvent"HONOR_CURRENCY_UPDATE"
frame:RegisterEvent"PLAYER_ENTERING_WORLD"
frame:RegisterEvent"BAG_UPDATE"
function dataobj.OnTooltipShow(tooltip)
tooltip:AddLine("Broker_Wallet", 0,1,0, 0,1,0)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Honor Points:", GetHonorCurrency(), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Arena Points:", GetArenaCurrency(), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Alterac Valley Marks:", GetItemCount("20560"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Arathi Basin Marks:", GetItemCount("20559"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Eye of the Storm Marks:", GetItemCount("29024"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Strand of the Ancients Marks:", GetItemCount("42525"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Warsong Gulch Marks:", GetItemCount("20558"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Wintergrasp Marks:", GetItemCount("43589"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Stone Keeper's Shards:", GetItemCount("43228"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Badges of Justice:", GetItemCount("29434"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Emblems of Heroism:", GetItemCount("40752"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Emblems of Valor:", GetItemCount("40753"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Dalaran Jewelcrafter's Token:", GetItemCount("41596"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Dalaran Cooking Token:", GetItemCount("43016"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddLine("Click to toggle display.")
end
local values = {
GetHonorCurrency(),
GetArenaCurrency(),
GetItemCount("20560"),
GetItemCount("20559"),
GetItemCount("29024"),
GetItemCount("42525"),
GetItemCount("20558"),
GetItemCount("43589"),
GetItemCount("43228"),
GetItemCount("29434"),
GetItemCount("40752"),
GetItemCount("40753"),
GetItemCount("41596"),
GetItemCount("43016")
}
local icons = {
"Interface\\Icons\\Inv_Misc_Coin_02",
"Interface\\PVPFrame\\PVP-ArenaPoints-Icon",
"Interface\\Icons\\Inv_Jewelry_Necklace_21",
"Interface\\Icons\\Inv_Jewelry_Amulet_07",
"Interface\\Icons\\Spell_Nature_EyeOfTheStorm",
"Interface\\Icons\\Inv_Jewelry_Amulet_01",
"Interface\\Icons\\Inv_Misc_Rune_07",
"Interface\\Icons\\Inv_Jewelry_Ring_66",
"Interface\\Icons\\INV_Misc_Platnumdisks",
"Interface\\Icons\\Spell_Holy_ChampionsBond",
"Interface\\Icons\\Spell_Holy_ProclaimChampion",
"Interface\\Icons\\Spell_Holy_ProclaimChampion_02",
"Interface\\Icons\\INV_Misc_Gem_Variety_01",
"Interface\\Icons\\INV_Misc_Ribbon_01"
}
local suffix = {
" Honor",
" Arena",
" AV",
" AB",
" EotS",
" SotA",
" WSG",
" LWG",
" SKShards",
" BoJ",
" EoH",
" EoV",
" JCTokens",
" Cooking"
}
function frame:HONOR_CURRENCY_UPDATE()
dataobj.value = values[Broker_Wallet.displ]
dataobj.text = dataobj.value.." "..dataobj.suffix
end
function frame:PLAYER_ENTERING_WORLD()
if(not Broker_Wallet) then Broker_Wallet = {} end
self:HONOR_CURRENCY_UPDATE()
end
function frame:BAG_UPDATE()
self:HONOR_CURRENCY_UPDATE()
end
function dataobj.OnClick(self, button)
Broker_Wallet.displ = (Broker_Wallet.displ == 14 and 1) or (Broker_Wallet.displ and Broker_Wallet.displ+1) or 1
dataobj.suffix = suffix[Broker_Wallet.displ]
dataobj.icon = icons[Broker_Wallet.displ]
frame:HONOR_CURRENCY_UPDATE()
end
[2008/11/27 17:06:35-1902-x1]: Broker_Wallet-1.3\Broker_Wallet.lua:101: attempt to concatenate field 'suffix' (a nil value)
Broker_Wallet-1.3\Broker_Wallet.lua:106: in function `?'
Broker_Wallet-1.3\Broker_Wallet.lua:14: in function <Interface\AddOns\Broker_Wallet\Broker_Wallet.lua:14>
[2008/11/27 17:06:35-1902-x10]: Broker_Wallet-1.3\Broker_Wallet.lua:101: attempt to concatenate field 'suffix' (a nil value)
Broker_Wallet-1.3\Broker_Wallet.lua:110: in function `?'
Broker_Wallet-1.3\Broker_Wallet.lua:14: in function <Interface\AddOns\Broker_Wallet\Broker_Wallet.lua:14>
You're not declaring things in proper order, so they don't exist when you call them. Also, ADDON_LOADED() wasn't properly named and was also not registered as an event.
I went ahead and re-worked your code and sectioned it in a sane manner. It now saves between sessions, and loads the value/text as well.
Broker_Wallet.lua:
-------------------------------------------------------------------------------
-- Addon namespace
local Broker_Wallet = CreateFrame("Frame")
_G["Broker_Wallet"] = Broker_Wallet
local LDB = LibStub:GetLibrary("LibDataBroker-1.1")
dataobj = LDB:NewDataObject("Broker_Wallet",
{
value = "1",
icon = "Interface\\Icons\\Inv_Misc_Coin_02",
suffix = " Honor",
})
-------------------------------------------------------------------------------
-- Local variables
local defaults = {
displ = 1
}
local values = {
GetHonorCurrency(),
GetArenaCurrency(),
GetItemCount("20560"),
GetItemCount("20559"),
GetItemCount("29024"),
GetItemCount("42525"),
GetItemCount("20558"),
GetItemCount("43589"),
GetItemCount("43228"),
GetItemCount("29434"),
GetItemCount("40752"),
GetItemCount("40753"),
GetItemCount("41596"),
GetItemCount("43016")
}
local icons = {
"Interface\\Icons\\Inv_Misc_Coin_02",
"Interface\\PVPFrame\\PVP-ArenaPoints-Icon",
"Interface\\Icons\\Inv_Jewelry_Necklace_21",
"Interface\\Icons\\Inv_Jewelry_Amulet_07",
"Interface\\Icons\\Spell_Nature_EyeOfTheStorm",
"Interface\\Icons\\Inv_Jewelry_Amulet_01",
"Interface\\Icons\\Inv_Misc_Rune_07",
"Interface\\Icons\\Inv_Jewelry_Ring_66",
"Interface\\Icons\\INV_Misc_Platnumdisks",
"Interface\\Icons\\Spell_Holy_ChampionsBond",
"Interface\\Icons\\Spell_Holy_ProclaimChampion",
"Interface\\Icons\\Spell_Holy_ProclaimChampion_02",
"Interface\\Icons\\INV_Misc_Gem_Variety_01",
"Interface\\Icons\\INV_Misc_Ribbon_01"
}
local suffix = {
" Honor",
" Arena",
" AV",
" AB",
" EotS",
" SotA",
" WSG",
" LWG",
" SKShards",
" BoJ",
" EoH",
" EoV",
" JCTokens",
" Cooking"
}
local DISPL_MAX = 14
-------------------------------------------------------------------------------
-- Event handlers
Broker_Wallet:SetScript("OnEvent",
function(self, event, ...)
if self[event] then
return self[event](self, event, ...)
end
end
)
Broker_Wallet:RegisterEvent("ADDON_LOADED")
Broker_Wallet:RegisterEvent("BAG_UPDATE")
Broker_Wallet:RegisterEvent("HONOR_CURRENCY_UPDATE")
Broker_Wallet:RegisterEvent("PLAYER_ENTERING_WORLD")
local function Update()
dataobj.value = values[Broker_WalletDB.displ]
dataobj.text = dataobj.value.." "..dataobj.suffix
end
function Broker_Wallet:ADDON_LOADED(event, addon)
if addon ~= "Broker_Wallet" then return end
Broker_WalletDB = Broker_WalletDB or defaults
dataobj.suffix = suffix[Broker_WalletDB.displ]
dataobj.icon = icons[Broker_WalletDB.displ]
Update()
self:UnregisterEvent("ADDON_LOADED")
end
function Broker_Wallet:HONOR_CURRENCY_UPDATE()
Update()
end
function Broker_Wallet:PLAYER_ENTERING_WORLD()
Update()
end
function Broker_Wallet:BAG_UPDATE()
Update()
end
-------------------------------------------------------------------------------
-- LDB functions
function dataobj.OnTooltipShow(tooltip)
tooltip:AddLine("Broker_Wallet", 0,1,0, 0,1,0)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Honor Points:", GetHonorCurrency(), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Arena Points:", GetArenaCurrency(), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Alterac Valley Marks:", GetItemCount("20560"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Arathi Basin Marks:", GetItemCount("20559"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Eye of the Storm Marks:", GetItemCount("29024"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Strand of the Ancients Marks:", GetItemCount("42525"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Warsong Gulch Marks:", GetItemCount("20558"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Wintergrasp Marks:", GetItemCount("43589"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Stone Keeper's Shards:", GetItemCount("43228"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Badges of Justice:", GetItemCount("29434"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Emblems of Heroism:", GetItemCount("40752"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Emblems of Valor:", GetItemCount("40753"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddDoubleLine("Dalaran Jewelcrafter's Token:", GetItemCount("41596"), 1,1,1, 1,1,1)
tooltip:AddDoubleLine("Dalaran Cooking Token:", GetItemCount("43016"), 1,1,1, 1,1,1)
tooltip:AddLine(" ")
tooltip:AddLine("Click to toggle display.")
end
function dataobj.OnClick(self, button)
local displ = Broker_WalletDB.displ
Broker_WalletDB.displ = (displ == DISPL_MAX and 1) or (displ and displ + 1) or 1
dataobj.suffix = suffix[Broker_WalletDB.displ]
dataobj.icon = icons[Broker_WalletDB.displ]
Update()
end
I am in your debt Torhal. Thank you. As I said before, my coding skills are horrible. I think I understand what you did and why you did it. I will be putting you as co-author on the addon when I upload it. :)
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
That said, I am a coding n00b and I am having the darnedest time getting the currency frame to toggle on click. Any ideas?
Broker_Wallet is a simple currency display for LDB. You must use an addon such as Fortress, among others, to display.
Mouseover to view summary of currency tab, click to toggle and track a particular currency.
Thanks to the current creators of LDB addons for inspiration and letting me use your code. :)
Current Issues: May not update when you loot badges. Fix untested. Let me know if you see any issues.
What happens now is you pick a currency to track, log out, and when you log back in, it shows the correct amoutn of what you were tracking, but the icon is the default and the suffix says "Honor." What can I do?
I did see that your code could be greatly simplified, as:
Also:
In the TOC:
In the addon itself:
Note that this doesn't set default values and is a rather verbose usage, but it will save your values, once set, upon logout or /reloadui.
...or just get rid of that function altogether and directly use the table. A function that only returns the value in a table is less efficient because of the overhead of the function call.
EDIT: Gah. I shouldn't read forums when I wake up. There was an error in the original post. :)
I got the suffix function out of the way and I am using a table and it works beautifully. Thank you!
My problems are still stemming from my lack of understanding saved variables. This is what I see in the saved variables file:
Now, that display value is saving correctly and the addon is calling it correctly, but I am not entirely sure how or why it is doing that, but I digress.
In my OnClick function, i use Broker_Wallet.displ value to determine the icon, value, and suffix. Is there a way to make dataobj.icon and dataobj.suffix to fire when the addon is loading in for the first time? Do I really need to save the icon and suffix values to savedvariables?
...replacing SavedVariables with whatever name you assigned to them in the TOC.
[2008/11/27 11:34:28-1851-x1]: Broker_Wallet-1.3\Broker_Wallet.lua:126: attempt to index global 'Broker_Wallet' (a nil value)
Here is the code:
The "self" in Broker_Wallet:ADDON_LOADED() is referring to Broker_Wallet. Therefore, "self.displ = Broker_Wallet.displ" is the same as "self.displ = self.displ" and since Broker_Wallet.displ hasn't been defined at that point you get an error.
Here is Broker_Wallet.toc:
Here is the saved variables file:
and here are my errors from Bugsack:
I went ahead and re-worked your code and sectioned it in a sane manner. It now saves between sessions, and loads the value/text as well.
Broker_Wallet.lua:
I also added support for AddonLoader in the TOC.
Broker_Wallet.toc