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
Well, this is pretty bad, but I've seen even worse than this. Hell, I've done worse than this.
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
oh yes, you can mix logic and math together like that :)
it's not the logic i was concerned about, it was the use of strings in math operations. i guess lua does an implied "tonumber()" on math operands? i wasn't sure. i know i've gone absolutely crazy pulling "numbers" from strings and using them as things like table indices and couldn't for the life of me figure out why things weren't working right.
if event == "VARIABLES_LOADED" then
if arg1 == "GuildLoot" then
print("GuildLoot has been loaded.")
currentTotal = currentTotal or 0
print( currentTotal )
end
end
Nothing print's. It should at least print that the AddOn has been loaded, correct?
local defaults = {
guildLoot_currentTotal = 0
}
guildLoot_currentTotal = guildLoot_currentTotal or 0
guildLoot_currentTotal.variable = guildLoot_currentTotal.variable + 1
But now, if you are running BugSack + BugGrabber, and possibly using the default blizz lua error display, you will receive some "error's" but they don't affect the AddOn at all. It will still keep track of amount of money looted, and save it perfectly fine.
SavedVariables are only saved to harddisc if you do a UI reload ('/script ReloadUI()'), log out or exit the game via ingame interface. There is no other way to save securely SavedVariables.
EDIT:
The SavedVariables are automatically saved as long as you have '## SavedVariables: blabla' or '## SavedVariablesPerCharacter: blabla' in your toc. Simply change the blabla variable in your code and it will be saved at UIreload/logout/exit.
Btw: use a better name (something like MyAddon_Options or MyAddonDB instead of currentTotal. blabla is global variable)...
ADDON_LOADED with arg1 == "YourAddonName" is the (imo) proper way to check when your addon has fully loaded, including saved variables. Anything you do to the saved variables after this point will be saved and written to the saved variables files automatically once you logout or reload UI. You don't have to do anything to make your saved variables work except specifying it in the ToC file.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
Well, this is pretty bad, but I've seen even worse than this. Hell, I've done worse than this.
it's not the logic i was concerned about, it was the use of strings in math operations. i guess lua does an implied "tonumber()" on math operands? i wasn't sure. i know i've gone absolutely crazy pulling "numbers" from strings and using them as things like table indices and couldn't for the life of me figure out why things weren't working right.
In my .toc it is:
and to save it i tried:
But it just reload's at the restart of the AddOn.
This is line 36:
This is getNew:
currentTotal is the SavedVariablesPerCharacter.
print( currentTotal:toNumber() ) give's the "attempt to index global 'currentTotal' (a boolean value)" error.
Also, i changed it back to:
And got the:
This is why you have the error in the other thread. Remove the "PLAYER_LOGOUT" code entirely.
Also, don't use "currentTotal" as a SavedVariables variable. I would recommend "guildLoot_currentTotal".
Nothing needs to be done to your SV when you logout, the client takes care of it.
Nothing print's. It should at least print that the AddOn has been loaded, correct?
It still renew's back to 0 in the event of logging out and back in.
But now, if you are running BugSack + BugGrabber, and possibly using the default blizz lua error display, you will receive some "error's" but they don't affect the AddOn at all. It will still keep track of amount of money looted, and save it perfectly fine.
EDIT:
The SavedVariables are automatically saved as long as you have '## SavedVariables: blabla' or '## SavedVariablesPerCharacter: blabla' in your toc. Simply change the blabla variable in your code and it will be saved at UIreload/logout/exit.
Btw: use a better name (something like MyAddon_Options or MyAddonDB instead of currentTotal. blabla is global variable)...
Does not do anything magical. Don't even bother putting this in your code if you're not going to reference it later.