Thread Tools Display Modes
08-01-14, 01:26 AM   #1
Ipood
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 14
Need help with LUA: PlaySoundFile when i get a buff

Hello, im writing a addon to play a soundfile whenever you get bloodlust or eqvivalent spell but i cannot get it to work, any help appriciated. The line
Code:
PlaySoundFile("Interface\\AddOns\\BLita\\Sounds\\BLita.mp3", "Master")
works as i have tested it with a macro ingame.

Can anyone tell me why it would not work?
Code:
local BLOODLUST_ID = 2825
local HEROISM_ID = 32182
local TIMEWARP_ID = 80353

local BLOODLUST = GetSpellInfo(HEROISM_ID)
local HEROISM = GetSpellInfo(BLOODLUST_ID)
local TIMEWARP = GetSpellInfo(TIMEWARP_ID)

local f = CreateFrame("Frame")

f:RegisterUnitEvent("UNIT_AURA", "player")

f:SetScript("OnEvent", function()
    local name = UnitBuff("player", BLOODLUST)
    if name == 1 then
        PlaySoundFile("Interface\\AddOns\\BLita\\Sounds\\BLita.mp3", "Master")
    end
end)

f:SetScript("OnEvent", function()
    local name = UnitBuff("player", HEROISM)
    if name == 1 then
        PlaySoundFile("Interface\\AddOns\\BLita\\Sounds\\BLita.mp3", "Master")
    end
end)

f:SetScript("OnEvent", function()
    local name = UnitBuff("player", TIMEWARP)
    if name == 1 then
        PlaySoundFile("Interface\\AddOns\\BLita\\Sounds\\BLita.mp3", "Master")
    end
end)
Appreciate your help!

/Ipood
  Reply With Quote
08-01-14, 03:55 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Your first problem is this:
Code:
    local name = UnitBuff("player", BLOODLUST)
    if name == 1 then
The first value returned by UnitBuff will never be the number 1, so this check will never pass.

Your second problem is that you can't set multiple OnEvent scripts on the same frame -- each one you set overwrites all the ones set before it.

Your third problem is that you're only checking if the player currently has the buff before playing the sound -- this will result in the sound playing over and over every time your buffs change, as long as the specific buff is still active. You need to keep track of the state for each buff, so you only play the sound when you first gain the buff.

Also you'd mixed up your Bloodlust and Heroism spell IDs when you get the names from GetSpellInfo.

Try this:

Code:
local state = {
	[(GetSpellInfo(2825))]  = false, -- Bloodlust
	[(GetSpellInfo(32182))] = false, -- Heroism
	[(GetSpellInfo(80353))] = false, -- Time Warp
}

local f = CreateFrame("Frame")
f:RegisterUnitEvent("UNIT_AURA", "player")
f:SetScript("OnEvent", function()
	for buff, active in pairs(state) do
		if UnitBuff("player", buff) then
			if not active then
				PlaySoundFile("Interface\\AddOns\\BLita\\Sounds\\BLita.mp3", "Master")
				state[buff] = true
			end
		elseif active then
			state[buff] = false
		end
	end
end)
__________________
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
08-01-14, 04:21 AM   #3
Ipood
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 14
Should really take course in coding :P

Trying to figure out how your code works now, but it does work! thank you!
I shall give you some credits when i upload the final addon
  Reply With Quote
08-01-14, 04:52 AM   #4
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
You are forgetting Ancient Hysteria.
  Reply With Quote
08-01-14, 04:56 AM   #5
Nynaeve
A Cobalt Mageweaver
 
Nynaeve's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 245
Phanx, as always your code is lovely and concise.

Since I no longer need to point anything else out, since Phanx explained so well, I want to tell the OP that he forgot the hunter pet version, Ancient Hysteria. Spell ID is 90355.

Code:
[(GetSpellInfo(90355))] = false, -- Ancient Hysteria
ETA: And beaten to the punch again. Really should stop doing things in the following order: open thread, go afk, return and begin to type.

Edit again: I just noticed all of us joined the site in 2006. That's a slightly strange coincidence.
__________________
"For in the plot we find more than just a man, we find the idea of that man, the spirit of that man, and that is what we must never forget." Evey (V)

Last edited by Nynaeve : 08-01-14 at 05:22 AM. Reason: Really should stop going afk after opening a thread
  Reply With Quote
08-01-14, 06:11 AM   #6
Nitrak
A Deviate Faerie Dragon
 
Nitrak's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 14
@Ipood if you want, you can just check out my addon.

It does exactly what you are looking for. It announces Bloodlust (and similar abilities) plus gives you a timer for the duration of the ability if you toggle it on

http://www.wowinterface.com/download...Assistant.html

And i see you already got plenty help for the code you linked, so i won't comment on that Phanx did a good job
  Reply With Quote
08-02-14, 02:19 AM   #7
Ipood
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 14
Thanks guys for all the help! And yeah I completely forgot about Ancient Hysteria!
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Need help with LUA: PlaySoundFile when i get a buff


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