Thread Tools Display Modes
08-03-14, 11:43 PM   #1
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Lua get Buff duration

Hi,

i want to return how much time left on my buff.

i startet with:

Code:
function()
    local value = select(15, UnitBuff("player", "Blood Shield")) or 0
    
end
But now i dont know how to go on.
Can somebody give a hint?
  Reply With Quote
08-04-14, 12:18 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Lua Code:
  1. local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable,
  2. shouldConsolidate, spellId, canApplyAura, isBossDebuff, value1, value2, value3 = UnitBuff("unit", index or "name"[, "rank"[, "filter"]])
You check on the 15th argument. The time left is the expirationTime value.

Take a look at the 2nd example
http://wowpedia.org/API_UnitBuff
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
08-04-14, 04:22 AM   #3
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Ok but i still get lua errors and a strange behaviour of my bar:

Code:
function()
    local value = select(8, UnitBuff("player", "Blutschild")) or 0
    return value;
end
So with this i get the expirationTime and i return it. My goal is
to have a bar which shows when the buff will expire.
With the above code i just get lua errors.

11x Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1864: attempt to compare number with string
44x Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:649: attempt to perform arithmetic on field 'expirationTime' (a nil value)
Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:649: in function <Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:647>
Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:1053: in functio

Last edited by Lybrial : 08-04-14 at 04:24 AM.
  Reply With Quote
08-04-14, 06:35 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Re-read the documentation. The 8th return value is a string identifying the unit that cast the buff; the expiration time is arg7. Also, there's really no reason to use select here; it's just slowing things down for no reason.

Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime
end
Or, if your function needs to return the amount of time left (instead of the time at which it ends) do this:

Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime - GetTime()
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.
  Reply With Quote
08-04-14, 07:00 AM   #5
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Originally Posted by Phanx View Post
Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime
end
4x Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1864: attempt to compare number with nil
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1864: in function `SetEventDynamics'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1835: in function `ActivateEvent'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1816: in function `ScanEvents'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1730: in function `ForceEvents'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:2144: in function `ScanForLoads'
Originally Posted by Phanx View Post
Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime - GetTime()
end
9x [string "return function()..."]:3: attempt to perform arithmetic on local 'expirationTime' (a nil value)
[string "return function()..."]:3: in function `durationFunc'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1860: in function `SetEventDynamics'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1835: in function `ActivateEvent'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1816: in function `ScanEvents'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1730: in function `ForceEvents'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:2144: in function `ScanForLoads'
Maybe i should describe my problem a little bit better.

Im working with WeakAuras 2 And i have a bar to observe my Blood Shield of my DK.

This is the function i use to show how much damage my shield can block:

Code:
function () 
    local shield_value = select(15, UnitBuff("player", "Blutschild")) or 0;
    shield_value = math.floor(shield_value/1000);
    
    local max_health = UnitHealthMax("player");
    max_health = math.floor(max_health/1000);
    
    return string.format("%uk/%uk", shield_value, max_health);
end
My Trigger Code:

Code:
function()
    return true
end
My Reverse Trigger Code:

Code:
function()
    local a = UnitBuff("player", "Blutschild")
    return a == nil
end
And now i need Code for the duration so that my bar
shows how much time is left until my shield expires.
If i use one of your codes posted above i get the
posted lua errors and the bar starts randomly in the
middle of the bar and decreases.
  Reply With Quote
08-04-14, 07:24 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You're (probably) getting those errors because you don't currently have the buff active. I guess WeakAuras can't handle nil values itself (I don't actually use it) so just return a zero if UnitAura didn't return anything:
Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime or 0
end
or:
Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime and (expirationTime - GetTime()) or 0
end
Again, as I don't actually use WeakAuras, I don't know whether it needs the remaining time or the expiration time, but either of the above should make sure it doesn't receive any scary nil values.
__________________
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.
  Reply With Quote
