Skip to content

LSV_Data (Hybrid Management)

Shadowfen edited this page Jul 29, 2026 · 1 revision

Most addons only need a single saved variables object created with NewAccountWide() or NewCharacterSettings(). For those cases, working directly with the saved variables table is the simplest approach.

Some addons, however, need greater flexibility. They may allow users to switch between account-wide and character-specific settings, share selected settings while keeping others character-specific, or provide a single interface regardless of where data is stored.

The LSV_Data class was designed for these advanced (hybrid) scenarios.

What is LSV_Data?

LSV_Data is a wrapper that manages one or more LibSavedVars objects and presents a unified interface for accessing the active settings.

Instead of your addon interacting directly with a particular saved variables table, it interacts with an LSV_Data object. LSV_Data determines which underlying saved variables are active and returns the appropriate values.

              Your Addon
                   │
                   ▼
              LSV_Data
              /      \
             ▼        ▼
     Account-wide   Character
       Settings      Settings

Your addon does not need to know which storage model is currently in use.

When Should You Use It?

LSV_Data is useful when your addon needs features such as:

  • Switching between account-wide and character-specific settings.
  • Sharing some settings while keeping others character-specific.
  • Managing multiple saved variable tables through a single interface.
  • Simplifying code that would otherwise need to distinguish between storage models.

If your addon always uses either account-wide or character-specific settings—but never both—you probably do not need LSV_Data.

Creating an LSV_Data Object

Begin by creating both saved variable tables.

local defaults = {
    enabled = true,
    scale = 1.0,
}

local accountSettings = LibSavedVars:NewAccountWide(
    "MyAddonSavedVariables",
    1,
    nil,
    defaults
)

local characterSettings = LibSavedVars:NewCharacterSettings(
    "MyAddonSavedVariables",
    1,
    nil,
    defaults
)

Next, create the LSV_Data object.

local settings = LibSavedVars:CreateData()

settings:SetAccountSavedVars(accountSettings)
settings:SetCharacterSavedVars(characterSettings)

Your addon now works with settings rather than directly accessing either saved variables table.

Selecting the Active Settings

LSV_Data can return either the account-wide or character settings depending on which storage model is active.

local active = settings:GetActiveSavedVars()

active.enabled = true

Your addon code remains the same regardless of where the data is actually stored.

Switching Storage Models

Changing between account-wide and character settings is straightforward.

settings:SetAccountSavedVarsActive(true)

or

settings:SetAccountSavedVarsActive(false)

After switching, calls to GetActiveSavedVars() automatically return the newly selected saved variables.

Pinned Account Settings

Some settings should always remain account-wide, even when the addon is using character-specific settings.

Examples include:

  • Window positions
  • Account-wide unlocks
  • Global preferences
  • User interface themes

LSV_Data supports pinned account settings, allowing specific keys to remain in the account-wide saved variables while all other settings use character storage.

For example:

settings:AddAccountWideToggle("window")
settings:AddAccountWideToggle("theme")

When character settings are active:

  • window is read from the account-wide saved variables.
  • theme is read from the account-wide saved variables.
  • Everything else comes from the character saved variables.

Your addon continues accessing the data normally.

local active = settings:GetActiveSavedVars()

active.window.x = 200
active.theme = "Dark"
active.opacity = 0.8

LSV_Data automatically retrieves each value from the correct location.

Iterating Through Settings

LSV_Data provides an iterator that presents a merged view of the active settings.

This is particularly useful when account-wide settings are pinned, since your addon can iterate over a single collection without worrying about where each value is stored.

for key, value in settings:GetIterator() do
    d(key, value)
end

Migrating Between Storage Models

If your addon originally stored settings in one location but later changes its design, LibSavedVars includes migration helpers that move data between account-wide and character-specific storage.

For example:

  • Account-wide → Character settings
  • Character → Account-wide settings

These helpers preserve existing user settings while updating the storage model.

Migration helpers are described in the migration reference.

Best Practices

  • Use LSV_Data only when your addon genuinely needs to manage multiple saved variable tables.
  • Access settings through GetActiveSavedVars() rather than directly through the underlying saved variables.
  • Pin only those settings that should always remain account-wide.
  • Keep storage decisions centralized in LSV_Data so the rest of your addon remains storage-agnostic.

Summary

LSV_Data provides a powerful abstraction for addons that need more than a single saved variables table. By managing account-wide and character-specific settings through one interface, it simplifies addon code while supporting flexible storage models, pinned account settings, and seamless transitions between them.

For many addons, LSV_Data is unnecessary. But when users need the freedom to choose how settings are stored, it provides a clean and maintainable solution without complicating the rest of your code.

Clone this wiki locally