Better Saved Variables Tutorial?
The new guy here (again) and i was looking for better Saved Variables Tutorial. The only one that i see is the one from wowwiki which has ## Interface: 1300 in its demo toc file, which is slightly unnerving , and i don't think is all that clear. What did you peoplez use when you where getting started?
http://www.wowwiki.com/Saving_variab..._game_sessions Thanks in advanced!! (again) :banana: |
I'm still new to this, but I think I have the gist of it.
First, you need to enable the SV in the TOC (shown in that tutorial). Code:
SavedVariables: TESTDB Example: Code:
TESTDB = { Example: Code:
function FRAME_Update() Lastly, you need to update the saved variables if the user wishes to change them. This is the basics of it. I hope this helped you a little. I am sorry if I have said something wrong. I am still really new :) |
the simple version is that global variables that you indicate in your .toc will get saved in whatever state they're in at logout/reload (after PLAYER_LEAVING_WORLD, etc. fire, so you can change them at the last second if you want to).
they are then restored after your addon is loaded. note that the *after* means that OnLoad handlers, etc. will run before the saved variables are loaded (see the events ADDON_LOADED and VARIABLES_LOADED) |
Where do i put the
Code:
if (not my_var) then my_var = "default Val" end |
Quote:
The code I generally use for saved variables goes as follows (although I have started mucking about with metatables): Code:
local addon = CreateFrame("Frame") |
Quote:
Code:
TESTDB = TESTDB or {defaulttable} |
In the past I have used AceDB-3.0 for working with my saved variables. In my current project, I wanted to try not using it. One problem that I have bumped into while using MYVAR = MYVAR or defaults is this:
When I add a new variable to be saved after MYVAR has been created, MYVAR doesn't pick it up. I get errors because even though it's defined in the defaults table, the addon is looking for it in MYVAR. Which is the best way to go about this? edit: and P3lim, I don't think you need to put defaulttable in brackets {}, as it is already a table. |
Quote:
i.e. Code:
local defaults = { Code:
print(myDB.foo) Code:
print(myDB.tbl.cow) but then if I were to do Code:
myDB.tbl.cow = "asleep" Code:
print(myDB.tbl.cow) Hope this helps. |
that last part confused me, where assigning a new value to the existing var still returned the old value, but...
I will look into metatables. I haven't worked with them yet. Just to clarify, my issue was with creating new options during development. Say I had Code:
defaults = { Code:
MYMODDB = MYMODDB or defaults Code:
print(MYMODDB.scale) After playing with the code for a while, I decide I want to add an alpha option as well. So my defaults table changes to this Code:
defaults = { Code:
f:SetAlpha(MYMODDB.alpha) Code:
f:SetAlpha(MYMODDB.alpha) Code:
print(MYMODDB.alpha) I probably didn't need to explain myself, as I'm sure you knew what I meant, and I will read up on metatables. :) Thanks, Slakah. :) |
Quote:
|
Okies - was wondering if it was a typo :p
|
Just checking your db table with:
Code:
MYMODDB = MYMODDB or defaults The problem is it only checks if the table exists, not that all keys that are in the default table also exist in the MYMODB table. So even if you add for example an alpha key to your defaults table, the table loaded from the saved variables where there is no alpha key will still be used. Something like this should work: Code:
for k,v in pairs(defaults) do |
Thank you everyone for all your help... that why i love this community, everyone is always so helpful.
So the general consensus is to load previously / set default saved vars via the ADDON_LOADED event? Right now i can't do this unless I have a OnMouseUp on my frame set to executed another function that sets/loads the saved/default vars. |
Quote:
That metatable thing looks *amazing.* I wish I hadn't been intimidated by the underscores when I first stumbled upon the concept, it looks like quite the ideal solution. |
Okay... a follow up... :)
This did not work for me. Gave me a PocketPlotDB is nil error. Code:
local PocketPlotDB = setmetatable(PocketPlotDB or {}, {__index = defaults}) Code:
PocketPlotDB = PocketPlotDB or {} This, modeled after what was shown in WoW Programming, gave me the same result as above. Worked for everything *except* the background color subtable. Code:
PocketPlotDB = PocketPlotDB or {} I finally went with conner686's method (since I do it at PLAYER_LOGIN anyhow) and it works perfectly. Code:
for k,v in pairs(defaults) do |
I'd like to use metatables for default settings, but I never got it to work. Instead, I do a version check at VARIABLES_LOADED, and if the user has an older version, I do something like this:
Code:
function MyAddOn.AddNewSettings(settings, defaults) |
look at this if you look for an easy way how to store settings and maybe if you need easy localisation: http://github.com/tekkub/addontemplate/tree/master
the way tekkub does it it will only save changed settings.. it will remove values from the table if they are default on logout. However this will not work if you want to use subtables. |
Too my knowledge :
Code:
local PocketPlotDB = setmetatable(PocketPlotDB or {}, {__index = defaults}) Code:
PocketPlotDB = PocketPlotDB or {} Code:
function printvar() As for your other problem of the metatable not being applied to existing substables, you could do this: Code:
local function DeepMetatable(tbl, defaulttbl) --I couldn't think of a better name Whereas the other Code:
PocketPlotDB = setmetatable(PocketPlotDB or {}, {__index = defaults}) I think the reason why the previous method I posted didn't work because the __index metatable wasn't applied to pre-existing subtables, so if those pre-existing subtables were missing values there was no __index metatable to get the default value. Hope this helps. |
Thanks for the tips btw, gonna implement it for pMinimap now (sick of all those bugreports people toss at me because of nil variables)
|
Quote:
Code:
PocketPlotDB = PocketPlotDB or defaults Anyway, the inpairs solution works for me. :) Thanks. :) |
All times are GMT -6. The time now is 05:42 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI