WoWInterface (
-   AddOn Help/Support (
-   -   Adding %HP to default nameplates; getting an error occasionally (

nKweo 03-25-18 03:47 PM

Adding %HP to default nameplates; getting an error occasionally
I've been looking into ways to add a simple %HP text to the default enemy nameplates. So far the best thing I've found is Gello's old addon Nameplate Percents.

It actually still works fine most of the times, but it sometimes throws an error (and doesn't display) in PvE instances. I'm still trying to nail down the exact situation; so far it seems that it only happens in boss fights (but not every time).

The error isn't very clear, but it does say something about taint. I suspect the problem is related to this function:

-- look for new nameplates that don't have a percent overlay and add one
function frame:ScanNameplates(...)
  for i=1,select("#",...) do
    local plate = select(i,...)
                local name = plate:GetName()
                if name and name:match("^NamePlate") then
                        -- the statusBar is the first child of the first child of the nameplate
                        local statusBar = plate:GetChildren():GetChildren()
                        if not statusBar.percentOverlay then
                                statusBar.percentOverlay = statusBar:CreateFontString(nil,"OVERLAY")
                                local percent = statusBar.percentOverlay
                                overlays[statusBar.percentOverlay] = 1 -- add new child to next update batch

As I said, it's pretty old code and Blizzard obviously has made big changes to the way nameplates are handled (e.g. C_NamePlate.GetNamePlates). Moreover, they protected some stuff in 7.2(?).

I've been messing around with it a little bit, but my knowledge is insufficient. I'm hoping for some ideas to get this working perfectly. I'm guessing that this shouldn't be too difficult for some of you code wizards. Any ideas are appreciated though!

Ammako 03-25-18 05:37 PM

You would only get those errors if friendly nameplates are enabled, but then again you skipped providing the actual error message, so there's no way to say for certain whether that is the issue or not.

briskman3000 03-25-18 10:48 PM

Blizzard restricted all friendly nameplate customizations inside PvE instances. You can do literally nothing to them anymore in dungeons, Raids, etc.

nKweo 03-26-18 02:53 AM

Thanks for the reply guys. The thing is that I'm not using friendly nameplates. Do you think this could nevertheless still lead to an error? I'll try to copy-past the error next time I get it. Like I said, it's been pretty elusive so far (only happening in PvE instances every now and then, so not the best time to troubleshoot).

If the error is caused by the code somehow trying to alter friendly nameplates, how could I make it so that it only looks for enemy plates?

Ammako 03-26-18 06:42 AM

If friendly nameplates aren't used, then there is no way for them to trigger taint.
Plates are only protected when assigned to friendly units, and only in PvE instances. Everything else is free game.
If they aren't enabled then they can't cause errors and break things.

The only thing I could think of would be neutral units that get yellow nameplates while "enemy nameplates" are enabled, but who would be somehow considered friendly by nameplate code... but I don't think that actually exists, and I've never had a problem in nearly a year of insecurely altering nameplates, as long as I kept friendly nameplates disabled (and I've been in a lot of raids/dungeon instances, but admittedly I could have missed some.)

(For the record, as far as I'm aware, Personal Resource Display is not considered a friendly nameplate, so it can't be that.)

But then again, error logs are king. I'd imagine it's not even friendly nameplate-related (or at least I'd be surprised if it were.)

thomasjohnshannon 03-29-18 07:16 PM

You can use "framenamehere:IsForbidden()" to check if the frame is protected. I use this method in my nameplate addon and I haven't had any problems with it.

nKweo 03-30-18 01:05 PM

Brilliant, thanks! Seems to work smooth now. I 'borrowed' heavily from your code. Happy to hear if there are flaws here.

-- Add health percent text to (unprotected) nameplates (credit: nPlates by Grimsbain)
hooksecurefunc("CompactUnitFrame_UpdateStatusText", function(frame)
        if frame:IsForbidden() or ( UnitIsFriend("player",frame.displayedUnit) and not UnitIsUnit(frame.displayedUnit,"player") ) then return end
        if not frame.healthBar.percent then
                frame.healthBar.percent = frame.healthBar:CreateFontString(nil,"OVERLAY")
                local percentcalc = ceil((UnitHealth(frame.displayedUnit) / UnitHealthMax(frame.displayedUnit)) * 100)
--                frame.healthBar.percent:Show()

EDIT: I'm trying to hide the display from friendly plates at all times (except the personal resource display). Doesn't seem to work like this. Suggestions appreciated!

Ammako 03-30-18 03:02 PM

What happens if you try frame.unit rather than frame.displayedUnit?

semlar 03-30-18 05:48 PM

The issue is that you're scanning for nameplates by looping through the children of WorldFrame, then you're trying to call a function on the frame (plate:GetName()), and if that frame happens to be forbidden it will throw an error.

You could add a check like if not plate:IsForbidden(), and put the rest of your function inside of that, or you can use the actual nameplate events that they added a couple years ago with the launch of Legion (NAME_PLATE_CREATED, NAME_PLATE_UNIT_ADDED, NAME_PLATE_UNIT_REMOVED).

The protected friendly nameplates in instances are implemented in a way that addons don't need to do anything to account for their existence; they don't trigger the same nameplate events and are completely separate from the standard nameplate frames, so unless you're doing something like actively scanning through every child of WorldFrame, you won't accidentally encounter them.

nKweo 03-31-18 01:59 PM

@Ammako: didn't seem to make a difference.
@Semlar: thanks for clarifying. I'm trying a completely different approach now (see previous post), which works better. The problem now though is that the percent display gets drawn on friendly plates even though I'm trying to prevent it (just for consistency).

What's worse is that the percentage overlay gets drawn onto the raid frames as well now... Initially friendly plates stay clean, but as soon as a percentage overlay is created for an enemy plate, overlays get added everywhere... Is there an easy way to circumvent this with this method?

Ammako 03-31-18 03:57 PM

OH wait no, I'm dumb.

See, nameplates get reused, once you draw %HP onto a plate from an enemy it will continue showing once the plate gets reused for another unit, friendly or not.

When unit is friend you want to specifically :Hide() frame.healthBar.percent

Unless this has changed lately, but last I was messing with this stuff, this has been the case.

The thing is, though, doing it via this method taints the friendly nameplates regardless, hiding the text doesn't untaint them (you can't untaint something once it's tainted.)
I wouldn't know of a way to do it without tainting friendly nameplates and tbh I never cared. Default plates are useless within PvE instances so there is very little reason to even have them enabled.

btw you still haven't shown us what the error you were getting from the first code actually was. If it's really just a friendly-nameplates-in-instances thing maybe you can just keep using that code but with extra checks like semlar said. That may be the better way to do it. Or maybe it's not even related to friendly nameplates, because we don't know.

nKweo 03-31-18 05:18 PM

That makes a lot of sense. At least I've managed to keep them from the raid frames. Using this function did the trick (thanks again Grimsbain!):

local IsNameplate = function(unit)
        if type(unit) ~= "string" then return false end
        if ( string.match(unit,"nameplate") ~= "nameplate" and string.match(unit,"NamePlate") ~= "NamePlate" ) then
                return false
                return true

Taint has never actually affected any functionality for me while playing, so I guess it's more of a 'puritan' thing. Pretty happy with this at the moment.

Ammako 03-31-18 06:02 PM

Nameplate taint doesn't really affect anything beyond throwing errors if a nameplate used by a friendly unit has been tainted, even if you aren't currently touching them. Anything you touch becomes tainted, but this is only a problem if said code is expected to run through secure Blizzard functions. Code tainted = the code can no longer be verified as secure = errors if secure code ever processes it.

Maybe I messed up when trying to avoid protected nameplates taint, though; if semlar says they're completely separate maybe I should mess with it again some more.

Edit: Sounds like my implementation of the check was bogus. I had no idea frame:IsForbidden() was a thing, so I was using a bunch of different conditionals together that were meant to prevent modifications from being done to friendly nameplates while you were in an instance, but I guess that wasn't really failproof and some of it got through anyway.

Using frame:IsForbidden() and not even changing any of the rest of my code appears to prevent taint errors when within instances and friendly nameplates are enabled. Or at least, disabling the checks triggers an error whenever a friendly nameplate comes on screen, and when I re-enable them those errors no longer occur.

Well that's pretty cool, that was barely any work, and basically, ignore most of the other things I said earlier about nameplate taint, because it's wrong.

nKweo 04-01-18 01:10 AM

That's super cool. Guess we all learned something then:). Funny how newbie questions such as mine can sometimes even (indirectly) help seasoned coders. And thanks again for your time and thoughts.

Ammako 04-01-18 07:30 AM

Psh I'm not a seasoned coder. I just do whatever gives me the results I want. To sum it up: I don't code, I hack.
I hang around and sometimes people show up with better and usually easier/more straightforward ways to do things, and that's how I learn to do better I guess.

I blame the lack of proper API documentation from Blizzard... fansites often just never get updated with new API documentation (For ex: nameplates are still undocumented on wowpedia, and the 7.2.0 API changes don't even mention forbidden nameplates.) So you have to just mess with it and feel your way around until you figure out how things work, or find the right people to ask for info, who happen to have done the research already.
Though I hear something is in the works with 8.0, as far as official documentation from Blizzard goes...

nKweo 04-01-18 03:02 PM

Correction: more seasoned coders :D. I made a small correction to the aforementioned function by the way. I tried to be a smart ass by fiddling with the original...

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

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