Quantcast Better Saved Variables Tutorial? - WoWInterface
 
Thread Tools Display Modes
11-04-08, 03:20 PM   #1
HyperGig
A Cyclonian
Join Date: Jan 2008
Posts: 46
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)

Last edited by HyperGig : 11-04-08 at 03:25 PM.
  Reply With Quote
11-04-08, 05:03 PM   #2
Cralor
Mmm... cookies!!!
 
Cralor's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 772
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
or
SavedVariablesPerCharacter: TESTDB
Second, you need to set up the default variables to save.
Example:
Code:
TESTDB = {
    variable1 = true;
    variable2 = true;
    variable3 = true;
    scale = 1.5;
    parent = "UIParent";
    point = "CENTER";
    parentPoint = "CENTER";
    x = 0;
    y = -100;
};
Third, you need to set it up so that you load the saved variables.
Example:
Code:
function FRAME_Update()
    FRAME:ClearAllPoints()
    FRAME:SetPoint(TESTDB.point, TESTDB.parent, TESTDB.parentPoint, TESTDB.x, TESTDB.y)
    FRAME:SetScale(RuneMoverDB.scale);
    if (FRAME:IsMouseEnabled()) then
        TESTDB.point, relativeTo, TESTDB.parentPoint, TESTDB.x, TESTDB.y = FRAME:GetPoint();
    end
end
(NOTE: This will vary drastically based on what variables you are looking to save. In this example, I have a frame's position saved and it's scale.)

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
__________________
Never be satisfied with satisfactory.

Last edited by Cralor : 11-04-08 at 05:07 PM.
  Reply With Quote
11-04-08, 05:23 PM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
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)
  Reply With Quote
11-05-08, 08:42 AM   #4
HyperGig
A Cyclonian
Join Date: Jan 2008
Posts: 46
Where do i put the
Code:
if (not my_var) then my_var = "default Val" end
part?
  Reply With Quote
11-05-08, 09:32 AM   #5
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Where do i put the
Code:
if (not my_var) then my_var = "default Val" end
In your function which is fired on "ADDON_LOADED" or "VARIABLES_LOADED".

The code I generally use for saved variables goes as follows (although I have started mucking about with metatables):

Code:
local addon = CreateFrame("Frame")
addon:RegisterEvent("ADDON_LOADED") --I use "ADDON_LOADED" because the majority of my mods use "AddonLoader".

local function OnEnable(self, event, addon)
	if addon ~= "my addon name" then return end
	
	if not AddonDB then
		AddonDB = {<defaults here>}
	end
	self:UnregisterEvent("ADDON_LOADED")

	<Register any events you want to register here>
	self:SetScript("OnEvent", MyEventHandler)
end

addon:SetScript("OnEvent", OnEnable)
Hope this helps you.
  Reply With Quote
11-05-08, 09:55 AM   #6
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,696
Originally Posted by HyperGig View Post
Where do i put the
Code:
if (not my_var) then my_var = "default Val" end
part?
much easier/cleaner would be like this:
Code:
TESTDB = TESTDB or {defaulttable}
  Reply With Quote
11-05-08, 10:22 AM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,690
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.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
11-05-08, 10:39 AM   #8
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Seerah View Post
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?
You can use metatables to set a default value if no value for that key is found.

i.e.

Code:
local defaults = {
	foo = "bar",
	tbl = {
		cat = "meow",
		dog = "bark",
		cow = "moo"
	}
}

local myDB = setmetatable(myDB or {}, {__index = defaults})
so if I were to do
Code:
print(myDB.foo)
then it would output "bar", and
Code:
print(myDB.tbl.cow)
would output "moo".

but then if I were to do
Code:
myDB.tbl.cow = "asleep"
then
Code:
print(myDB.tbl.cow)
would output "asleep".

Hope this helps.

Last edited by Slakah : 11-05-08 at 10:57 AM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Better Saved Variables Tutorial?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off