View Single Post
08-18-13, 06:04 AM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Yeah, ditch that book. Based on your code, I'd have to say that book is incredibly outdated, and is doing you a lot more harm than good.

Anyway, as suspected, the problem is that you are hardcoding your frames/units -- eg. you always attach your first frame to the first raid frame, and assume that corresponds to the "raid1" unit based on your hardcoded table of units. This assumption will nearly always be wrong. Instead, you should do several things:

(1) Parent your frames to the relevant raid frames instead of to the UIParent.

(2) When you update one of your frames, figure out which raid frame it's parented to, and then look at that frame to determine the unit. For default raid frames, the currently displayed unit is stored on the frame itself under the "displayedUnit" key as shown in my earlier post. For ElvUI frames, you'll have to figure it out yourself.

On a side note, there are a lot of other issues in your code:

(1) Currently you are putting your "unitlist" and "buttonlist" variables in the global scope. Don't do that. Make them local instead, as there is no need for them to be accessible outside of this file.

In cases where you do need variables to be global, give them names that are (a) unique and (b) clearly identify them as part of your addon, eg. "TotalAbsorbs_UnitList", but this is not one of those cases.

(2) Currently you are updating all frames whenever (a) any aura changes on any unit, including units who are not group members, (b) your target changes, or (c) any group roster change occurs. This is hugely wasteful, especially (a) which can fire hundreds of times per second in a raid combat situation.

Instead, since your addon is only concerned with absorption amounts, you should ignore all of those events, and just respond to UNIT_ABSORB_AMOUNT_CHANGED, which fires specifically to tell you about changes in the amount of absorption on a unit. When the event fires, you do not need to update every single frame -- only the one connected to that unit.

(3) Instead of looking at a few auras to figure out the amount of absorption, you should use the API functions that exist specifically to tell you how much absorption the unit has from all sources:

Code:
local amount = UnitGetTotalAbsorbs(unit)
(4) The table.getn function is deprecated. You should use the # operator instead:

Code:
for i = 1, #unitlist do
Even if it wasn't deprecated, it's still a function call, which is slow and should be avoided whenever possible.

There are a lot of other minor issues, but none of them are critical and will just distract you from the important issues. I'd suggest starting over using pure Lua, proper parenting, and the absorb-specific event and API as described above.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote