View Single Post
11-11-16, 10:39 PM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
UNIT_INVENTORY_CHANGED fires when you equip, swap, or unequip an item. BAG_UPDATE is the event that fires when you loot, drop, or move items around in your bags. The idea of an event firing causing a memory leak is a misconception. Code not properly releasing objects that they allocated memory for is what causes memory leaks. Lua uses a garbage collection system that runs once in a while to clear out data that's no longer in use. The problem with your data usage wasn't how frequent the code was run, but recreating expensive data structures when you didn't need to. Furthermore, with UNIT_* functions, you can use :RegisterUnitEvent() instead of :RegisterEvent() to only respond to the event firing for a specific unit rather than everyone in your party/raid. For example, this is the bare minimum for your code to function properly and efficiently.

Lua Code:
  1. local KnowledgeMultipliers={
  2.     25, 50, 90, 140, 200,
  3.     275, 375, 500, 650, 850,
  4.     1100, 1400, 1775, 2250, 2850,
  5.     3600, 4550, 5700, 7200, 9000,
  6.     11300, 14200, 17800, 22300, 24900,
  7. };
  8.  
  9. local MainFrame=CreateFrame("Frame","M4xArtifactFrame",UIParent);
  10. local ExpText=MainFrame:CreateFontString(nil,"OVERLAY","GameFontHighlightSmall");
  11. ExpText:SetPoint("TOPLEFT",UIParent,"TOPLEFT");--   This is the object we're anchoring everything to
  12. ExpText:SetText("Initializing...");
  13.  
  14. --  This will twist your thinking, but you can set the points of your parents to a child as long as you don't create any circular references, as in points depending on each other.
  15. MainFrame:SetAllPoints(ExpText);
  16.  
  17. local Background=MainFrame:CreateTexture(nil,"BACKGROUND");
  18. Background:SetColorTexture(0,0,0,0.2);
  19. Background:SetAllPoints(ExpText);
  20.  
  21. MainFrame:RegisterUnitEvent("UNIT_INVENTORY_CHANGED","player");
  22. MainFrame:RegisterEvent("ARTIFACT_XP_UPDATE");
  23. MainFrame:SetScript("OnEvent",function(self,event,...)
  24.     local itemid,_,_,_,exp,lvl=C_ArtifactUI.GetEquippedArtifactInfo()
  25.     if itemid then
  26.         local available,tonext=0,C_ArtifactUI.GetCostForPointAtRank(lvl);
  27.         local _,knowledge=GetCurrencyInfo(1171);
  28.         while exp>=tonext do exp,lvl,available,tonext=exp-tonext,lvl+1,available+1,C_ArtifactUI.GetCostForPointAtRank(lvl+1); end
  29.  
  30. --      %5$d and %6$d force grab integers from the 5th and 6th parameters respectively
  31.         ExpText:SetFormattedText("AP |cff00ff00%d/%d (%.0f%%)|r"..(available>0 and " (+%d)" or "").."\nAK |cff00ff00%5$d (+%6$d%%)|r",exp,tonext,100*exp/tonext,available,knowledge,KnowledgeMultipliers[knowledge]);
  32.     end
  33.  
  34.     MainFrame:SetShown(itemid and true or false);-- Typecast to boolean
  35. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-11-16 at 10:45 PM.
  Reply With Quote