Quantcast
How to check if Unit target is player - WoWInterface
Thread Tools Display Modes
10-20-18, 09:52 AM   #1
neheri
A Murloc Raider
Join Date: Oct 2018
Posts: 5
How to check if Unit target is player

Hello,

I would like to edit nameplate addon to change colour when unit targets a player. For example there are 4 units attacking a player and 2 of them not. 4 units should have one colour and other different one.

if UnitTarget == Player then
-- player targeted colour stuff
else
-- player not targetted

How to achieve that?

I was trying with if UnitIsPlayer(UnitName("targettarget")) then

however it works only with unit you target. If you switch a target to some other unit, it works as if player was not targetted, while he is.
 
10-20-18, 10:49 AM   #2
Tim
A Frostmaul Preserver
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 285
I really haven't delved into nameplate stuff and really have no intentions of doing so. However, you're trying to use an incorrect identifier for the UnitIsPlayer function. That function only accepts identifiers of the unit (ie: player, target, pet, etc).

Code:
if UnitIsPlayer("target") then
   -- color some stuff
else
   -- color some stuff
end
https://wow.gamepedia.com/API_UnitIsPlayer
https://wow.gamepedia.com/UnitId
__________________
AddOns: Tim @ WoWInterface
Characters: Hunter, Mage, Priest, Monk, DH,
Battle Tag: Tim#11172
Current PC Setup: PCPartPicker List
 
10-20-18, 12:56 PM   #3
neheri
A Murloc Raider
Join Date: Oct 2018
Posts: 5
Thank you for your reply, Tim.

However I think that you got my idea wrong.

I want to create mobs nameplates, so if they target me they have different colour then those not targetting me.

So, if mob's target = me, it should do something.

With code you written, it should do something if my current target is player.

The quote I quoted works the way I want, however only partially, for a single mob. If there are more than 1 it's broken.

I think that there will be some stuff with unitID gathering into an array, and checking every single ID if their's target is player.

No idea how to do that though.
 
10-20-18, 01:05 PM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,059
To identify if a nameplate's unit is targeting you, you can use the UnitIsUnit function like UnitIsUnit("Nameplate1-Target", "player"), which will check whether the target of "Nameplate1" is the player.

Just use the unitID of the nameplate that you're checking against.
 
10-20-18, 01:34 PM   #5
neheri
A Murloc Raider
Join Date: Oct 2018
Posts: 5
I understand.

However how do I define mob1, mob2 into Nameplate1-Target1, Nameplate1-Target2?

I need some array and check it in a loop am I right?

Do you have any tutorial showing how to handle arrays in addons?

Also is there any way to execute function of an addon every 0.1s for an instance?

Many thanks for your help
 
10-20-18, 02:05 PM   #6
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,059
In your nameplate addon you will have a unitID associated with every nameplate that you're drawing.

When a nameplate is shown, the NAME_PLATE_UNIT_ADDED event will fire, and provide the unitID as its first argument. This can be passed to C_NamePlate.GetNamePlateForUnit(unitID) to get the frame associated with the nameplate unit. That frame is what your addon is attaching to in order to draw its own nameplate above the unit's head.

The NAME_PLATE_CREATED, NAME_PLATE_UNIT_ADDED, and NAME_PLATE_UNIT_REMOVED events are used to track when to create a new nameplate frame, when to show and hide your addon's frames, and to keep track of which unitIDs belong to which nameplates.

When coloring your nameplates you generally have a series of conditions that need to be checked in a certain order, so "unit is targeting me" which you want to represent with one color might be a higher priority than "unit is a hostile npc" which has a different color, and therefor needs to take precedence over it.

In my personal addon I just do this check OnUpdate because I don't particularly care about efficiency, but how you do this is up to you.

Last edited by semlar : 10-20-18 at 02:08 PM.
 
10-20-18, 04:42 PM   #7
Lyak
A Cyclonian
Join Date: Jul 2018
Posts: 46
Sorry for hijacking your thread, neheri, but I was also been curious about how nameplates work.

Originally Posted by semlar View Post
The NAME_PLATE_CREATED, NAME_PLATE_UNIT_ADDED, and NAME_PLATE_UNIT_REMOVED events are used to track when to create a new nameplate frame, when to show and hide your addon's frames, and to keep track of which unitIDs belong to which nameplates.
So, if I am understanding correctly, NAME_PLATE_CREATED gets fired when the new nameplate frame needs to be made if there is more of them needed than what currently exists (which I am guessing that they are recycled) while NAME_PLATE_UNIT_ADDED and NAME_PLATE_UNIT_REMOVED are fired when the unit is being allocated to/ release from the nameplate frame.

That being said in case if there are five nameplate frames already exist and the game has to display only four units to assign to the nameplate frame the only event being fried would be NAME_PLATE_UNIT_ADDED.

Am I getting it correctly?
 
10-21-18, 02:14 AM   #8
neheri
A Murloc Raider
Join Date: Oct 2018
Posts: 5
Thanks everybody for your replies. I will try work it out today according to your tips.

I will be honest - I am trying to edit Aloft addon for TBC (2.4.3) game version, so it works like TidyPlates. It's a tank addon which helps you to see which NPC targets you or not. TidyPlates backport for TBC would be ideal, however it's much more complicated addon than aloft so I am afraid I won't be able to backport it with my current LUA knowledge.

Yeah I know, you think I am a private server pirate - but I have Battle For Azeroth purchased and had 60 days of gameplay while I managed to play only 2hrs and got bored. There are several upcoming TBC servers and I am just find it 100x more fun to play there than on retail.

Back to the point - please see attached Aloft addon with my changes. In file CoreModules\HealthBar.lua, line 260 I made my edits (if I managed to do it here I would also do elsewhere). It works the way I wanted - on health change it changes colour if mob is targeting me, if someone else attacks it it changes colour to yellow.

However it gets bugged with more than 1 mob and also if someone heals me and overaggroes (NPC health is not chaged).

This addon would make a tank's like so much easier. Not to mention TidyPlates which is all the way great addon (including PvP arena plays). Any idea where to request an addon backport?

Cheers
Attached Files
File Type: zip Aloft.zip (647.8 KB, 33 views)
 
10-21-18, 03:10 AM   #9
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Originally Posted by neheri View Post
I will be honest - I am trying to edit Aloft addon for TBC (2.4.3) game version
And now you are doomed.
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill
 
10-21-18, 03:42 AM   #10
neheri
A Murloc Raider
Join Date: Oct 2018
Posts: 5


You happy now? I paid blizzard a lot of money for a game that is not worth it. I think it justifies playing previous version of their game elsewhere.

If there were retail TBC servers I would be super happy to pay and play there.

Cheers
 
10-21-18, 04:58 AM   #11
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
I could not care less one way or another. I am not Blizzard's guardian. I was just making a prediction, with no personal interest other then being right about what will happen.
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill
 
10-21-18, 08:43 AM   #12
Tim
A Frostmaul Preserver
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 285
What he meant by you're doomed is now that you've mentioned that you're not developing for retail you will more than likely see no more help. This forum abides by Blizzards rules thus private server development is not supported/allowed.
__________________
AddOns: Tim @ WoWInterface
Characters: Hunter, Mage, Priest, Monk, DH,
Battle Tag: Tim#11172
Current PC Setup: PCPartPicker List
 

WoWInterface » Developer Discussions » Lua/XML Help » How to check if Unit target is player

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