Thank you Fizzlemizz. Those are both very good points and I appreciate your thoughtful replies.
This code has been modified to use a unique database name and a more generic form of the date function.
I might have overdone the db name a little bit, but it makes a good point.
Here is the saved variables toc file:
Lua Code:
## Title: MyAddon
## Interface: 100206
## Version: 1.0
## Author:
## Notes: A simple WoW addon using a database
## SavedVariablesPerCharacter: MyAddonDBExampleMarch2024 --must be unique
MyAddon.lua
Here is the saved variables lua file:
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 MyAddonDBExampleMarch2024 then
lastLogoffTime = MyAddonDBExampleMarch2024.lastLogoffTime
print("Last logoff time loaded:", lastLogoffTime)
else
print("Trying to load logoff time but MyAddonDBExampleMarch2024 is nil.")
end
end
-- Save last time to saved variable
local function SaveLastLogoffTime()
if not MyAddonDBExampleMarch2024 then
print("Trying to save logoff time but MyAddonDBExampleMarch2024 is nil")
else
MyAddonDBExampleMarch2024.lastLogoffTime = date()
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 MyAddonDBExampleMarch2024 then
MyAddonDBExampleMarch2024 = {}
print ("MyAddonDBExampleMarch2024 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
One more question, if I had an extremely unique database name that was 50 or 60 characters long, how could I set up an alias?
Something like a local variable defined at the top and set in event PLAYER_LOGIN?