-
Notifications
You must be signed in to change notification settings - Fork 0
Supporting Both Character and Account‐Wide Settings
Many addons begin with a single storage model: either account-wide settings or character-specific settings. As an addon grows, users may request more flexibility:
- "I want my UI layout shared across all characters."
- "I want my crafting settings different for each character."
- "I want some settings shared and others customized."
LibSavedVars supports this scenario by allowing an addon to manage both account-wide and character-specific settings at the same time.
The LSV_Data class provides a unified interface that allows your addon to work with the active settings without needing to know where individual values are stored.
Different types of settings naturally belong in different places.
Good candidates include:
- Window positions.
- UI themes.
- Global addon preferences.
- Color schemes.
- Feature unlocks.
Example:
accountSettings.window.x = 250
accountSettings.theme = "Dark"Every character sees the same values.
Good candidates include:
- Character-specific builds.
- Crafting preferences.
- Combat configurations.
- Character notes.
- Per-character filters.
Example:
characterSettings.build = "Tank"Only the current character sees the change.
Create separate saved variables objects for each storage model.
local defaults = {
enabled = true,
scale = 1.0,
window = {
x = 100,
y = 100,
},
build = "",
}Create the account-wide settings:
local accountSettings = LibSavedVars:NewAccountWide(
"MyAddonSavedVariables",
1,
nil,
defaults
)Create the character settings:
local characterSettings = LibSavedVars:NewCharacterSettings(
"MyAddonSavedVariables",
1,
nil,
defaults
)At this point, the addon has two independent storage locations.
LSV_Data combines the two settings objects into a single interface.
local settings = LibSavedVars:NewData(
accountSettings,
characterSettings
)The addon now interacts with settings rather than directly accessing either saved variable table.
The addon can determine which storage model is currently active.
Account-wide:
settings:SetAccountSavedVarsActive(true)Character-specific:
settings:SetAccountSavedVarsActive(false)Retrieve the currently active settings:
local activeSettings = settings:GetActiveSavedVars()The returned table represents the storage model selected by the user.
Many addons provide an option such as:
Storage Mode:
( ) Account-wide
( ) Character-specific
When the user changes the option:
settings:SetAccountSavedVarsActive(useAccountWide)The addon immediately begins using the selected storage location.
The rest of the addon does not need to change.
Instead of:
if accountMode then
accountSettings.scale = 1.2
else
characterSettings.scale = 1.2
enduse:
settings.scale = 1.2LSV_Data handles the correct destination.
Sometimes users want both:
- Most settings character-specific.
- A few important settings shared.
For example:
Account-wide:
window position
theme
Character:
combat settings
filters
preferences
LibSavedVars supports this through pinned account settings.
settings:AddAccountWideToggle("window")
settings:AddAccountWideToggle("theme")Now:
settings.window.x = 300
settings.theme = "Dark"
settings.build = "Healer"stores:
Account-wide:
window
theme
Character:
build
The addon uses one table while LibSavedVars manages the storage separation.
Supporting both storage models is especially useful when changing an existing addon.
Example:
Version 1:
Account-wide:
all settings
Version 2:
Account-wide:
UI settings
Character:
gameplay settings
A migration can:
- Read the existing account-wide data.
- Copy selected values to character storage.
- Enable the new storage model.
Users keep their existing configuration without manually re-entering settings.
For addons supporting both storage models:
- Define a complete defaults table.
- Create account-wide and character saved variables.
- Create an
LSV_Dataobject. - Let users select their preferred storage model.
- Pin settings that should always remain shared.
- Access settings only through the
LSV_Datainterface.
Example:
local settings = LibSavedVars:NewData(
accountSettings,
characterSettings
)
settings:AddAccountWideToggle("window")
settings:AddAccountWideToggle("theme")
local active = settings:GetActiveSavedVars()
active.enabled = trueSupporting both account-wide and character-specific settings gives users greater control over how their addon configuration is stored.
LSV_Data removes the complexity of managing multiple storage locations by providing a single interface for:
- Account-wide settings.
- Character-specific settings.
- Mixed storage through pinned keys.
- Switching storage modes.
- Migrating between storage models.
For simple addons, a single storage model is usually enough. For addons with many settings or a diverse user base, supporting both models provides a more flexible and user-friendly experience.