Thread Tools Display Modes
02-29-12, 06:52 AM   #1
Kendian
A Molten Giant
 
Kendian's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 614
UnitAffectingCombat

Is there any way to show/hide a panel based on whether target is in combat? I've looked around, but am not sure UnitAffectingCombat is the function I'm looking for. My thanks, for any/all help. in advance~
__________________
  Reply With Quote
02-29-12, 07:06 AM   #2
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
lua Code:
  1. local addon = CreateFrame("Frame")
  2.  
  3. local function checkStatus()
  4.   local f = _G["NameOfFrameToTrack"]
  5.   if InCombatLockdown() then
  6.     f:Hide() --hide in combat  
  7.   else
  8.     f:Show() --show out of combat
  9.   end
  10. end
  11.  
  12. --out of combat
  13. function addon:PLAYER_REGEN_ENABLED(...)
  14.   checkStatus()
  15. end
  16.  
  17. --in combat
  18. function addon:PLAYER_REGEN_DISABLED(...)
  19.   checkStatus()
  20. end
  21.  
  22. --call func based on event
  23. addon:SetScript("OnEvent", function(self, event, ...)
  24.   self[event](...)
  25. end)
  26.  
  27. --register events
  28. addon:RegisterEvent("PLAYER_REGEN_ENABLED")
  29. addon:RegisterEvent("PLAYER_REGEN_DISABLED")
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 02-29-12 at 12:58 PM.
  Reply With Quote
02-29-12, 07:30 AM   #3
Kendian
A Molten Giant
 
Kendian's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 614
Simply brilliant, Zork. My thanks~
__________________
  Reply With Quote
02-29-12, 05:29 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
InCombatLockdown() does not return true immediately when PLAYER_REGEN_DISABLED fires; this is intentional, so that addons have time to do stuff with secure frames when the player enters or exits combat, so your code may not always work as desired. UnitAffectingCombat("player") would be a more reliable check.

There's also no reason to do a _G lookup when you already have the frame's global name. Just run NameOfFrameToTrack:Show() instead of _G["NameOfFrameToTrack"]:Show(), and save the extra lookup.

You may also want to register PLAYER_ENTERING_WORLD.

I'd change/simplify the whole thing to:

Code:
local addon = CreateFrame("Frame")

--register events
addon:RegisterEvent("PLAYER_ENTERING_WORLD")
addon:RegisterEvent("PLAYER_REGEN_DISABLED")
addon:RegisterEvent("PLAYER_REGEN_ENABLED")

-- call func based on event
addon:SetScript("OnEvent", function(self, event, ...)
	if UnitAffectingCombat("player") then
		-- in combat combat
		NameOfFrameToTrack:Hide()
	else
		-- not in combat combat
		NameOfFrameToTrack:Show()
	end
end)
If you're using this in kgPanels, change "addon" to "self", put the RegisterEvent lines in the OnLoad section for the panel, and everything inside the OnEvent script function in the OnEvent section for the panel.

Last edited by Phanx : 02-29-12 at 06:21 PM.
  Reply With Quote
03-01-12, 11:31 AM   #5
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
InCombatLockdown() does not return true immediately when PLAYER_REGEN_DISABLED fires; this is intentional, so that addons have time to do stuff with secure frames when the player enters or exits combat, so your code may not always work as desired. UnitAffectingCombat("player") would be a more reliable check.
Good to know that.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
03-01-12, 11:55 AM   #6
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Is it at all necessary to use UnitAffectingCombat()? I just do this, normally

Code:
local addon = CreateFrame("Frame")

--register events
addon:RegisterEvent("PLAYER_REGEN_DISABLED")
addon:RegisterEvent("PLAYER_REGEN_ENABLED")

-- call func based on event
addon:SetScript("OnEvent", function(self, event, ...)
	if event == "PLAYER_REGEN_DISABLED" then
		-- in combat
		NameOfFrameToTrack:Hide()
	else
		-- not in combat
		NameOfFrameToTrack:Show()
	end
end)

Last edited by Haleth : 03-01-12 at 12:25 PM.
  Reply With Quote
03-01-12, 12:11 PM   #7
oomp
A Murloc Raider
 
oomp's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 7
Originally Posted by Haleth View Post
Is it at all necessary to use UnitAffectingCombat()? I just do this, normally

Code:
local addon = CreateFrame("Frame")

--register events
addon:RegisterEvent("PLAYER_ENTERING_WORLD")
addon:RegisterEvent("PLAYER_REGEN_DISABLED")
addon:RegisterEvent("PLAYER_REGEN_ENABLED")

-- call func based on event
addon:SetScript("OnEvent", function(self, event, ...)
	if event == "PLAYER_REGEN_DISABLED" then
		-- in combat
		NameOfFrameToTrack:Hide()
	else
		-- not in combat
		NameOfFrameToTrack:Show()
	end
end)
I assume you simply forgot to omit the addon:RegisterEvent("PLAYER_ENTERING_WORLD") line? Otherwise, the frame will be shown when PLAYER_ENTERING_WORLD fires, as well as PLAYER_REGEN_ENABLED.
  Reply With Quote
03-01-12, 12:25 PM   #8
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
That line isn't needed in this example yes, unless you do other things with your frame and require it to show on PLAYER_ENTERING_WORLD anyway. My question is if UnitAffectingCombat() is needed when the event fired already tells you whether you're in or out of combat.
  Reply With Quote
03-01-12, 02:52 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
If you are only registering for PLAYER_REGEN_DISNABLED and PLAYER_REGEN_ENABLED, then no, you do not need to check UnitAffectingCombat.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » UnitAffectingCombat

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