Thread Tools Display Modes
08-20-10, 04:19 PM   #1
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Help with this script (health/mana alerts)

I miss MSBT's low health/mana alerts since I don't use it anymore and decided to recreate it myself with a small addon.

Code:
local playedhp, playedmp
local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event, unit, ...)
	if not unit=="player" then return end
	if event=="UNIT_HEALTH" then
		if UnitHealth("player") < (UnitHealthMax("player")/100)*30 then
			if playedhp ~= true then
				playedhp = true
				PlaySoundFile("Interface\\AddOns\\!FreeUI\\media\\LowHealth.mp3")
			end
		else
			playedhp = false
		end
	elseif event=="UNIT_MANA" then
		if UnitPower("player") < (UnitPowerMax("player")/100)*30 then
			if playedmp ~= true then
				playedmp = true
				PlaySoundFile("Interface\\AddOns\\!FreeUI\\media\\LowMana.mp3")
			end
		else
			playedmp = false
		end
	end
end)
f:RegisterEvent("UNIT_HEALTH")
f:RegisterEvent("UNIT_MANA")
The problem is that, say, when the health drops to below 30%, it won't play the sound instantly; it will play the sound after the health is updated *again* (OOC regen/healing/...). I'm fairly clueless as to why. The way I see it now, as soon as the health drops below 30%, 'playedhp' becomes true and the sound file should be instantly played. But that doesn't seem to be happening.

I didn't test mana yet but I'm fairly sure it's the same.

Could anyone help me out here?
  Reply With Quote
08-20-10, 05:47 PM   #2
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Code:
local health, power
local addon = CreateFrame('Frame')
addon:RegisterEvent('UNIT_HEALTH')
addon:RegisterEvent('UNIT_MANA')
addon:SetScript('OnEvent', function(self, event, unit)
	if(unit ~= 'player') then return end

	if(event == 'UNIT_HEALTH') then
		if(not health and (UnitHealth(unit) / UnitHealthMax(unit)) < 30) then
			health = true
			PlaySoundFile([=[Interface\AddOns\!FreeUI\media\LowHealth.mp3]=])
		else
			health = false
		end
	elseif(event == 'UNIT_MANA') then
		if(not power and (UnitPower(unit) / UnitPowerMax(unit)) < 30) then
			power = true
			PlaySoundFile([=[Interface\AddOns\!FreeUI\media\LowMana.mp3]=])
		else
			power = false
		end
	end
end)
I'm bored tbh..

You should also consider registering more events for the power (energy/rage etc).

Last edited by p3lim : 08-20-10 at 05:52 PM.
  Reply With Quote
08-20-10, 05:50 PM   #3
Cralor
Mmm... cookies!!!
 
Cralor's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 772
Originally Posted by p3lim View Post
Code:
local health, power
local addon = CreateFrame('Frame')
addon:RegisterEvent('UNIT_HEALTH')
addon:RegisterEvent('UNIT_MANA')
addon:SetScript('OnEvent', function(self, event, unit)
	if(unit ~= 'player') then return end

	if(event == 'UNIT_HEALTH') then
		if(not health and (UnitHealth(unit) / UnitHealthMax(unit)) < 30) then
			health = true
			PlaySoundFile([=[Interface\AddOns\|FreeUI\media\LowHealth.mp3]=])
		else
			health = false
		end
	elseif(event == 'UNIT_MANA') then
		if(not power and (UnitPower(unit) / UnitPowerMax(unit)) < 30) then
			power = true
			PlaySoundFile([=[Interface\AddOns\|FreeUI\media\LowMana.mp3]=])
		else
			power = false
		end
	end
end)
I'm bored tbh..

You should also consider registering more events for the power (energy/rage etc).
See if p3lim's code works.

Also, to note, the '|FreeUI' in p3lim's code should actually be '!FreeUI'. (Unless I am mistaken by the functionality of the '[=', '=]' usage... then smack me.)
__________________
Never be satisfied with satisfactory.
  Reply With Quote
08-20-10, 05:52 PM   #4
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
2min drycode spelling mistakes
  Reply With Quote
08-21-10, 04:50 AM   #5
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
That still doesn't fix it I'm afraid. When health or power switches to true (when the sound file is played), then the if-statement will become false (if not health and...), instantly switching to the next step to turn health false again, creating a loop that repeats over and over until the health goes above the set value.

Also, with that maths, it's not '30', but '.3', small detail.

And I'm not going to add rage/energy etc, because it's meant only for mana users.

Still looking for a fix.
  Reply With Quote
08-21-10, 05:17 AM   #6
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Wouldn't it work if you check if health is true and health is above 30% and only set health to false again if those two conditions are met. Potentially, you could get spammed if you hover around the limit. But I suppose you could save the time for the last warning and then see if sufficient time has passed.
__________________
Oh, the simulated horror!
  Reply With Quote
08-21-10, 05:59 AM   #7
Soulofsin_007
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 125
Here is some code I wrote awhile back for someone kinda wanting the same thing. Its not perfect, but it works.

Code:
local total = 0

local function onUpdate(self,elapsed)
   local health = UnitHealth("player")
   local h = floor(UnitHealth("player")*100/UnitHealthMax("player"))
   local affectingCombat = UnitAffectingCombat("player");
  
    total = total + elapsed
  if(affectingCombat == 1) then
    if total >=6 then
        if(h <= 20) then
          print("WARNING:CURRENT HEALTH IS LOW! "..h.."% IS YOUR HEALTH!!")
          PlaySound("RaidWarning")
          end
        total = 0
    end
  end
