WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Lua get Buff duration (https://www.wowinterface.com/forums/showthread.php?t=49563)

Lybrial 08-03-14 11:43 PM

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?

Rilgamon 08-04-14 12:18 AM

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

Lybrial 08-04-14 04:22 AM

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.

Quote:

11x Interface\AddOns\WeakAuras\WeakAuras-2.0.7.lua:1864: attempt to compare number with string
Quote:

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

Phanx 08-04-14 06:35 AM

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


Lybrial 08-04-14 07:00 AM

Quote:

Originally Posted by Phanx (Post 294687)
Code:

function()
    local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
    return expirationTime
end


Quote:

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'
Quote:

Originally Posted by Phanx (Post 294687)
Code:

function()
    local _, _, _, _, _, duration, expirationTime = UnitBuff("player", "Blutschild")
    return expirationTime - GetTime()
end


Quote:

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.

Phanx 08-04-14 07:24 AM

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.

Lybrial 08-04-14 07:38 AM

Quote:

Originally Posted by Phanx (Post 294690)
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:

Quote:

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.

Phanx 08-04-14 08:49 AM

Quote:

Originally Posted by Lybrial (Post 294691)
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...

Lybrial 08-04-14 10:25 AM

Quote:

Originally Posted by Phanx (Post 294695)
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.

Phanx 08-04-14 12:21 PM

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.

semlar 08-04-14 12:26 PM

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.

Lybrial 08-04-14 12:47 PM

Quote:

Originally Posted by semlar (Post 294714)
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.

semlar 08-04-14 12:57 PM

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

Lybrial 08-04-14 01:12 PM

Quote:

Originally Posted by semlar (Post 294717)
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 :D


All times are GMT -6. The time now is 02:28 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI