Thread Tools Display Modes
06-20-14, 10:15 AM   #1
ImportedDj
A Deviate Faerie Dragon
Join Date: Jun 2014
Posts: 10
Need help with Player and Target Health Bars

Hey guys, Just need a little bit of help here i have a target health bar and a player health bar, Currently the Player health bar is filled out and working as intended.. but the Target bar is not.. Right now its just visible and Does not fill in.. Its just an empty box.. Is anyone able to explain to me how to add in the target frame.. Picture to explain what i mean.. I actually have no idea how to remove the default UI, if someone could help with that, it would also be exteremly helpful!

Here is my current Healthbar code.. If you think something could be change to make this more optimal please let me know how i can change things..

Code:
function CreateHealthbar(unit)
local bar = CreateFrame('StatusBar', nil, UIParent)
bar:SetMovable(true)
bar:EnableMouse(true)
bar:RegisterForDrag("LeftButton")
bar:SetScript("OnDragStart", bar.StartMoving)
bar:SetScript("OnDragStop", bar.StopMovingOrSizing)
bar:SetPoint('CENTER')
bar.unit = unit -- Variable for the unitID to watch so we only have to change it in one location

-- Style it
bar:SetSize(200, 23)
bar:SetBackdrop({bgFile = [[Interface\DialogFrame\UI-DialogBox-Background-Dark]]})
bar:SetStatusBarTexture([[Interface\TARGETINGFRAME\UI-StatusBar]], 'ARTWORK') --Background image
bar:SetOrientation('HORIZONTAL')
bar:SetBackdropColor(144/255, 47/255, 223/255, 1)

local fill = bar:GetStatusBarTexture()
fill:SetVertexColor(0.0,0.3,0.0)

local spark = bar:CreateTexture(nil, 'ARTWORK', nil, 3)
spark:SetTexture([[Interface\CastingBar\UI-CastingBar-Spark]]) -- Image at the end of the Healthbar to specify where current health..
spark:SetBlendMode('ADD')
spark:SetPoint('CENTER', fill, 'RIGHT')
spark:SetSize(8, 23)


--Overlay, This is needed in order to add a border to the health bar
local overlay = CreateFrame('frame', nil, bar)
overlay:SetAllPoints()

local border = overlay:CreateTexture(nil, 'OVERLAY', nil, 2)
border:SetPoint('CENTER')
border:SetWidth(bar:GetWidth()) -- Add +# past () to make it larger
border:SetHeight(bar:GetHeight()) -- ^
border:SetTexture([[Interface\AddOns\WoWUI\Background\NaTUI_HealthFrame]])
bar.border = border

bar:SetScript('OnEvent', function(self, event, ...)
	if event == 'UNIT_MAXHEALTH' then
		self:SetMinMaxValues(0, UnitHealthMax(self.unit)) -- Update max value for the bar
	elseif event == 'UNIT_HEALTH' or event == 'UNIT_HEALTH_FREQUENT' then
		self:SetValue(UnitHealth(self.unit)) -- Update current value for the bar to reflect our health
	elseif event == 'PLAYER_ENTERING_WORLD' then -- Update everything after a loading screen
		self:SetMinMaxValues(0, UnitHealthMax(self.unit))
		self:SetValue(UnitHealth(self.unit))
	end
end)

-- Register events for the unit we're interested in, creates the update of the Health bar

bar:RegisterUnitEvent('UNIT_MAXHEALTH', bar.unit)
bar:RegisterUnitEvent('UNIT_HEALTH', bar.unit)
bar:RegisterUnitEvent('UNIT_HEALTH_FREQUENT', bar.unit)
bar:RegisterEvent('PLAYER_ENTERING_WORLD')

-- Add some text
local pctText = bar:CreateFontString(nil, 'OVERLAY')
pctText:SetFont('Fonts\\FRIZQT__.ttf', 10, "THINOUTLINE")
pctText:SetPoint('LEFT', bar, 'LEFT', 2, 0)
pctText:SetTextColor(1, 1, 1)
pctText:SetShadowOffset(1, -1)

local maxText = bar:CreateFontString(nil, 'OVERLAY')
maxText:SetFont('Fonts\\FRIZQT__.ttf', 10, "THINOUTLINE")
maxText:SetPoint('LEFT', border, 'RIGHT', -35, 0)
maxText:SetTextColor(1, 1, 1)
maxText:SetShadowOffset(1, -1)

local name = bar:CreateFontString(nil, 'OVERLAY')
name:SetFont('Fonts\\FRIZQT__.ttf', 10, "THINOUTLINE")
name:SetPoint('CENTER', border, 2, 0)
name:SetTextColor(1, 1, 1)
name:SetShadowOffset(1, -1)

--sets player name in health bar
local PlayerName = UnitName(bar.unit)
name:SetText(PlayerName)

--Update text when our value changes
local function UpdateText(self) -- Set percentage and maxhealth text 
	local currentHealth, maxHealth = UnitHealth(self.unit), UnitHealthMax(self.unit)
	pctText:SetFormattedText('%d%%', ceil(100 * currentHealth / maxHealth))
	maxText:SetText(AbbreviateLargeNumbers(maxHealth))
end

bar:SetScript('OnValueChanged', UpdateText) -- Keeps the text on the screen updated.
bar:SetScript('OnMinMaxChanged', UpdateText)

end

local PlayerBar = CreateHealthbar('Player')

local TargetBar = CreateHealthbar('Target')
  Reply With Quote
06-20-14, 01:48 PM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I don't think you'll get the UNIT_MAXHEALTH event just by switching target. Try using PLAYER_TARGET_CHANGED (or whatever it is) and set the MinMax values then.

As for removing the default frames, you could try this:
Code:
PlayerFrame:UnregisterAllEvents()
PlayerFrame:Hide()
TargetFrame:UnregisterAllEvents()
TargetFrame:Hide()
__________________
Grab your sword and fight the Horde!

Last edited by Lombra : 06-20-14 at 01:50 PM.
  Reply With Quote
06-20-14, 08:29 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You can take a look at oUF to see how it handles hiding the Blizzard unit frames. Specifically, it re-parents the frames to a hidden frame so they don't re-appear, and also unregisters events from child frames like the health and power bars to avoid wasting time running their event handlers:

https://github.com/haste/oUF/blob/master/blizzard.lua
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » Need help with Player and Target Health Bars


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