I've created a very simple addon to see how saved variables work but the saved variables are not working.
Here is what I have so far:
My .toc file:
Lua Code:
## Title: MyAddon
## Interface: 100206
## Version: 1.0
## Author:
## Notes: A simple WoW addon using a database
## SavedVariablesPerCharacter: MyAddonDb
MyAddon.lua
and my lua code is:
Lua Code:
-- Create a saved variable to store last time
local lastLogoffTime = nil
-- Create a new frame
local frame = CreateFrame("Frame")
-- Load last logoff time from saved variable
local function LoadLastLogoffTime()
if MyAddonDB then
lastLogoffTime = MyAddonDB.lastLogoffTime
print("Last logoff time loaded:", lastLogoffTime)
else
print("Trying to load logoff time but MyAddonDB is nil.")
end
end
-- Save last time to saved variable
local function SaveLastLogoffTime(time)
if not MyAddonDB then
print("Trying to save logoff time but MyAddonDB is nil")
else
MyAddonDB.lastLogoffTime = time
end
end
-- Hook the Logout event to update last logoff time
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_LOGOUT")
frame:SetScript("OnEvent", function(self, event)
if event == "PLAYER_LOGIN" then
if not MyAddonDB then
MyAddonDB = {}
print ("MyAddonDB was nil")
end
LoadLastLogoffTime()
elseif event == "PLAYER_LOGOUT" then
SaveLastLogoffTime()
end
end)
-- Slash command handler function
local function SlashCommandHandler(msg)
LoadLastLogoffTime()
end
-- Register slash command
SLASH_MYADDON1 = "/myaddon"
SlashCmdList["MYADDON"] = SlashCommandHandler
When I login I see these messages:
My AddonDB was nil
Lastlogofftimeloaded: nil
and after logging out, the contents of the saved variable file 'MyAddon.lua' is:
MyAddonDb = nil
I've looked at so many posts but can't seem to figure out what I'm doing wrong.
I've logged out of my character and deleted the WTF saved variables file - didn't help.
I've restarted my computer in case the file was somehow preventing writing but no help.
I appreciate any help you can provide.