Quantcast
WoWInterface - View Single Post - [LUA] How to do this library works for more than 1 addon at the same time
View Single Post
12-16-16, 11:23 AM   #5
millanzarreta
A Deviate Faerie Dragon
 
millanzarreta's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2010
Posts: 11
Ohh, that code looks nice... Can I modify it to not use the TrackerMeta object directly as data store? Can I use the TrackerMeta to store only the addon name (and the functions) and use this addon name to access a local variables when I wants?

I can't test this now, but I am looking some like this (modified version of your code):

lua Code:
  1. --[[    Library Registration    ]]
  2. local LibTalentsTrack=LibStub:NewLibrary("LibTalentsTrack-1.0",1);
  3. if not LibTalentsTrack then return; end
  4.  
  5. --[[    Internal Variables  ]]
  6. local MasterList=setmetatable({},{__mode="v"});--   Generate table with weak values to let GarbageCollection take them as necessary
  7.  
  8. --[[    Tracker Object Metatable    ]]
  9. local TrackerMeta={__index={}};
  10.  
  11. local libUnitsToTrack = {}          --accesed libUnitsToTrack[addonName][guid]
  12. local libUnitsToDoSomething2 = {}   --accesed libUnitsToDoSomething2[addonName][guid]
  13. local libUnitsToDoSomething3 = {}   --accesed libUnitsToDoSomething3[addonName][guid]
  14.  
  15. function TrackerMeta.__index:RegisterUnit(unit)
  16. --  self is our tracker object table given to addons
  17.     if UnitExists(unit) then
  18.         local guid=UnitGUID(unit);--    Save by GUID to guarantee correct tracking
  19.         local addonName = self["addonName"]
  20.         libUnitsToTrack[addonName][guid] = {0, 0, 0} --my default data
  21.     end
  22. end
  23.  
  24. function TrackerMeta.__index:UnregisterUnit(unit)
  25. --  self is our tracker object table given to addons
  26.     if UnitExists(unit) then
  27.         local addonName = self["addonName"]
  28.         libUnitsToTrack[addonName][UnitGUID(unit)]=nil;-- Erase data
  29.     end
  30. end
  31.  
  32. function TrackerMeta.__index:UnregisterAllUnits(unit)
  33. --  self is our tracker object table given to addons
  34.     local addonName = self["addonName"]
  35.     table.wipe(libUnitsToTrack[addonName]);-- Erase everything
  36. end
  37.  
  38. --  Generates new tracker object for addons to use (if exists, return the current tracker for that addon)
  39. function LibTalentsTrack:NewTracker(addonName)
  40.     if addonName == nil then return end
  41.     local existTracker=MasterList[addonName];
  42.     if not existTracker then
  43.         local tracker = setmetatable({},TrackerMeta);
  44.         tracker["addonName"] = addonName
  45.         MasterList[addonName]=tracker["addonName"]
  46.         return tracker
  47.     end
  48.     return existTracker
  49. end
  50.  
  51. function LibTalentsTrack:DeleteTracker(tracker)
  52.     if tracker == nil then return end
  53.     local addonName = tracker["addonName"]
  54.     table.wipe(tracker)
  55.     tracker = nil
  56.     MasterList[addonName]=nil
  57. end
  58.  
  59. --Do stuff without problems with MasterList and GarbageCollector

If this is possible, it's easy for me programming the rest of library and I can use infinity variables to store info (with your code my data it's limited to the TrackerMeta object). I could manage complicated think like MasterList only on NewTracker and DeleteTracker functions and forget about this in the rest of the code. On every function that I needs differentiation I only should use local addonName = self["addonName"] and no more complications by metatables and GarbageCollector. It's this possible?

Forgive me if I pretend do some in wrong way, but the use of metatables and metafunctions are difficult to undestand for me :S

And one more question... what it's the purpose for the MasterList? I know it's for GarbageCollection, but, I don't know why it's needed...

Seriously, thank you very much, your help is helping me a lot!
  Reply With Quote