end

local f = CreateFrame("frame")
f:SetScript("OnUpdate", onUpdate)
  Reply With Quote
08-21-10, 06:20 AM   #8
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Uh, why are you using an OnUpdate that only does something after 6 seconds of combat?
  Reply With Quote
08-21-10, 06:46 AM   #9
Soulofsin_007
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 125
Again, I thought I stated earlier it wasn't perfect, and no, if I recall correctly, it will instantly alert you and then every 6 seconds it will say something. I also came up with the code for a 13 line addon, I didn't think that it would matter if it was an OnUpdate or OnEvent. =P
  Reply With Quote
08-21-10, 08:20 AM   #10
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Ah yeah, you're right. Still, I'd like my code to work though, I don't like to throw stuff away and use something else entirely just because it works.
  Reply With Quote
08-22-10, 06:52 AM   #11
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Still looking for a fix for the original problem. Anyone?
  Reply With Quote
08-22-10, 05:16 PM   #12
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Haleth View Post
Still looking for a fix for the original problem. Anyone?
I just tried your original code, using "Sound\\Spells\\ThunderClap.wav" in place of "Interface\\AddOns\\!FreeUI\\media\\LowHealth.mp3", and it worked as expected. Are you sure the mp3 doesn't have any dead space before the actual warning sound?

Here it is rearranged some:
Code:
local frame, playedHP, playedMP = CreateFrame('Frame')
frame:SetScript('OnEvent', function(self, event, unit)
	if unit ~= 'player' then return end
	if event == 'UNIT_HEALTH' then
		if UnitHealth('player') / UnitHealthMax('player') >= 0.3 then
			playedHP = nil
		elseif not playedHP then
			playedHP = PlaySoundFile([[Interface\AddOns\!FreeUI\media\LowHealth.mp3]])
		end
	elseif UnitPower('player') / UnitPowerMax('player') >= 0.3 then
		playedMP = nil
	elseif not playedMP then
		playedMP = PlaySoundFile([[Interface\AddOns\!FreeUI\media\LowMana.mp3]])
	end
end)
frame:RegisterEvent('UNIT_HEALTH')
frame:RegisterEvent('UNIT_MANA')


Originally Posted by Soulofsin_007 View Post
I also came up with the code for a 13 line addon, I didn't think that it would matter if it was an OnUpdate or OnEvent.
Using OnUpdate should always be a last resort, and the number of lines of code involved is no indicator of cpu usage. If you ever find yourself using an OnUpdate script immediately stop and make sure there is not another way to proceed. I also firmly believe that OnUpdate scripts are always an exception to the "don't worry to much about optimization" rule when it comes to addons.
  Reply With Quote
08-23-10, 01:58 AM   #13
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
It's strange, I'm quite sure there's no dead space, especially seeing as the time until the sound file was played varied. Sometimes it was instantly, sometimes there was a second delay.

I'll try the rearranged code, thanks.
  Reply With Quote
08-23-10, 02:22 AM   #14
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Haleth View Post
It's strange, I'm quite sure there's no dead space, especially seeing as the time until the sound file was played varied. Sometimes it was instantly, sometimes there was a second delay.
Perhaps rounding/flooring of the displayed percentage is causing a perceived delay when in fact there isn't one?
  Reply With Quote
08-23-10, 02:56 AM   #15
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Well, I tested the health alert on my tank. Taking all armour off, then putting it all on again put me well below 30% health, yet the time until the sound was played still varied; the only explanation I have for that is that it waits until the next regen tick.
  Reply With Quote
08-23-10, 03:32 AM   #16
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Haleth View Post
Well, I tested the health alert on my tank. Taking all armour off, then putting it all on again put me well below 30% health, yet the time until the sound was played still varied; the only explanation I have for that is that it waits until the next regen tick.
I understand the issue now. I just set the threshold to 0.9 and tested in combat. The problem is that when you remove your gear, your health and max health both change together keeping you at 100%. When you put your gear back on only your max health changes (thus no UNIT_HEALTH triggered) until a regen tick kicks in. Try this:

Code:
local frame, playedHP, playedMP = CreateFrame('Frame')
frame:SetScript('OnEvent', function(self, event, unit)
	if unit ~= 'player' then return end
	if event == 'UNIT_HEALTH' or event == 'UNIT_MAXHEALTH' then
		if UnitHealth('player') / UnitHealthMax('player') >= 0.3 then
			playedHP = nil
		elseif not playedHP then
			playedHP = PlaySoundFile([[Interface\AddOns\!FreeUI\media\LowHealth.mp3]])
		end
	elseif UnitPower('player') / UnitPowerMax('player') >= 0.3 then
		playedMP = nil
	elseif not playedMP then
		playedMP = PlaySoundFile([[Interface\AddOns\!FreeUI\media\LowMana.mp3]])
	end
end)
frame:RegisterEvent('UNIT_HEALTH')
frame:RegisterEvent('UNIT_MANA')
frame:RegisterEvent('UNIT_MAXHEALTH')
frame:RegisterEvent('UNIT_MAXMANA')
  Reply With Quote
08-23-10, 03:36 AM   #17
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
You're a genius, going to try this as soon as I can get back on WoW.

Edit: Works! Thank you very much.

Last edited by Haleth : 08-23-10 at 03:47 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with this script (health/mana alerts)

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