feat: add SavedVariables persistence for Lua addons

Addons can now persist data across sessions using the standard WoW
SavedVariables pattern:

1. Declare in .toc: ## SavedVariables: MyAddonDB
2. Use the global in Lua: MyAddonDB = MyAddonDB or {default = true}
3. Data is automatically saved on logout and restored on next login

Implementation:
- TocFile::getSavedVariables() parses comma-separated variable names
- LuaEngine::loadSavedVariables() executes saved .lua file to restore globals
- LuaEngine::saveSavedVariables() serializes Lua tables/values to valid Lua
- Serializer handles tables (nested), strings, numbers, booleans, nil
- Save triggered on PLAYER_LEAVING_WORLD and AddonManager::shutdown()
- Files stored as <AddonDir>/<AddonName>.lua.saved

Updated HelloWorld addon to track login count across sessions.
This commit is contained in:
Kelsi 2026-03-20 12:22:50 -07:00
parent 5ea5588c14
commit 062cfd1e4a
9 changed files with 177 additions and 2 deletions

View file

@ -1,5 +1,11 @@
-- HelloWorld addon — demonstrates the WoWee addon system
-- Initialize saved variables (persisted across sessions)
if not HelloWorldDB then
HelloWorldDB = { loginCount = 0 }
end
HelloWorldDB.loginCount = (HelloWorldDB.loginCount or 0) + 1
-- Create a frame and register for events (standard WoW addon pattern)
local f = CreateFrame("Frame", "HelloWorldFrame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
@ -10,6 +16,7 @@ f:SetScript("OnEvent", function(self, event, ...)
local name = UnitName("player")
local level = UnitLevel("player")
print("|cff00ff00[HelloWorld]|r Welcome, " .. name .. "! (Level " .. level .. ")")
print("|cff00ff00[HelloWorld]|r Login count: " .. HelloWorldDB.loginCount)
elseif event == "CHAT_MSG_SAY" then
local msg, sender = ...
if msg and sender then
@ -23,6 +30,7 @@ SLASH_HELLOWORLD1 = "/hello"
SLASH_HELLOWORLD2 = "/hw"
SlashCmdList["HELLOWORLD"] = function(args)
print("|cff00ff00[HelloWorld]|r Hello! " .. (args ~= "" and args or "Type /hello <message>"))
print("|cff00ff00[HelloWorld]|r Sessions: " .. HelloWorldDB.loginCount)
end
print("|cff00ff00[HelloWorld]|r Addon loaded. Type /hello to test slash commands.")
print("|cff00ff00[HelloWorld]|r Addon loaded. Type /hello to test.")

View file

@ -1,4 +1,5 @@
## Interface: 30300
## Title: Hello World
## Notes: Test addon for the WoWee addon system
## SavedVariables: HelloWorldDB
HelloWorld.lua