Thread Tools Display Modes
04-25-17, 10:17 PM   #21
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
To add to Seerah's post, about local functions, frames, etc: if SmartRes2's core option panel is toggled to disable a module, then AceAddon-3.0 will fire an internal callback for that module called Disable. The reverse is also true for enabling, called Enable. These respectively provide OnDisable and OnEnable to the addon developer. See the example below.

Just like you would for any other Ace3 addon, in your module, you would have functions myModule:OnEnable() and myModule:OnDisable() where you would show or hide frames, clear variables, wipe tables, etc.

SmartRes2 does not need to know what those frames are called, or which variable does what within any module. The module controls all of its internal workings.

Just be careful about using nil on everything. It is better to make sure something exists before you recreate it. The following is a short example of the correct way.
Lua Code:
  1. local animal
  2. local frame = CreateFrame('Frame', 'frameName', parentFrame)
  3.  
  4. function MyModule:OnEnable()
  5.     animal = animal or "dog"
  6.     frame:Show()
  7. end
  8.  
  9. function MyModule:OnDisable()
  10.     animal = nil
  11.     frame:Hide()
  12. end

Last edited by myrroddin : 04-25-17 at 11:24 PM. Reason: clarity and correction
  Reply With Quote
04-25-17, 11:23 PM   #22
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Sorry, I have an error above. It is AceAddon-3.0 that fires the Enable/Disable callbacks, and AceEvent-3.0 has nothing to do with that process.

AceEvent-3.0 is totally irrelevant to the discussion at this point.

I will edit the above post.
  Reply With Quote
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

WoWInterface » Developer Discussions » Lua/XML Help » Any possible/alternative ways to access SV at the top of the file?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off