[Help] "If I target myself" code

Hello, I am currently using an addon made by addon.bool.no with the codes i’ve found here;

I’ve added TargetFrame too and it is almost perfect for me but I want PlayerFrame to re-appear whenever i target myself out of combat but neither i don’t know nor I could find the code on web.

it looks like this right now;

 PlayerFrame:SetAlpha(0.05)
 EventRegistry:RegisterCallback("PLAYER_REGEN_DISABLED", function() 
 	PlayerFrame:SetAlpha(1)
 end)
 
 EventRegistry:RegisterCallback("PLAYER_REGEN_ENABLED", function() 
 	PlayerFrame:SetAlpha(0.05)
 end)
 
 PlayerFrame:HookScript("OnEnter", function(self)
 	self:SetAlpha(1)
 end)
 
 PlayerFrame:HookScript("OnEnter", function(self)
 	self:SetAlpha(1)
 end)
 
 PlayerFrame:HookScript("OnLeave", function(self)
 	if not InCombatLockdown() then
 		self:SetAlpha(0.05)
 	end
 end)
 
 TargetFrame:SetAlpha(0.3)
 EventRegistry:RegisterCallback("PLAYER_REGEN_DISABLED", function() 
 	TargetFrame:SetAlpha(1)
 end)
 
 EventRegistry:RegisterCallback("PLAYER_REGEN_ENABLED", function() 
 	TargetFrame:SetAlpha(0).3
 end)
 
 TargetFrame:HookScript("OnEnter", function(self)
 	self:SetAlpha(1)
 end)
 
 TargetFrame:HookScript("OnLeave", function(self)
 	if not InCombatLockdown() then
 		self:SetAlpha(0)
 	end
 end)

