Good morning,
the following segment of code is working exactly as intended, it just feels rather awkward, particularly the hcur = UnitHealth('player') or UnitHealth('vehicle') in the UNIT_HEALTH event. I imagine there's a better way to do this, any help would be greatly appreciated:
Lua Code:
local ht = CreateFrame('Button', 'PlayerHealthTextFrame', self)
ht:SetSize(50,10)
ht:SetPoint('LEFT', self, 'RIGHT', 7, 3)
ht:EnableMouse(false)
ht:RegisterEvent('PLAYER_ENTERING_WORLD')
ht:RegisterEvent('PLAYER_DEAD')
ht:RegisterEvent('PLAYER_ALIVE')
ht:RegisterUnitEvent('UNIT_HEALTH', 'player', 'vehicle')
ht:RegisterUnitEvent('UNIT_ENTERING_VEHICLE', 'player')
ht:RegisterUnitEvent('UNIT_ENTERED_VEHICLE', 'player')
ht:RegisterUnitEvent('UNIT_EXITING_VEHICLE', 'player')
ht:RegisterUnitEvent('UNIT_EXITED_VEHICLE', 'player')
local htt = ht:CreateFontString('PlayerHealthText', 'OVERLAY')
htt:SetAllPoints(true)
htt:SetFont(cfg.font, 11)
htt:SetShadowColor(0,0,0,1)
htt:SetShadowOffset(-1,-1)
htt:SetJustifyH('LEFT')
ht:SetScript('OnEvent', function(self, event, unit)
if event == 'PLAYER_DEAD' or event == 'PLAYER_ENTERING_WORLD' and UnitIsDead('player') then
htt:SetFormattedText('|cff%02x%02x%02xDead', 180, 0, 0)
elseif event == 'PLAYER_ALIVE' and UnitIsGhost('player') or event == 'PLAYER_ENTERING_WORLD' and UnitIsGhost('player') then
htt:SetFormattedText('|cff%02x%02x%02xGhost', 180, 0, 0)
else
local r, g, l, hcur, hmax
if event == 'PLAYER_ENTERING_WORLD' and UnitInVehicle('player') or event == 'UNIT_ENTERED_VEHICLE' or event == 'UNIT_ENTERING_VEHICLE' then
hcur, hmax = UnitHealth('vehicle'), UnitHealthMax('vehicle')
elseif event == 'PLAYER_ENTERING_WORLD' or 'UNIT_EXITING_VEHICLE' or 'UNIT_EXITED_VEHICLE' then
hcur, hmax = UnitHealth('player'), UnitHealthMax('player')
elseif event == 'UNIT_HEALTH' then
hcur = UnitHealth('player') or UnitHealth('vehicle')
hmax = UnitHealthMax('player') or UnitHealthMax('vehicle')
end
if hcur < hmax then
r, g = 180, 0
else
r, g = 0, 180
end
if hcur > 1000000 then
l = 'm'
hcur = round(hcur/1000000,1)
elseif hcur > 1000 then
l = 'k'
hcur = round(hcur/1000)
else
l = ''
end
htt:SetFormattedText('|cff%02x%02x%02x%s'..l, r, g, 0, hcur)
end
end)