View Single Post
03-04-18, 12:03 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I can't actually answer your question as I don't use WeakAuras, but you'll want to be careful about leaking your variables into the global environment:

Code:
-- Function for Custom Text
function()
    local buff_name = "Broadsides"
    if UnitBuff("PLAYER", buff_name) then
        dur, exptime = select(6, UnitBuff("PLAYER", buff_name))
    end
    return string.format("%.1f", max(0, dur + exptime - GetTime())), max(36, dur), true
end
You're leaking both "dur" and "exptime" as globals. Also, your function as written is raising an error any time you don't have the buff active; if you don't see the error, either you don't have anything to show error messages, or WeakAuras is wrapping your function in a pcall so it's getting hidden. I'd rewrite it like so:

lua Code:
  1. function()
  2.     local name = "Broadsides"
  3.     local _, _, _, _, _, dur, exptime = UnitBuff("player", name)
  4.     -- ^ avoid "select" if you can, function calls are expensive
  5.     if not dur or not exptime then
  6.         -- buff is not active, nothing to show
  7.         return 0, 0, true
  8.     end
  9.     -- buff is active, do your thing
  10.     return string.format("%.1f", max(0, dur + exptime - GetTime())), max(36, dur), true
  11. end

edit: Actually, on the original topic, you may not be able to do what you want inside WeakAuras, but there's nothing stopping you from doing it outside of WeakAuras. Just create an addon whose name comes before "WeakAuras" in alphabetical order, and put your function in there as a global, with a nice unique name appropriate for a global:

lua Code:
  1. function GetMyWABuff(name)
  2.     local _, _, _, _, _, dur, exptime = UnitBuff("player", name)
  3.     if not dur or not exptime then
  4.         -- buff is not active, nothing to show
  5.         return 0, 0, true
  6.     end
  7.     -- buff is active, do your thing
  8.     return string.format("%.1f", max(0, dur + exptime - GetTime())), max(36, dur), true
  9. end

...and then call it in your WeakAuras function:

lua Code:
  1. function()
  2.     return GetMyWABuff("Broadsides")
  3. end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 03-04-18 at 12:10 PM.
  Reply With Quote