P.S. Logic errors are a bitch to debug because you (usually) don't get any error messages.
On that same note, to debug logic errors the easiest thing to do is break it apart into as many as needed "If then else end"s as needed or break all your variable movement into one step at a time and use print() & then optimize from there.
function guildLoot_OnLoad(self)
self:Hide()
self:RegisterEvent("CHAT_MSG_MONEY")
self:RegisterEvent("ADDON_LOADED")
end
function guildLoot_OnEvent(self, event, msg)
if event == "ADDON_LOADED" then
if event == "CHAT_MSG_MONEY" then
local _, guild = msg:match( "(.+)%((.+)%)" )
local gold = guild:match(goldLoot) or 0
local silver = guild:match(silverLoot) or 0
local copper = guild:match(copperLoot) or 0
local currentTotal = (gold*1000) + (silver*100) + copper
currentTotal = (currentTotal or 0) + currentTotal
lootAmounts:SetText("You have looted " .. GetCoinTextureString( currentTotal ) .. " for your guild bank." )
end
end
end
Because it isn't printing the currentTotal to the frame now.
Why would event equal one thing and then, with nothing changed, equal something else on the next line of code?
Good point ;)
function guildLoot_OnEvent(self, event, msg)
if event == "ADDON_LOADED" then
print("GuildLoot has been loaded.")
end
if event == "CHAT_MSG_MONEY" then
local _, guild = msg:match( "(.+)%((.+)%)" )
local gold = guild:match(goldLoot) or 0
local silver = guild:match(silverLoot) or 0
local copper = guild:match(copperLoot) or 0
local currentTotal = (gold*1000) + (silver*100) + copper
currentTotal = (currentTotal or 0) + currentTotal
upgradeTotal = currentTotal
lootAmounts:SetText("You have looted " .. GetCoinTextureString( upgradeTotal ) .. " for your guild bank." )
end
end
Caused it to print it was loaded 8 time's, and the loot info now double's each loot amount, and still doesn't add to the next loot.
That's because with this line of code your doubling it. What you want to do is not double it, but set your SV to it. You might be running into scoping issues if your variable names are all the same.
local currentTotal = 0 -- if you are using a SavedVariables variable, delete this line and change all of [I]currentTotal[/I] to it.
function guildLoot_OnEvent(self, event, msg)
if event == "CHAT_MSG_MONEY" then
local guild = tostring(msg:match( "%((.+)%)" )) -- to string makes [I]guild [/I]equal to string "nil" rather than constant [I]nil[/I] if nothing is found which will make the following three lines fail silently rather than loudly.
local gold = guild:match(goldLoot) or 0
local silver = guild:match(silverLoot) or 0
local copper = guild:match(copperLoot) or 0
currentTotal = currentTotal + (gold*1000) + (silver*100) + copper -- [I]currentTotal [/I]is 0 when the event fires the first time and gets added to new loots each time to keep a running total.
lootAmounts:SetText( "You have looted " .. GetCoinTextureString( currentTotal ) .. " for your guild bank.") -- edited from the original
end
end
Edit:
If using a SavedVariables variable, you can use the ADDON_LOADED, PLAYER_ENTERING_WORLD, or VARIABLES_LOADED event to make sure it's 0.
if <whichever event> then
SavedVariablesName = SavedVariablesName or 0
end
Edit again: I'm not sure if the variables are available when ADDON_LOADED fires.
I just answered my own question, but would this be it?
function guildLoot_OnEvent(self, event, msg)
if event == "ADDON_LOADED" then
keepVarTot = keepVarTot or 0
else
if event == "CHAT_MSG_MONEY" then
local _, guild = msg:match( "(.+)%((.+)%)" )
local gold = guild:match(goldLoot) or 0
local silver = guild:match(silverLoot) or 0
local copper = guild:match(copperLoot) or 0
keepVarTot = (keepVarTot or 0) + (gold*1000) + (silver*100) + copper
lootAmounts:SetText("You have looted " .. GetCoinTextureString( keepVarTot ) .. " for your guild bank." )
elseif event == "PLAYER_LOGOUT" then
keepVarTot = keepVarTot or 0
end
end
end
http://paste.wowace.com/3027/ ----As this is just a pastey, copy and paste in to a file.
---- do a find and replace on ' SAVED_VARIABLE_NAME '
---- with the actual name of the saved variable in the ToC File.
Is your cookie. It accounts for things like different toons being in different guilds and even changing guilds mid session.
It also adds in using LDB. So if this particular problem with gold and events ties into your other thread about minimap buttons n' such... then this paste should solve it :)
On that same note, to debug logic errors the easiest thing to do is break it apart into as many as needed "If then else end"s as needed or break all your variable movement into one step at a time and use print() & then optimize from there.
Because it isn't printing the currentTotal to the frame now.
Why would event equal one thing and then, with nothing changed, equal something else on the next line of code?
Good point ;)
Caused it to print it was loaded 8 time's, and the loot info now double's each loot amount, and still doesn't add to the next loot.
Stopped the doubling, but it still does add the previous and new amount's and print the sum.
That's because with this line of code your doubling it. What you want to do is not double it, but set your SV to it. You might be running into scoping issues if your variable names are all the same.
This makes my brain hurt.
Please fix your tabbing when posting to the forum, rather interesting to read code blocks that are all over the place
..
ok a few ways to go about this, depending on preference and practice.
the "SavedVariable" here is the actual variable in the ToC file being saved, or somewhere on a table that is being saved to said SavedVariable
I would do this instead.
Or scroll back up to this post and use that.
Edit:
If using a SavedVariables variable, you can use the
ADDON_LOADED, PLAYER_ENTERING_WORLD, or VARIABLES_LOADED event to make sure it's 0.Edit again: I'm not sure if the variables are available when ADDON_LOADED fires.
Maybe i should make a mental note to tell me, not to write lua coding when tired.
You guy's should have seen the monstrosity of an idea i was about to try before checking the thread lol.
I've seen worse. At least he's trying. lol
And that did work, and it's now adding correctly.
Would i need to call for a PLAYER_LOGOUT, to make sure the AddOn save's the variable, or will that not matter?
Sweet, I'm not the worst, lol.
----As this is just a pastey, copy and paste in to a file.
---- do a find and replace on ' SAVED_VARIABLE_NAME '
---- with the actual name of the saved variable in the ToC File.
Is your cookie. It accounts for things like different toons being in different guilds and even changing guilds mid session.
It also adds in using LDB. So if this particular problem with gold and events ties into your other thread about minimap buttons n' such... then this paste should solve it :)
Now i can't say i did this.
And you have done it, because of me asking question's lol. :/
/edit
But thank's for the cookie :)
But has your understanding increased ?
that is all that really matters :)
Yes, yes it has.