Description
LibSharedVariables-1.0
LibSharedVariables-1.0 is a lightweight storage helper for World of Warcraft addons.
It provides a merged db proxy view over per-character and account-wide saved variables,
with optional defaults for each scope and automatic table creation.
Features
- Merged read view across character DB, account DB, and optional defaults.
- Automatic creation of global saved variable tables (
<name>PCDBand<name>DB). - Smart write routing based on existing key ownership/default scope.
- Reusable handles by addon name (calling
Loadagain refreshes references/defaults). - Simple helper methods (
Get,Set) andpairs()support on mergeddb.
Quick Start
local LSV = LibStub("LibSharedVariables-1.0")
if not LSV then return end
local defaults = {
profileVersion = 1,
minimap = true,
}
local defaultsPC = {
firstRun = true,
}
local function handleOnLoad(db, handle)
if db.firstRun then
print("Welcome on this character")
db.firstRun = false
end
handle.accountDB.lastLoadedAt = date("%Y-%m-%d %H:%M:%S")
end
local handle = LSV:Load("MyAddon", defaults, defaultsPC, handleOnLoad)
Read and Write Behavior
Read lookup order for db[key] is:
- Character DB (
<name>PCDB) - Account DB (
<name>DB) - Character defaults (
defaultsPC) - Account defaults (
defaults)
Write behavior for db[key] = value:
- If key already exists in character DB, write to character DB.
- Else if key exists in
defaultsPC, write to character DB. - Else if key exists in
defaults, write to account DB. - Else write to character DB.
This allows account-style defaults and character-style defaults to naturally map to the expected persistence scope.
OnLoad Callback
LSV:Load("MyAddon", defaults, defaultsPC, function(db, handle)
if db.firstRun then
print("Welcome on this character")
db.firstRun = false
end
-- handle.accountDB and handle.charDB are direct table references
end)
Iteration
You can iterate merged keys using pairs(db). Duplicate keys are returned once,
respecting read precedence.
for key, value in pairs(db) do
print(key, value)
end
API
lib:Load(name, defaults?, defaultsPC?, onLoad?) -> handlehandle.db[key](merged reads/writes via metatable)handle:Get(key, default?) -> valuehandle:Set(key, value) -> value
handle fields:
handle.namehandle.accountDBhandle.charDBhandle.defaultshandle.defaultsPChandle.db
Notes
namemust be a non-empty string.onLoad, when provided, must be a function.- Calling
Loadagain with the samenamereuses the existing handle and refreshes DB table references/defaults. - Ensure your addon TOC declares matching saved variable names for persistence.
License
- GPL-3.0 - see LICENSE.