08-04-14, 07:38 AM   #7
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Originally Posted by Phanx View Post
You're (probably) getting those errors because you don't currently have the buff active. I guess WeakAuras can't handle nil values itself (I don't actually use it) so just return a zero if UnitAura didn't return anything:
Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime or 0
end
or:
Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime and (expirationTime - GetTime()) or 0
end
Again, as I don't actually use WeakAuras, I don't know whether it needs the remaining time or the expiration time, but either of the above should make sure it doesn't receive any scary nil values.
It is still complaining about the fact that expirationTime is nil while the Blood Shield Buff isnt up.
This time ill post the whole error output:

132x Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:649: attempt to perform arithmetic on field 'expirationTime' (a nil value)
Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:649: in function <Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:647>
Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:1053: in function `SetDurationInfo'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1875: in function `SetEventDynamics'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1835: in function `ActivateEvent'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1816: in function `ScanEvents'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1730: in function `ForceEvents'
Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:2144: in function `ScanForLoads'
...terface\AddOns\WeakAurasOptions\WeakAurasOptions-2.0.7.lua:8128: in function <...terface\AddOns\WeakAurasOptions\WeakAurasOptions.lua:8127>

Locals:
self = <unnamed> {
0 = <userdata>
values = <table> {
}
UpdateCustomText = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:905
Color = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:783
duration = 0
border = <unnamed> {
}
SetDurationInfo = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:1012
stacks = <unnamed> {
}
SetIcon = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:834
SetStacks = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:928
trigger_count = 0
icon = <unnamed> {
}
color_g = 0
Scale = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:942
Expand = <function> defined @Interface\AddOns\WeakAuras\WeakAuras.lua:3710
color_b = 0.058823529411765
text = <unnamed> {
}
GetColor = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:790
color_r = 1
triggers = <table> {
}
iconFrame = <unnamed> {
}
SetName = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:1005
Collapse = <function> defined @Interface\AddOns\WeakAuras\WeakAuras.lua:3698
timer = <unnamed> {
}
color_a = 1
orientation = "HORIZONTAL"
bar = <unnamed> {
}
}
duration = 0
expirationTime = nil
customValue = nil
inverse = nil
data = <table> {
textFlags = "None"
stacksSize = 24
borderBackdrop = "Blizzard Tooltip"
xOffset = 0
parent = "DK Group"
stacksFlags = "None"
customText = "function ()

local max_health = UnitHealthMax("player");
return math.floor(max_health/1000);
end"
yOffset = 60
anchorPoint = "CENTER"
untrigger = <table> {
}
color = <table> {
}
borderColor = <table> {
}
customTextUpdate = "update"
rotateText = "NONE"
icon = true
fontFlags = "OUTLINE"
actions = <table> {
}
displayTextLeft = "%sk"
numTriggers = 1
selfPoint = "CENTER"
trigger = <table> {
}
text = true
barColor = <table> {
}
stickyDuration = false
desaturate = false
backgroundColor = <table> {
}
barInFront = true
alpha = 1
timer = true
timerFlags = "None"
load = <table> {
}
height = 26.999997118166
backdropColor = <table> {
}
id = "Blood Shield"
displayStacks = "%s"
timerColor = <table> {
}
zoom = 0
displayTextRight = "%ck"
border = false
borderEdge = "None"
regionType = "aurabar"
stacks = false
stacksFont = "Friz Quadrata TT"
icon_side = "LEFT"
stacksContainment = "INSIDE"
stacksColor = <table> {
}
borderSize = 16
texture = "Flat"
textFont = "Friz Quadrata TT"
borderOffset = 5
auto = false
timerSize = 18
additional_triggers = <table> {
}
timerFont = "Friz Quadrata TT"
frameStrata = 1
width = 150
animation = <table> {
}
borderInset = 11
inverse = false
textSize = 18
orientation = "HORIZONTAL"
displayIcon = "Interface\Icons\Spell_DeathKnight_Butcher2"
stacksPoint = "BOTTOMRIGHT"
textColor = <table> {
}
}
UpdateValue = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:700
UpdateTimeInverse = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:694
UpdateTime = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:647
bar = <unnamed> {
OnSizeChanged = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:159
GetBackgroundColor = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:244
GetVertexColor = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:258
Update = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:57
GetOrientation = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:207
SetVertexColor = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:255
fg = <unnamed> {
}
SetOrientation = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:196
value = 1
SetMinMaxValues = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:164
bg = <unnamed> {
}
GetForegroundColor = <function> defined @Interface\AddOns\WeakAuras\RegionTypes\aurabar.lua:236
GetMinMaxValues = <function> defined
Even after i active the Blood Shield the errors arent disappearing. It is defenitely related to the duration
i want to show because if i delete it everything just works fine.

Last edited by Lybrial : 08-04-14 at 10:26 AM.
  Reply With Quote
08-04-14, 08:49 AM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Lybrial View Post
It is still complaining about the fact that expirationTime is nil while the Blood Shield Buff isnt up.
Yes, which is why in the post directly before yours I posted revised versions that will return 0 instead of nil when the buff isn't up...
__________________
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.
  Reply With Quote
08-04-14, 10:25 AM   #9
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Originally Posted by Phanx View Post
Yes, which is why in the post directly before yours I posted revised versions that will return 0 instead of nil when the buff isn't up...
No my friend, the last error i posted appeared with this code:

Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime or 0
end
or

Code:
function()
     local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
     return expirationTime and (expirationTime - GetTime()) or 0
end
I just made a wrong quote im sorry for that. I corrected the quote.
  Reply With Quote
08-04-14, 12:21 PM   #10
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
If you're still getting that error, then it's not coming from the code snippets I posted, as it's literally not possible for either of those snippets to return a nil value, and the error you posted is being caused by a nil value.
__________________
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.
  Reply With Quote
08-04-14, 12:26 PM   #11
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
WeakAuras probably wants multiple return values and I have no idea what they're supposed to be (start or expiration time and duration, maybe).

I'm also pretty sure WA can show you how much time is left on a buff without creating a custom function for it.

Judging by other WA scripts I've seen, and the error above, it appears to want "duration, expirationTime" for the return values.

Last edited by semlar : 08-04-14 at 12:40 PM.
  Reply With Quote
08-04-14, 12:47 PM   #12
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Originally Posted by semlar View Post
WeakAuras probably wants multiple return values and I have no idea what they're supposed to be (start or expiration time and duration, maybe).

I'm also pretty sure WA can show you how much time is left on a buff without creating a custom function for it.

Judging by other WA scripts I've seen, and the error above, it appears to want "duration, expirationTime" for the return values.
But i need to use the custom trigger because of the other stuff i want to show.
For that i need custom lua codes.

But as i said, if i keep the code for the duration empty everything works without lua errors
(and ofcourse the duration is not shown). When i put in the code postet above lua complains
about the nil value.
  Reply With Quote
08-04-14, 12:57 PM   #13
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Presumably it should be something like this, but as far as I can tell there isn't any documentation for WeakAuras.
Lua Code:
  1. function()
  2.      local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
  3.      return duration or 0, expirationTime or 0
  4. end
  Reply With Quote
08-04-14, 01:12 PM   #14
Lybrial
A Flamescale Wyrmkin
AddOn Compiler - Click to view compilations
Join Date: Jan 2010
Posts: 120
Originally Posted by semlar View Post
Presumably it should be something like this, but as far as I can tell there isn't any documentation for WeakAuras.
Lua Code:
  1. function()
  2.      local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
  3.      return duration or 0, expirationTime or 0
  4. end
You are my hero this evening. Its exactly that!
The only thing wrong is that if the buff is not up it shows
the bar fully filled instead of empty.

But for now this is great. I dont need more today
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Lua get Buff duration

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