WoWInterface (
-   Lua/XML Help (
-   -   How to check if Unit target is player (

neheri 10-20-18 09:52 AM

How to check if Unit target is player

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
-- 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.

Tim 10-20-18 10:49 AM

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).


if UnitIsPlayer("target") then
  -- color some stuff
  -- color some stuff

neheri 10-20-18 12:56 PM

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.

semlar 10-20-18 01:05 PM

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.

neheri 10-20-18 01:34 PM

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

semlar 10-20-18 02:05 PM

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.

Lyak 10-20-18 04:42 PM

Sorry for hijacking your thread, neheri, but I was also been curious about how nameplates work.


Originally Posted by semlar (Post 330529)
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?

neheri 10-21-18 02:14 AM

1 Attachment(s)
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?


Banknorris 10-21-18 03:10 AM


Originally Posted by neheri (Post 330531)
I will be honest - I am trying to edit Aloft addon for TBC (2.4.3) game version

And now you are doomed.

neheri 10-21-18 03:42 AM

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.


Banknorris 10-21-18 04:58 AM

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.

Tim 10-21-18 08:43 AM

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.

All times are GMT -6. The time now is 11:47 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI