So, as a part of my small project I'm working on a functionality to toggle modules depending on player's current specialization and here's a brief draft of what I've done so far:
Lua Code:
local specList = {}; -- Proxy
local _specList = {};
local f = CreateFrame("Frame");
f:RegisterEvent("PLAYER_LOGIN");
f:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_LOGIN" then
local currentSpecIndex = GetSpecialization();
for index = 1, GetNumSpecializations() do
local id = GetSpecializationInfo(index);
_specList[id] = {};
specList[id] = setmetatable({
__parent = specList, -- to access parent table
_specList = _specList,
},
{
__index = function(t, k)
local specList = t.__parent;
local _specList = t._specList;
for sK, sV in pairs(specList) do
if t == sV then
for _sK, _sV in pairs(_specList) do
if sK == _sK then
return _sV[k];
end
end
end
end
end,
__newindex = function(t, k, v)
if k == "enabled" and v then
local specList = t.__parent;
local _specList = t._specList;
for sK, sV in pairs(specList) do
if t == sV then
for _sK, _sV in pairs(_specList) do
_sV.enabled = sK == _sK;
end
break;
end
end
end
end,
});
end
local id = GetSpecializationInfo(currentSpecIndex);
specList[id].enabled = true;
self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED");
self:UnregisterEvent("PLAYER_LOGIN");
elseif event == "ACTIVE_TALENT_GROUP_CHANGED" then
local id = GetSpecializationInfo(GetSpecialization());
specList[id].enabled = true;
end
end);
It's working perfectly well as I expected, BUT all those nasty for loops & if statements look so messy and I'm pretty sure I would forget what the hxxk I've done here in the close future.
(I'll be changing the variable names and leave comments, tho...)
It would be grateful if someone could come up with better ideas.
Thank you!