View Single Post
02-14-18, 10:59 PM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Toggling modules with proxy table

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:
  1. local specList = {}; -- Proxy
  2. local _specList = {};
  3.  
  4. local f = CreateFrame("Frame");
  5. f:RegisterEvent("PLAYER_LOGIN");
  6. f:SetScript("OnEvent", function(self, event, ...)
  7.     if event == "PLAYER_LOGIN" then
  8.         local currentSpecIndex = GetSpecialization();
  9.  
  10.         for index = 1, GetNumSpecializations() do
  11.             local id = GetSpecializationInfo(index);
  12.  
  13.             _specList[id] = {};
  14.  
  15.             specList[id] = setmetatable({
  16.                 __parent = specList, -- to access parent table
  17.                 _specList = _specList,
  18.             },
  19.             {
  20.                 __index = function(t, k)
  21.                     local specList = t.__parent;
  22.                     local _specList = t._specList;
  23.  
  24.                     for sK, sV in pairs(specList) do
  25.                         if t == sV then
  26.                             for _sK, _sV in pairs(_specList) do
  27.                                 if sK == _sK then
  28.                                     return _sV[k];
  29.                                 end
  30.                             end
  31.                         end
  32.                     end
  33.                 end,
  34.                 __newindex = function(t, k, v)
  35.                     if k == "enabled" and v then
  36.                         local specList = t.__parent;
  37.                         local _specList = t._specList;
  38.  
  39.                         for sK, sV in pairs(specList) do
  40.                             if t == sV then
  41.                                 for _sK, _sV in pairs(_specList) do
  42.                                     _sV.enabled = sK == _sK;
  43.                                 end
  44.  
  45.                                 break;
  46.                             end
  47.                         end
  48.                     end
  49.                 end,
  50.             });
  51.         end
  52.  
  53.         local id = GetSpecializationInfo(currentSpecIndex);
  54.         specList[id].enabled = true;
  55.  
  56.         self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED");
  57.  
  58.         self:UnregisterEvent("PLAYER_LOGIN");
  59.     elseif event == "ACTIVE_TALENT_GROUP_CHANGED" then
  60.         local id = GetSpecializationInfo(GetSpecialization());
  61.         specList[id].enabled = true;
  62.     end
  63. 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!

Last edited by Layback_ : 02-14-18 at 11:21 PM.
  Reply With Quote