Probably i need to change this line;

 PlayerFrame:HookScript("OnLeave", function(self)
 	if not InCombatLockdown() then
 		self:SetAlpha(0.05)

into something like;

 PlayerFrame:HookScript("OnLeave", function(self)
 	if not InCombatLockdown() **or TargetSelf** then
 		self:SetAlpha(0.05)

So this is my problem, I don’t know coding stuff, i just copy pasted it and found the TargetFrame with trial and error method. Tried TargetSelf, Target:TargetPlayer and something more but couldn’t find what refers “if i target myself” code.

It is not a vital thing but it annoys me a little that PlayerFrame is faded when i target myself out of combat. If someone can help, it will be great.

Wrap your code in code tags (</> button in the editor) not quote tags, or the forums will butcher your syntax.

Also
https://wowpedia.fandom.com/wiki/API_UnitIsUnit

Thanks but didn’t work.

After I edit the post it has been deleted by automated system, sorry for late answer and duplicate topics.

I tried a lot of stuff from your link, either I don’t know where to put them or they won’t work. :smiley: probably first one :smiley:

UnitIsUnit("target","player")

EventRegistry:RegisterCallback("PLAYER_TARGET_CHANGED", function() 
if UnitIsUnit("target","player") then
 	PlayerFrame:SetAlpha(1)
	TargetFrame:SetAlpha(1)
end
 end)

I’ve tried exact copy of this code before you wrote but it didn’t work neither.

I am suspecting that any of the target change is written as “player” on my setup by another addon. Downloaded EventTracker addon and checked the logs and whenever PLAYER_CHANGED_TARGET event happens, unitid was always “player” even i target an enemy or npc.

maybe i had make mistakes on those punctuations so i will try yours too. I am logging in now and let you now after i try.

Edit: Tried and it didn’t work.
Edit2: Disabled all AddOns then tried it again, it still doesn’t work;

PlayerFrame fades away to 0.05 by default - works fine
PlayerFrame fades in combat - works fine
PlayerFrame fades away again when out of combat - works fine
PlayerFrame fades in when i hover the cursor over - works fine
PlayerFrame fades away when i move the cursor away - works fine
PlayerFrame doesn’t fade in to 1 value when I target myself- doesn’t work. I don’t know why but PLAYER_CHANGED_TARGET or UNIT_FRAME event registries doesn’t work with UnitIsUnit.

I’ve tried;

TargetFrame:HookScript("OnShow", function(self)
    if UnitIsUnit("targettarget", "player") then
      self:SetAlpha(1)
    else
      self:SetAlpha(0.2)
    end
end)

This code worked for Target frame and now fades in when i target myself but still couldn’t figure it out how i can do it for player frame.

targettarget is the thing your current target is targeting, so you’re just creating a weird feedback loop, just use target.

You’ve got some duplicated logic and bad syntax throughout the code in the OP so here’s a full rewrite. Also added comments explaining what it’s doing. For consistency, I’m using 100% opacity for in-combat and 5% opacity for out of combat, for both the player and target frames.

PlayerFrame:SetAlpha(0.05)
TargetFrame:SetAlpha(0.05)

-- Show PlayerFrame & TargetFrame when entering combat
EventRegistry:RegisterCallback("PLAYER_REGEN_DISABLED", function() 
	PlayerFrame:SetAlpha(1)
	TargetFrame:SetAlpha(1)
end)

-- Hide PlayerFrame & TargetFrame when leaving combat if target is not player
EventRegistry:RegisterCallback("PLAYER_REGEN_ENABLED", function()
	if not UnitIsUnit("target","player") then
		PlayerFrame:SetAlpha(0.05)
		TargetFrame:SetAlpha(0.05)
	end
end)

-- Show/Hide PlayerFrame & TargetFrame on target Change
EventRegistry:RegisterFrameEventAndCallback("PLAYER_TARGET_CHANGED", function()
	-- Check if you're not in combat so you don't mess with the combat logic
	if not InCombatLockdown() then
		 -- Show if target is player
		if UnitIsUnit("target","player") then
			PlayerFrame:SetAlpha(1)
			TargetFrame:SetAlpha(1)
		 -- Else hide
		else
			PlayerFrame:SetAlpha(0.05)
			TargetFrame:SetAlpha(0.05)
		end
	end
end)

-- Show PlayerFrame on mouseover
PlayerFrame:HookScript("OnEnter", function(self)
	self:SetAlpha(1)
end)

-- Hide PlayerFrame on mouseout if target is not player and you're not in combat
PlayerFrame:HookScript("OnLeave", function(self)
	if not InCombatLockdown() and not UnitIsUnit("target","player") then
		self:SetAlpha(0.05)
	end
end)

-- Show TargetFrame on mouseover
TargetFrame:HookScript("OnEnter", function(self)
	self:SetAlpha(1)
end)

-- Hide TargetFrame on mouseout if target is not player and you're not in combat
TargetFrame:HookScript("OnLeave", function(self)
	if not InCombatLockdown() and not UnitIsUnit("target","player")  then
		self:SetAlpha(0.05)
	end
end)

Thanks for the kind reply and explanations of code groups but i already can understand which does which job from what it is written. I just don’t know what exactly codes like eventregistry and hookscript does since i didn’t study coding before. Things like OnEnter or events or PLAYER_REGEN_ENABLED are easy to understand already but hard to implement when you don’t really know how much time you should end code group etc. which is the basics of coding that i am not used to and stuff like you mentioned “targettarget” which tires to system more with more job while u can tell the computer in a better way i guess. I need to consider these if i am onto coding i guess. It quite entertained me to be busy with coding tho.

For the codes you provided; Firstly thanks all of them, i really appreciate it. I also appreciate that you lead me to WoW API documentations, it taught a lot about making simple addons for personal usage.

Since you didn’t add PlayerFrame:SetAlpha(0.05) and TargetFrame:SetAlpha(0.05) at header, it doesn’t set the alphas’ of both to 0.05 at first login, after once they are triggered by combet or cursor hover, they work well tho.

I completely copy-pasted your code before i add those parts above to make them fade at start of the session but the thing is, it still doesn’t set playerframe’s alpha to 0.05 when i target myself out of combat;

SS from imgur: a/G8GDfW2

Weird thing is when i hover my cursor over while i am targeting myself now, it stays at alpha(1) now even after i move the cursor over somewhere else but still doesn’t set the alpha to 1 whenever i target myself.

I guess, this might do the job for me, it is not a big deal for me to move my cursor to make it semi-permanent visible while being targeted by myself but it would be better if i don’t have to.

Edit: I mean, it seems like this PLAYER_TARGET_CHANGED event just doesn’t work. I couldn’t make it work neither when i was trying to write by myself and it seems it doesn’t work on your version too. I don’t know but i sense like Blizzard changed how the system works because when i check the Event Tracker, whenever PLAYER_TARGET_CHANGED event triggers, it always shows UnitId = player so maybe this is the case that why PlayerFrame can’t set its alpha value to 1. I tried with/without Action combat, with/without Auto Self Cast by suspecting Auto Self Cast might cause these but nothing changed.

Thanks again wholeheartedly for all of your guiding, providing the codes and leading me to the direction that i can learn !

Oh I see, the event registering is wrong.

-- Show/Hide PlayerFrame & TargetFrame on target Change
EventRegistry:RegisterFrameEventAndCallback("PLAYER_TARGET_CHANGED", function()
	-- Check if you're not in combat so you don't mess with the combat logic
	if not InCombatLockdown() then
		 -- Show if target is player
		if UnitIsUnit("target","player") then
			PlayerFrame:SetAlpha(1)
			TargetFrame:SetAlpha(1)
		 -- Else hide
		else
			PlayerFrame:SetAlpha(0.05)
			TargetFrame:SetAlpha(0.05)
		end
	end
end)

Yeah, that did it !

So callback just doesn’t enough for target changed event ? Should i understand that ?

Thanks for awesome work !

Dunno, I can’t get the PLAYER_REGEN ones to work with EventRegistry:RegisterCallback either, but they seemed to be working for you so I wasn’t gonna change things.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.