Saved variables not working
I've created a very simple addon to see how saved variables work but the saved variables are not working.
Here is what I have so far: My .toc file: Lua Code:
and my lua code is: Lua Code:
When I login I see these messages: My AddonDB was nil Lastlogofftimeloaded: nil and after logging out, the contents of the saved variable file 'MyAddon.lua' is: MyAddonDb = nil I've looked at so many posts but can't seem to figure out what I'm doing wrong. I've logged out of my character and deleted the WTF saved variables file - didn't help. I've restarted my computer in case the file was somehow preventing writing but no help. I appreciate any help you can provide. |
Code:
MyAddonDB.lastLogoffTime = time Code:
MyAddonDB.lastLogoffTime = time() That and lua is case sensitive. MyAddonDb in the .toc is not the same as MyAddonDB in the code. |
Saved variables work now
Thank you Fizzlemizz!!
I changed the time to a date function and renamed the db file in the toc. Here is the final code, toc file first: Lua Code:
Lua Code:
|
Saving
Code:
MyAddonDB.lastLogoffTime = date("%m/%d/%y %H:%M:%S") No very useful in future if you just want the date part or just the time part (having to slice up a string). Probably better saving as Code:
MyAddonDB.lastLogoffTime = date() |
As an aside, all addons and the Blizzard UI share the same global table so any variable that will be global like frame names and SaveVariable names should be unique. Blizzard will use "common" names so addons should use things like prefixing with the addon name.
Saying this because MyAddonDB might be unknowingly used by any first time addon author that doesn't know. |
Here is updated code for the Saved Variables example
Thank you Fizzlemizz. Those are both very good points and I appreciate your thoughtful replies.
This code has been modified to use a unique database name and a more generic form of the date function. I might have overdone the db name a little bit, but it makes a good point. Here is the saved variables toc file: Lua Code:
Here is the saved variables lua file: Lua Code:
One more question, if I had an extremely unique database name that was 50 or 60 characters long, how could I set up an alias? Something like a local variable defined at the top and set in event PLAYER_LOGIN? |
After the gobal has been created you can alway do
Code:
local db = MyVeryLongGlobaVariableName Code:
db.SomeVariable = "SomeThing New" With other simple types (strings, numbers etc.) changing the local variable won't change the global. Global names don't have to be uber long, just unique. The addon (folder) name will always be unique (or acronym with something like _DATA after it.) locals are scope restricted so a local defined in anything the ends with an END (if, function etc.) will only be available in that scope. Code:
local a = 2 |
All times are GMT -6. The time now is 08:39 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI