Quantcast Healthbar changing colors based on % - glitch on target death - WoWInterface
Thread Tools Display Modes
10-24-19, 04:09 PM   #1
cbjewelz
A Murloc Raider
Join Date: Oct 2019
Posts: 4
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)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Healthbar changing colors based on % - glitch on target death

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off