View Single Post
04-26-17, 01:48 AM   #23
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
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:
  1. local animal
  2.  
  3. local InitObjects, UpdateObjects
  4.  
  5. function InitObjects()
  6.     local frame = CreateFrame("Frame");
  7.     frame:RegisterUnitEvent("UNIT_POWER_FREQUENT", "player", "vehicle")
  8.  
  9.     function frame:OnEvent(event, ...)
  10.         if event == "UNIT_POWER_FREQUENT" then
  11.             self:UNIT_POWER_FREQUENT(...)
  12.         end
  13.     end
  14.  
  15.     function frame:UNIT_POWER_FREQUENT(...)
  16.         UpdateObjects()
  17.     end
  18.  
  19.     MyModule.frame = frame
  20. end
  21.  
  22. function UpdateObjects()
  23.     -- ...
  24. end
  25.  
  26. function MyModule:OnInitialize()
  27.     local state = MyAddon.db.global[MyModule:GetName()]
  28.  
  29.     self:SetEnabledState(state)
  30. end
  31.  
  32. function MyModule:OnEnable()
  33.     animal = animal or "dog"
  34.  
  35.     if not self.frame then
  36.         InitObjects()
  37.     else
  38.         self.frame:Show()
  39.     end
  40. end
  41.  
  42. function MyModule:OnDisable()
  43.     animal = nil
  44.  
  45.     if self.frame then
  46.         self.frame:Hide()
  47.     end
  48. 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.

Last edited by Layback_ : 04-26-17 at 02:04 AM.
  Reply With Quote