View Single Post
03-16-13, 02:09 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You can make that even more efficient by storing the player's GUID in a variable, and referring to that, instead of looking up the same value over and over again every time a combat log event occurs. The GUID isn't available before PLAYER_LOGIN on a fresh login, so you'll need to wait until then to populate the variable.

Similarly, in your OnUpdate function, you have this:
Code:
        local function onUpdate(self, elapsed)
                totalTime = totalTime + elapsed
                if totalTime >= updateInterval then
                        for k, v in pairs(damageTable) do
                                if k >= time() - 5 then
You could improve efficiency by looking up time() and subtracting 5 once per OnUpdate, instead of once per table entry per OnUpdate:
Code:
        local function onUpdate(self, elapsed)
                totalTime = totalTime + elapsed
                if totalTime >= updateInterval then
                        local t = time() - 5
                        for k, v in pairs(damageTable) do
                                if k >= t then
Also, it's kind of pointless to define your OnUpdate function, and then set it separately:

Code:
local function onUpdate(self, elapsed)
        -- stuff happening here
end
frame:SetScript("OnUpdate", onUpdate)
You can just define the function anonymously in the SetScript call:

Code:
frame:SetScript("OnUpdate", function(self, elapsed)
        -- stuff happening here
end)
You might also consider clearing out old table entries; otherwise after a few hours of play you'll have a pretty large table.

Code:
                        local t = time() - 5
                        for k, v in pairs(damageTable) do
                                if k < t then
                                        -- old, get rid of it
                                        damageTable[k] = nil
                                else
                                        -- within the window, use it
                                        totalDamage = totalDamage + v
                                end
                        end
__________________
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.

Last edited by Phanx : 03-16-13 at 02:17 AM.
  Reply With Quote