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-15-16, 11:44 AM   #1
millanzarreta
A Deviate Faerie Dragon
 
millanzarreta's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2010
Posts: 11
[LUA] How to do this library works for more than 1 addon at the same time

Hi, I create a new library with the purpose to track some units to check what talents are they using (enemy units), but I have one problem to separate the dates from the diferents addons that use it.

When I started to create it, I believed all addons that use it would retrieve a different "copy" of my addon library, but no, using LibStub only one Library object it's created and used on all addons (this make sense to save resources, but I had not noticed...)

My code looks similar to this (example code to show concept):

Lua Code:
  1. local LIB_MAJOR, LIB_MINOR = "LibTalentsTrack-1.0", 1
  2.  
  3. local LibTalentsTrack = LibStub:NewLibrary(LIB_MAJOR, LIB_MINOR)
  4. if not LibTalentsTrack then return end
  5.  
  6. local libUnitsToTrack = {}  -- the variable that saves the units to track and their info
  7.  
  8. function LibTalentsTrack:RegisterUnit(unitName) do
  9.     -- ... do stuff
  10.     ...
  11.     libUnitsToTrack[unitName] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} --the 0s are the talents tiers and pvp talents tiers
  12.     return 0
  13. end
  14.  
  15. function LibTalentsTrack:UnregisterUnit(unitName) do
  16.     -- ... do stuff
  17.     ...
  18.     libUnitsToTrack[unitName] = nil
  19.     return 0
  20. end
  21.  
  22. function LibTalentsTrack:UnregisterAllUnits(unitName) do
  23.     -- ... do stuff
  24.     ...
  25.     for k, v in pairs(libUnitsToTrack) do
  26.         self:UnregisterUnit(unitName)
  27.     end
  28.     return 0
  29. end
  30.  
  31. -- more functions etc...
  32. ...

The problem it's all addons have the same object, so, for example, if addon #1 register some units, and addon #2 register other units, and addon #2 then calls UnregisterAllUnits, this action deletes all the units (including units registered by addon #1 and #2). My intention is if the addon #2 calls UnregisterAllUnits only addon #2 unit's should be removed.

Ok, how can I deal with this? For the data variable "libUnitsToTrack" I know that I can do a structure like this:

Lua Code:
  1. libUnitsToTrack[addonName][unitName] =  = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

or like this:

Lua Code:
  1. libUnitsToTrack[unitName] = {{}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  2. --where:
  3. libUnitsToTrack[unitName][1] = {addon1, addon2, addon3, ...}  -- In this [1] I store the addons that tracks this unit

instead of the actual:

Lua Code:
  1. libUnitsToTrack[unitName] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

But the problem it's how I can easy manage this since the addon part, I don't like add one more argument (the addon name) for all "RegisterUnit" and "UnregisterUnit" functions. I really have many functions to register and unregister units (allowing the addon that use this library register units by unitId, unitName, unitGUID, etc...). I prefer a solution more simplest and more transparent for the user. I like some like this but I don't know how I can do this:

Lua Code:
  1. -- this it's the addon code that wants use my lib
  2. local LibTalentsTrack = LibStub("LibTalentsTrack-1.0")
  3. IDKWhatShouldReturnHere = LibTalentsTrack:RegisterNewAddon("MyAddonName")
  4.  
  5. -- and now the addon can use the functions without pass the addon name as argument, and the library function know what addon call's the function
  6. LibTalentsTrack:RegisterUnit(unitName)
  7.  
  8. ...

Some way to do this? I don't know if I have explained well :S

Thanks for the help!

TL;DR: I am doing a library. I am looking for some way to use a global library object in diferents addons and I wants the library functions know the addon that which were called.
  Reply With Quote