Quantcast WeakAuras: Shared functions withing a dynamic group - WoWInterface
Thread Tools Display Modes
03-04-18, 06:37 AM   #1
FranekW
A Defias Bandit
Join Date: Mar 2018
Posts: 3
WeakAuras: Shared functions withing a dynamic group

Is it possible to use some kind of "global" functions in WeakAuras environment, or global to a group, or to a dynamic group but still local in terms of WoW env? I could still refer to them when creating codes in Custom Triggers or Custom Text

[TL;DR]

I have created and completed a dynamic group in WeakAuras which contains a set of progress bars, which in turn track random buffs when you apply Rogue's Roll the Bones (based on Muzgu in Wowhead). The problem is Roll the Bones produces a number of buffs out of six in total. In terms of WA, each buff works exactly the same. The only difference is the buff itself. I had to copy and paste a lot of functions and change only one line. Because I created codes for Custom Text and Custom Triggers, for each buff, it was very laborious and prone to error. It would be much more effective and optimised, if I could place the core functions somewhere else within WA environment with a single parameter "buff_name".

For instance, for the buff "Broadsides", I created functions for custom text and custom trigger "Dynamic Info". The code for other buffs, such as "Jolly Roger", "Grand Melee", "Grand Melee", "Shark Infested Waters", "True Bearing", "Restless Blades", and "Buried Treasure" is exactly the same. I only change "buff_name". Below is an example of redundant functions and the third one which would handle everything in one place.
Thanks



Custom text:
Lua Code:
  1. -- Function for Custom Text
  2. function()
  3.     local buff_name = "Broadsides"
  4.     if UnitBuff("PLAYER", buff_name) then
  5.         dur, exptime = select(6, UnitBuff("PLAYER", buff_name))
  6.     end
  7.     return string.format("%.1f", max(0, dur + exptime - GetTime())), max(36, dur), true
  8. end

Custom Trigger "Dynamic Info"
Lua Code:
  1. -- Function for Custom Trigger Dynamic Info
  2. function()
  3.     local buff_name = "Broadsides"
  4.     if UnitBuff("PLAYER", buff_name) then
  5.         dur, exptime = select(6, UnitBuff("PLAYER", buff_name))
  6.     end
  7.     return max(0, dur + exptime - GetTime()), max(36, dur), true
  8. end
Example of an optimised function:

Lua Code:
  1. -- global function
  2. function(buff_name, format)
  3.     if UnitBuff("PLAYER", buff_name) then
  4.         dur, exptime = select(6, UnitBuff("PLAYER", buff_name))
  5.     end
  6.     out = {max(0, dur + exptime - GetTime()), max(dur, 36), true}
  7.     if format then
  8.         out[1] = string.format("%.1f", out[1])
  9.     end
  10.     return out
  11. end

Last edited by FranekW : 03-04-18 at 12:41 PM.
  Reply With Quote
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
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!

Last edited by Phanx : 03-04-18 at 12:10 PM.
  Reply With Quote
03-04-18, 12:09 PM   #3
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 726
You're missing one underscore,Phanx. 6th argument is duration
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
03-04-18, 12:12 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Rilgamon View Post
You're missing one underscore,Phanx. 6th argument is duration
Yeah, already caught that in the edit.
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote
03-04-18, 12:39 PM   #5
FranekW
A Defias Bandit
Join Date: Mar 2018
Posts: 3
Thanks for you answer Phanx.

I have never done any addon before and have not a clue how to start -.- I know there is a TOC file which contains some info about. Maybe it's the time to start one ...

I did not see any error. I though testing UnitBuff() would be enough. Thanks for the comment and suggestions on leaking variables as well.
  Reply With Quote
03-04-18, 05:05 PM   #6
Kkthnx
A Cobalt Mageweaver
 
Kkthnx's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2011
Posts: 243
Originally Posted by FranekW View Post
Thanks for you answer Phanx.

I have never done any addon before and have not a clue how to start -.- I know there is a TOC file which contains some info about. Maybe it's the time to start one ..
https://addon.bool.no/ very very useful.
__________________
Success isn't what you've done compared to others. Success is what you've done compared to what you were made to do.
  Reply With Quote
03-04-18, 07:57 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Kkthnx View Post
https://addon.bool.no/ very very useful.
That was where the "create an addon" link in my post was pointing.

@FranekW: It's an online tool that creates an addon for you. All you have to do is type a name and paste in the code you want to be in it.
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote
03-05-18, 03:18 AM   #8
FranekW
A Defias Bandit
Join Date: Mar 2018
Posts: 3
Thanks for the link. My addon will be super basic I also found WoWWiki with plenty of links and suggestions.

Just one question. If I want to check if buff exists, is it all right if I just check UnitBuff() itself, e.g.:

Lua Code:
  1. -- code
  2. if UnitBuff("PLAYER", buff) then
  3.     -- code to calculate times
  4. end

or should I always test variables returned from the function:

Lua Code:
  1. local _, _, _, _, _, dur, exptime = UnitBuff("PLAYER", buff)
  2. if dur and exptime then
  3.     -- code to calculate times
  4. end

Thanks
  Reply With Quote
03-06-18, 12:15 AM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
If you just want to show that you have the buff, then yes, the first way will work.

If you want to show how much time is left on the buff, or any other information about it other than "it exists", then the only way to do that is by capturing the values returned by UnitBuff.
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » WeakAuras: Shared functions withing a dynamic group

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