(4 Kb)
Updated: 04-16-09 07:16 PM
Updated:04-16-09 07:16 PM
Created:10-26-08 03:34 PM


Version: 2.1
by: AnduinLothar [More]

This small library lets you register saved variable defaults which it sets only if the variable is nil when it loads. You can also register functions to fire after the variables of an addon have been loaded and initialized.

For advanced usage, you can also set n-deep table value defaults. If the containing tables don't exist they will be automatically created.

lib:SetDefault(addonName, varName, ...)
Register a saved variable's default value.
For non-tables:
if the variable is nil once the addon's saved variables are loaded, it will be set to the default value.
For tables:
The variable will be set to an empty table which is meta-index-linked to the stored default table.
Any time you access keys in the table the default will be returned, unless you set that key to a non-default value.
This saves space in live memory because the defaults only reside in one place.
With the drawback that #, pairs and ipairs will not count the default values.

SetDefault also gives you the option to build your defaults on the fly without creating the containing tables.
It can handle N-depth table values by providing any number of keys. ex: _G["SavedVarName"][key1][key2] = value

The addon saved variable defaults will be initialized at the addon's ADDON_LOADED event,
or they can be forced to load using lib:InitAddOn(addonName).

lib:SetDefault("MyAddOn", "SavedVar(Table)Name", [key1, [key2, [...] ] ], value)
Example Usage:
local lib = LibStub("LibDefaults")
lib:SetDefault("MyAddOn", "SavedVarName", SavedVarValue)

lib:SetScript(addonName, func)
Register a function to be executed once an addon's variables have been loaded.
Fires after addon defaults have been initialized.

lib:SetScript("MyAddOn", function)
Example Usage:
local lib = LibStub("LibDefaults")
lib:SetScript("MyAddOn", function(addonName) dostuff() end)

lib:Reset(addonName, varName)
Reset you addon's global variable back to default.
Also recursively resets all table values and sub-tables.

lib:SetDefault("MyAddOn", "SavedVar(Table)Name")
Example Usage:
local lib = LibStub("LibDefaults")
lib:SetDefault("MyAddOn", "SavedVarName")

- Fixed a bug that broke non-table defaults

- Added Meta-Indexing
Defaults are only saved in the default storage, not in the main table,
but still allows you to get them from the main table. Saves on run-time memory!
Defaults wont be accessable via pairs() from the main table,
unless you set the value to a non-default and then back to a default.
- Added Default Unloading
Removes defaults from the saved variables so that they aren't saved to disk. Speed up load time, yay!
- Added lib:Reset(addonName, varName) to reset an addons variable to default (and all table values to default)

- Hopefully one version is enough!
Archived Files (1)
File Name
10-29-08 10:07 PM

There have been no comments posted to this file.
Be the first to add one.

Category Jump: