Yeap, I got how it works!
But, I still got some questions before I proceed any further.
How do you think of this kind of code structure?
Lua Code:
local animal
local InitObjects, UpdateObjects
function InitObjects()
local frame = CreateFrame("Frame");
frame:RegisterUnitEvent("UNIT_POWER_FREQUENT", "player", "vehicle")
function frame:OnEvent(event, ...)
if event == "UNIT_POWER_FREQUENT" then
self:UNIT_POWER_FREQUENT(...)
end
end
function frame:UNIT_POWER_FREQUENT(...)
UpdateObjects()
end
MyModule.frame = frame
end
function UpdateObjects()
-- ...
end
function MyModule:OnInitialize()
local state = MyAddon.db.global[MyModule:GetName()]
self:SetEnabledState(state)
end
function MyModule:OnEnable()
animal = animal or "dog"
if not self.frame then
InitObjects()
else
self.frame:Show()
end
end
function MyModule:OnDisable()
animal = nil
if self.frame then
self.frame:Hide()
end
end
I'm slightly against creating a function within a function, but there doesn't seem to be any other solutions to create a frame, its event functions and :SetScript("OnEvent", ...) when OnIntialize function is called to avoid frames being created in spite of the modules are disabled on Login.
Of course I could create those functions in local, but if possible I would like them to be subordinated to the frame.