Lua Code:
local AddOnPath=("Interface\\AddOns\\%s\\"):format(...);-- Dynamically fetch our addon path
local EventFrame=CreateFrame("Frame");
EventFrame:RegisterEvent("PLAYER_LOGIN");
EventFrame:RegisterUnitEvent("UNIT_PET","player");
EventFrame:RegisterUnitEvent("UNIT_HEALTH","pet");
local LowHealthThreshold=0.35;
local LastHealthValue=0;
EventFrame:SetScript("OnEvent",function(self)
if UnitExists("pet") then-- Check if pet exists
local cur,max=UnitHealth("pet"),UnitHealthMax("pet");
if cur and max and max>0 then-- Check if we have health data
if cur<=0 and LastHealthValue>0 then-- Is pet dead?
PlaySoundFile(AddOnPath.."Res\\PHA.ogg");
elseif cur/max<=LowHealthThreshold and LastHealthValue/max>LowHealthThreshold then-- Is pet low health?
PlaySoundFile(AddOnPath.."Res\\PHA.ogg");
end
LastHealthValue=cur;-- Update tracking value
else LastHealthValue=0; end-- Reset to zero if health not available yet
else LastHealthValue=0; end-- Reset to zero if no pet
end)