The addon loading process of the client is as follow:
- (Load all dependencies, if needed)
- Load, parse and execute all XML and lua files listed in the TOC, in order.
- Load the SavedVariables files, if needed
- Broadcast the ADDON_LOADED event for this addon to the UI
- (Load all addons that are declared as LoadWith for this addon)
If your TOC is like this:
Code:
## SavedVariables: MyAddonDB
main.lua
And your main.lua file is like this:
Code:
MyAddonDB = {
count = 0,
}
local function test ()
print (MyAddonDB.count)
MyAddonDB.count = MyAddonDB.count + 1
end
test()
local f = CreateFrame"Frame"
f:SetScript("OnEvent", function (self, event, addon)
if addon == "MyAddon" then
test()
end
end)
f:RegisterEvent"ADDON_LOADED"
The first time the addon is loaded, it will print "0" then "1"
The second time the addon is loaded, it will print "0" then "
2"!
Why ?
The first time, the SavedVariables file is empty, and thus the "MyAddonDB" file is not overwritten.
The second time, the SavedVariable file exists, with a count of 2, so the "MyAddonDB" variable is overwritten by the one in this file.
Of course, when saving the variables after the second run, MyAddonDB.count is 3, so the next time the addon is loaded, it will print "0" then "3".