mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-22 23:30:14 +00:00
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.
36 lines
1.3 KiB
Lua
36 lines
1.3 KiB
Lua
-- 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")
|
|
f:RegisterEvent("CHAT_MSG_SAY")
|
|
|
|
f:SetScript("OnEvent", function(self, event, ...)
|
|
if event == "PLAYER_ENTERING_WORLD" then
|
|
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
|
|
print("|cff00ff00[HelloWorld]|r " .. sender .. " said: " .. msg)
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- Register a custom slash command
|
|
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.")
|