Thread Tools Display Modes
04-30-21, 09:31 AM   #1
Yukka
A Theradrim Guardian
 
Yukka's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2020
Posts: 60
Sound when pet dies

Hello world, I play BM hunter and sometimes (especially Ashran) my pet dies and I'm too distracted to see it... Can someone share the code to create an addon that play a sound when pet dies? Thanks in advance.
  Reply With Quote
04-30-21, 10:00 AM   #2
DahkCeles
A Cliff Giant
 
DahkCeles's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2020
Posts: 73
I don't play hunter, so I'm not quite sure if a dead pet is a 'corpse' (zero health, would trigger UNIT_HEALTH) or despawns (no longer exists, would trigger UNIT_PET).

This code is just a guess -- I haven't tested it -- to cover both possible cases.

Lua Code:
  1. local f = CreateFrame("Frame")
  2. f:RegisterUnitEvent("UNIT_HEALTH", "pet")
  3. f:RegisterUnitEvent("UNIT_PET", "player")
  4. f:SetScript("OnEvent", function()
  5.   if not UnitExists("pet") or UnitHealth("pet") == 0 then
  6.     PlaySound(SOUNDKIT.LOOT_WINDOW_COIN_SOUND)
  7.   end
  8. end)

Change the soundkit for something on this list (I just took the first one):
https://github.com/Gethe/wow-ui-sour...tConstants.lua
  Reply With Quote
04-30-21, 10:38 AM   #3
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
I play a hunter so will take a look see.
__________________
  Reply With Quote
04-30-21, 11:32 AM   #4
Yukka
A Theradrim Guardian
 
Yukka's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2020
Posts: 60
Nice, it works amazing

But where can I see the list of all sounds to choose another one?
  Reply With Quote
04-30-21, 11:34 AM   #5
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Originally Posted by Yukka View Post
Nice, it works amazing

But where can I see the list of all sounds to choose another one?

Glad you got it working for you. My everyday fighting that didn't kill the pet didn't trigger any of the events for me rofl. So maybe it only triggers on death after combat.

To get the other sounds, the link under the code that was provided will give you the different sounds the game uses.
__________________
  Reply With Quote
04-30-21, 11:43 AM   #6
Yukka
A Theradrim Guardian
 
Yukka's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2020
Posts: 60
Oh nice I didnt saw it, thanks.
My pet was killed by the dummy at Boralus and there was a coin sound.
UI_LEGENDARY_LOOT_TOAST is a nice sound.

Edit: I changed the sound and deleted f:RegisterUnitEvent("UNIT_PET", "player") because this part plays the sound a second time when I use the Revive Pet ability. Just f:RegisterUnitEvent("UNIT_HEALTH", "pet") is enough.

local f = CreateFrame("Frame")
f:RegisterUnitEvent("UNIT_HEALTH", "pet")
f:SetScript("OnEvent", function()
if not UnitExists("pet") or UnitHealth("pet") == 0 then
PlaySound(SOUNDKIT.UI_LEGENDARY_LOOT_TOAST)
end
end)

Last edited by Yukka : 04-30-21 at 12:09 PM.
  Reply With Quote
04-30-21, 12:36 PM   #7
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by Yukka View Post
Oh nice I didnt saw it, thanks.
My pet was killed by the dummy at Boralus and there was a coin sound.
UI_LEGENDARY_LOOT_TOAST is a nice sound.

Edit: I changed the sound and deleted f:RegisterUnitEvent("UNIT_PET", "player") because this part plays the sound a second time when I use the Revive Pet ability. Just f:RegisterUnitEvent("UNIT_HEALTH", "pet") is enough.
You are not limited to that soundkit constant table. If you want to explore all of the possible sound IDs, you can browse https://www.wowhead.com/sounds and use the number in the URL for the sound page. For example, the "level up" sound is at https://www.wowhead.com/sound=888/levelup. The number in the URL is 888 so you would use PlaySound(888) to play it.

Examples I use for some stuff is PlaySound(23331) which has a few random "engineering doodad" fun sounds. I also use the old major/minor glyph sounds for special events PlaySoundFile(567404)PlaySoundFile(567487) I don't remember which one is which, but played together, they sound great.

Notice how those last two say "PlaySoundFile" instead. Sound IDs, which PlaySound uses, are basically indexes to a scripted table of sounds, some going to single sounds, others going to a group of randomly played sounds. File IDs, however, correlate directly to a file. File IDs are a little more tricky to get if you don't know what you're doing. wow.tools is one of the best tools available. That specific link takes you to the main sound table for the game. Searching names or IDs gets you File IDs in the FileDataID columns.

Going back to that engineering doodad ID, let's search for that. Now we know that ID plays one of five random sounds.

Last edited by Kanegasi : 04-30-21 at 01:11 PM.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Sound when pet dies

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