WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Healthbar changing colors based on % - glitch on target death (https://www.wowinterface.com/forums/showthread.php?t=57631)

cbjewelz 10-24-19 04:09 PM

Healthbar changing colors based on % - glitch on target death
 
Hi all,

New to Lua/WoW API coding so I've been modifying the existing whoathickframes lua files with great success. However, I'm running into a mysterious problem. I created working code that changes the color of my healthbar based on my % health remaining, from green to red. It works except that right after an enemy dies, the color changes to the blizzard default green, and then on the next tick it correctly color codes based on my rules. My guess is I'm missing the correct hook function? Any help much appreciated. Here is the current code:

Code:

--        Player class colors.
function whoaUnitClass(healthbar, unit, value)
        --print(unit);
        if unit ~= 'player' and UnitIsPlayer(unit) and UnitIsConnected(unit) and UnitClass(unit) and (cfg.classColor) then
                _, class = UnitClass(unit);
                local c = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class];
                healthbar:SetStatusBarColor(c.r, c.g, c.b);
        elseif UnitIsPlayer(unit) and (not UnitIsConnected(unit)) then
                healthbar:SetStatusBarColor(0.5,0.5,0.5);
        else
                --healthbar:SetStatusBarColor(0,0,1);
                local min,max = healthbar:GetMinMaxValues()

                if not value or value<min or value>max then
                    return
                  end
          -- change value to a percentage
                  value = (max-min)>0 and (value-min)/(max-min) or 0

          -- recolor based on percentage
                  if value>0.5 then
                    healthbar:SetStatusBarColor((1-value)*2,0.9,0)
                    --healthbar:SetStatusBarColor(0,0,1)
                  else
                    healthbar:SetStatusBarColor(1,value*2,0)
                    --healthbar:SetStatusBarColor(1,0,0)
                  end
        end
end

hooksecurefunc("UnitFrameHealthBar_Update", whoaUnitClass)
hooksecurefunc("HealthBar_OnValueChanged", function(self,value)
        whoaUnitClass(self, self.unit, value)
end)



All times are GMT -6. The time now is 04:44 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI