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:
local LIB_MAJOR, LIB_MINOR = "LibTalentsTrack-1.0", 1
local LibTalentsTrack = LibStub:NewLibrary(LIB_MAJOR, LIB_MINOR)
if not LibTalentsTrack then return end
local libUnitsToTrack = {} -- the variable that saves the units to track and their info
function LibTalentsTrack:RegisterUnit(unitName) do
-- ... do stuff
...
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
return 0
end
function LibTalentsTrack:UnregisterUnit(unitName) do
-- ... do stuff
...
libUnitsToTrack[unitName] = nil
return 0
end
function LibTalentsTrack:UnregisterAllUnits(unitName) do
-- ... do stuff
...
for k, v in pairs(libUnitsToTrack) do
self:UnregisterUnit(unitName)
end
return 0
end
-- more functions etc...
...
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:
libUnitsToTrack[addonName][unitName] = = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
or like this:
Lua Code:
libUnitsToTrack[unitName] = {{}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
--where:
libUnitsToTrack[unitName][1] = {addon1, addon2, addon3, ...} -- In this [1] I store the addons that tracks this unit
instead of the actual:
Lua Code:
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:
-- this it's the addon code that wants use my lib
local LibTalentsTrack = LibStub("LibTalentsTrack-1.0")
IDKWhatShouldReturnHere = LibTalentsTrack:RegisterNewAddon("MyAddonName")
-- 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
LibTalentsTrack:RegisterUnit(unitName)
...
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.