1. #1

    Question [Weak Auras] Question about Custom Text

    Ok, so first off, let me apologize in advance if this question has been asked and answered. I did use the search and I also tried googling it, but no help.

    Second, let me also apologize for my total lack of LUA and coding knowledge.


    Ok, so I'm looking to set up a custom Weakauras Text Display. I want to use the "%c" function so I can input my own LUA code, but I want the text to show up as my name, and in my class color. (I also want to copy this to make it do the same thing for my target) I realize that "%n" with the trigger set to player will already output my name, and I have also figured out that there's a way to tell it to use class color, but I can't get it to do both at the same time.

    I have tried the below, and it works to do the class color, but it just returns a flat value of "0.78" no matter what else I add in.

    Code:
    function()
        local c = RAID_CLASS_COLORS[select(2,UnitClass("player"))]
        return c.r, c.g, c.b
    end

    Also, I realize that the above code can be applied in the custom animations section. I have tried setting the display text to "%n" and then setting the animations to the above code, but when I have no target, it causes a mass flood of errors.

  2. #2
    -Set your display to %n
    -Go to Animation, set the Main-Animation to Custom, give it a Duration of 1s or whatever (it needs a duration though)
    -Check the Box next to Color and choose Custom Function in the dropdown menu, expand the text editor and replace whatever you
    have in there with your function

    That works for me at least

    edit: it's giving me some errors when I try to change its position with the mouse tho, it works with changing the values X and Y Offset
    Last edited by oneyoner; 2015-01-15 at 03:00 AM.

  3. #3
    Yeah, I've done everything I can to make the animations angle work. The problem is that I'm already using a slide-in start animation (and slide-out finish animation) and adding the color to those animations PLUS having the main animation be a custom color isn't working. Causes errors and also, there's a small gap of time between the start-main-finish animations where the text turns back to the default color. This is why I'm looking for custom code to just pull the name of the unit and display it in class color.

  4. #4
    was trying to make a display function that gets the class and your name, basically
    Code:
    function()
    local unit_name = UnitName("player")
    local unit_class = select(2, UnitClass("player"))
    
         ...
    then maybe make if/elseif lines with every class and their color codes and in the end return the name in the color from the loop
    but I can't seem to figure out how to change the font color of a string that the function is supposed to return.
    Not experienced enough I guess, if it even works

    sorry
    bed now

    edit: I think I got it actually, use this in your custom display function:
    Code:
    function()
        local unit_name = UnitName("player")
        local color = ""
        
        if (select(2, UnitClass("player")) == "HUNTER") then
            color = "|cFFABD473"
        elseif (select(2, UnitClass("player")) == "DEATHKNIGHT") then
            color = "|cFFC41F3B"
        end
        return color..unit_name
    end
    I only did it with Hunter and DK now, the rest should be easy. Just google "wow class colors", first link should be from wowwiki.
    There you can see the HEX codes, you will notice that those have 6 digits while in the function above the color code has 8 digits.
    Just put the FF in front of the wowwiki color codes, that is the alpha value (transparency).
    Example for Mage:
    Code:
    elseif (select(2, UnitClass("player")) == "MAGE") then
    
    color = "|cFF69CCF0"
    okay?
    Last edited by oneyoner; 2015-01-15 at 06:08 AM.

  5. #5
    Wonderful. Thanks so much for the help! Just loaded it in and it works great!

    One last question-- I copied/pasted that over to my target frame and changed all the "player" calls to "target" instead. Here's what it ended up as:

    Code:
    function()
        local unit_name = UnitName("target")
        local color = ""
        
        if (select(2, UnitClass("target")) == "HUNTER") then
            color = "|cFFABD473"
        elseif (select(2, UnitClass("target")) == "DEATHKNIGHT") then
            color = "|cFFC41F3B"
        elseif (select(2, UnitClass("target")) == "MAGE") then
            color = "|cFF69CCF0"
        elseif (select(2, UnitClass("target")) == "DRUID") then
            color = "|cFFFF7D0A"
        elseif (select(2, UnitClass("target")) == "MONK") then
            color = "|cFF00FF96"
        elseif (select(2, UnitClass("target")) == "PALADIN") then
            color = "|cFFF58CBA"
        elseif (select(2, UnitClass("target")) == "PRIEST") then
            color = "|cFFFFFFFF"
        elseif (select(2, UnitClass("target")) == "ROGUE") then
            color = "|cFFFFF569"
        elseif (select(2, UnitClass("target")) == "SHAMAN") then
            color = "|cFF0070DE"
        elseif (select(2, UnitClass("target")) == "WARLOCK") then
            color = "|cFF9482C9"
        elseif (select(2, UnitClass("target")) == "WARRIOR") then
            color = "|cFFC79C6E"
        else color = "|CFFFFFFFF"
        end
        return color..unit_name
    end


    It works fine at first, but then it causes a massive error when I open the weakauras options. The error that pops up is:

    Message: [string "return function()..."]:29: attempt to concatenate local 'unit_name' (a nil value)
    Time: 01/15/15 03:00:46
    Count: 159
    Stack: [C]: ?
    [string "return function()..."]:29: in function <[string "return function()..."]:1>
    Interface\AddOns\WeakAuras\RegionTypes\text.lua:91: in function `UpdateCustomText'
    Interface\AddOns\WeakAuras\WeakAuras.lua:5019: in function <Interface\AddOns\WeakAuras\WeakAuras.lua:5015>

    I think it's trying to find my target's name, but since I don't have a target, it's coming back as an error. Is there a way to safeguard against this? Some way to return "..." or "Name Unknown" if it can't find the name?

  6. #6
    You're on the right track.

    • Target a mob.
    • Open options.
    • <no error>
    • Target nothing
    • Open options
    • <error>

    The problem is caused by unit_name being nil. The next step is to look where that variable is used (the second last one) and see that you are indeed trying to concatenate it with color (if you wren't, it would indicate a syntax error elsewhere rather than a logical error).

    So you are reasonably sure the second last line is okay. Keep reading "up" the file and you'll come to the first line of the function body: local unit_name = UnitName("target"). We know that if you have no target, then that target will have no unit name so that must be what's happening.

    Now lets think about ways we can fix it. You want this aura to display even when you have no target selected. If that's ever going to happen (and it will, because it displays when you open the options) then you should probably handle the case where you have no target because that's letting nils sneak into your code.

    The easiest way to do that would be to assign some value for unit name no mater even if UnitName returns nil. Modify that line to:

    Code:
    local unit_name = UnitName("target") or 'No Target'
    This way it will try to evaluate the call to UnitName and if that fails it'll use the value 'No Target' for target name.

    Assuming your trigger functions are good, this aura won't display when you don't have a target so the only time that text will display is if you open the options menu without having anything targeted.


    ---


    Edit: Also, that if-elsif-elsif-elsif-else mess is pretty gross. You're making a whole lot of calls to select() and UnitClass() when you really only need to do it once and store the value. Also, you're duplicating information that's already part of the game and using a big branching control structure when you really just want a simple table lookup. Let's clean that up a bit:

    Have a look at this file, specifically the line that starts "RAID_CLASS_COLORS"
    http://wowprogramming.com/utils/xmlb.../Constants.lua

    Notice that it has keys for each class type, and those points to tables with r,g,b values along with a pre-computed hex string for the colour called colorStr (that's the part we want for the format string).

    All you need to do is look up what 'key' value for that table maps to the class of your current target.
    Now have a look at what UnitClass() returns:

    http://wowprogramming.com/docs/api/UnitClass

    The second value is the key that you need (classFileName) and there's some example code about how you might use it. We want something slightly different, but it pointed us in the right direction.

    Code:
    local unit = 'target' -- what unit are we looking for? player, target, boss1, etc.
    
    if UnitExists(unit) then
      local name = UnitName(unit)
      local class, key = UnitClass(unit)
      local color = RAID_CLASS_COLORS[key].colorStr
      unit = string.format("|c%s%s", color, name)
    end
    
    return unit
    If there was not a target then unit wouldn't exist and you'll get "target" at the end, otherwise you'll get their class coloured name consistent with the rest of the UI. you could have an else branch to return something else, but I think if the only time this will display without a target is when you're configuring it, then it'll be more helpful to have the exact unit name displayed.

    * Written on a phone, not anywhere near a warcraft client.
    Last edited by evn; 2015-01-15 at 01:35 PM.
    A new UI series for 5.2Steal my old UILearn about WeakAuras • Nobody to raid with right now so no PVE videos.

  7. #7
    yeah I was sure it can be done in a cleaner way :P

  8. #8
    oneyoner, evn..... Thank you both for your help! I wasn't expecting to get so much assistance so quickly!

    One problem I'm having: From time to time, when I click on an NPC it will flood with errors again. I'm thinking this is happening because the target has no identifiable class. Is there a safeguard I can put in or an "else" clause that would tell it to return some sort of set color if no class can be found?

    Just tested it again- It seems to happen when I target a friendly NPC that's transitioning. For instance, at the WoD starter area, when you're following Khadgar around. If I target Khadgar when he's offering me a quest, no issues. His name shows up in mage-blue. But as soon as I initiate the next step of the chain, and Khadgar goes to run off, if I target him, it floods with an error.

    Same thing happens when I target an NPC that's trapped in a prison cage. When the cage is closed and the NPCs are still, no issues. They show up as Warrior-brown. But when I open the cage, if I have them targetted, or if I retarget them, errors.

    the error is:

    Code:
    Message: [string "return function()..."]:6: attempt to index field '?' (a nil value)
    Time: 01/15/15 14:25:19
    Count: 495
    Stack: [C]: ?
    [string "return function()..."]:6: in function <[string "return function()..."]:1>
    Interface\AddOns\WeakAuras\RegionTypes\text.lua:91: in function `UpdateCustomText'
    Interface\AddOns\WeakAuras\WeakAuras.lua:5019: in function <Interface\AddOns\WeakAuras\WeakAuras.lua:5015>
    
    Locals:
    Last edited by Cronah; 2015-01-15 at 07:25 PM. Reason: clarification

  9. #9
    Just posting a reply in case someone else ever tries to do the same thing and ends up here... Doubtful, I know....but stranger things have happened.

    So I went back and did more testing. Like I said in my earlier post, it seems to be caused by NPCs when they are transitioning from one area to another. Note: This doesn't mean when they are simply walking around. I think when an NPC is doing some scripted events, they return no class value. I've noticed that the my tooltip shows "unknown" as the type of mob (as opposed to "beast" or "humanoid") during this time as well.

    To fix this, I put in an elseif clause that checks to see if the unit has a class. If it does have a class, it returns that value. If there is no class available, then it goes to the next step (the next step simply ignores the class altogether and doesn't check for it.) The final custom code was:

    Code:
    function()
        local unit = 'target'
        if UnitExists(unit) and UnitClass(unit) then
            local name = UnitName(unit)
            local class, key = UnitClassBase(unit)
            local color = RAID_CLASS_COLORS[key].colorStr 
            unit = string.format("|c%s%s", color, name)
        elseif UnitExists(unit) then
            local name = UnitName(unit)
            unit = string.format (name)    
        end
        
        return unit
    end
    What that does is if you have a unit targeted, it will show up as the Unit's Name with the class color. If the unit doesn't have a class, it will show up as whatever color you have previously selected in the weakaura option (in my case, White).

  10. #10
    When targeting npcs, this causes errors.



    Any ideas how to fix this?
    Sonie <Method> - Twisting Nether EU
    SonieTV @ Twitter

  11. #11
    I like to write code with one path through the function. If there was a chance this could display without a target selected (e.g. option dialog) then I'd do something like this:

    Text display custom function:
    Code:
    function(...)
        local name = "Target"
        local color = 'FFFFFFFF'
        if UnitExists('target') then
            name = UnitName('target')
            local class, key = UnitClassBase('target')
            if key then
                color = RAID_CLASS_COLORS[key].colorStr
            end
        end
        return string.format("|c%s%s", color, name)
    end
    Trigger on custom event: PLAYER_TARGET_CHANGED
    Code:
    function(event, cause)
        return UnitExists('target')
    end
    Untrigger function:
    Code:
    function(event, cause)
        return not UnitExists('target')
    end
    I didn't manage to duplicate any errors with NPCs around my garrison.
    Last edited by evn; Yesterday at 03:46 AM.
    A new UI series for 5.2Steal my old UILearn about WeakAuras • Nobody to raid with right now so no PVE videos.

  12. #12
    Where would you put this untrigger function code?
    Sonie <Method> - Twisting Nether EU
    SonieTV @ Twitter

  13. #13
    in the custom intriguer function, also, I'd have the text update on trigger rather than every frame.
    A new UI series for 5.2Steal my old UILearn about WeakAuras • Nobody to raid with right now so no PVE videos.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •