Thread Tools Display Modes
Unread 09-28-10, 07:04 AM   #1
Lane
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 36
Lua help UnitDebuff

I'm trying to calculate the time remaining for a specific debuff. I'm not getting any errors but I think my lack of lua Math and Rounding is causing a nonsensical return.

Given:
Code:
local function getSpellTime(spell)
	local name, _, _, _, _, dur, exp, caster = UnitDebuff("target", spell)
	if name ~= nil and caster == "player" and name == spell then
		return exp - GetTime();
	end
end
My return value is usually something like 2... or 4...

Any idea what I need to get whole second times?
Lane is offline  
Unread 09-28-10, 07:34 AM   #2
Lane
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 36
So I realized part of my problem was having a narrow frame to display the value. I solved that part so it is now working except for the zillion decimal places showing up. How do I just drop the decimal values?
Lane is offline  
Unread 09-28-10, 08:54 AM   #3
yj589794
A Rage Talon Dragon Guard
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 314
If you want to round up the value you can use math.ceil, or to round down the value you can use math.floor (http://lua-users.org/wiki/MathLibraryTutorial).

If you want to print a formatted value, but retain the actual value in the parameter, you can use string.format (http://www.lua.org/pil/20.html).
yj589794 is offline  
Unread 09-28-10, 08:58 AM   #4
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 317
Or something like:

Code:
FontString:SetFormattedText("%.0f", floor(timeleft))
http://wowprogramming.com/docs/widge...tFormattedText
__________________
Oh, the simulated horror!
Ailae is offline  
Unread 09-28-10, 09:13 AM   #5
Lane
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 36
Originally Posted by Ailae View Post
Or something like:

Code:
FontString:SetFormattedText("%.0f", floor(timeleft))
http://wowprogramming.com/docs/widge...tFormattedText
I don't know C at all so I started to go this direction but couldn't figure out what any of those options mean.

What does %.0f mean in english?
Lane is offline  
Unread 09-28-10, 09:15 AM   #6
Lane
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 36
Also, to clarify I don't want to round at all I just want to drop the decimal place.

From: 20.34546825638
To: 20

From: 20.67483
To: 20
Lane is offline  
Unread 09-28-10, 09:24 AM   #7
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 317
Originally Posted by Lane View Post
What does %.0f mean in english?
%.0f means that it will format the number you're inputting to a float with 0 decimals. So if you hand it 24.1241 you'd get 24 out.

You can try it ingame with
Code:
/dump string.format("%.0f", 24.1241)
which would yield..

Code:
Dump: value=string.format("%.0f", 24.1241)
[1]=24
If you'd want one decimal, you would use 1 instead of 0.
__________________
Oh, the simulated horror!

Last edited by Ailae : 09-28-10 at 09:26 AM. Reason: Helps if I actually type the right stuff.
Ailae is offline  
Unread 09-28-10, 11:14 AM   #8
Lane
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 36
Cool I'll try that tonight. While I was doing research on this I found some printf resources that said "%d" would give me the same results. Would that work in Lua too?
Lane is offline  
Unread 09-28-10, 11:22 AM   #9
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 317
It would, but then it doesn't round the value.

With %.0f 21.7 becomes 22.
With %d 21.7 becomes 21.
__________________
Oh, the simulated horror!
Ailae is offline  
Unread 09-28-10, 11:55 AM   #10
Lane
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 36
Place holder for when I get home:

Code:
local function getSpellTime(spell)
	local name, _, _, _, _, dur, exp, caster = UnitDebuff("target", spell)
	if name ~= nil and caster == "player" and name == spell then
	    --return math.ceil(exp - GetTime());
	    return string.format("%d", exp - GetTime());
	end
end
Lane is offline  
Unread 10-10-10, 03:35 AM   #11
abija
A Kobold Labourer
AddOn Author - Click to view addons
Join Date: Apr 2005
Posts: 1
You can try
Code:
local function getSpellTime(spell)
	local name, _, _, _, _, dur, exp = UnitDebuff("target", spell, nil, "PLAYER")
	if name then
	    return string.format("%d", exp - GetTime())
	end
end
abija is offline  
Unread 10-10-10, 08:26 AM   #12
Aznamir
A Fallenroot Satyr
 
Aznamir's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 20
In my mod I use
Code:
function PallyPower:FormatTime(time)
        if not time or time < 0 or time == 9999 then
                return ""
        end
        local mins = floor(time / 60)
        local secs = time - (mins * 60)
        return sformat("%d:%02d", mins, secs)
end
Aznamir is offline  
 

Go BackWoWInterface » AddOns, Compilations, Macros » Cataclysm Beta » Lua help UnitDebuff

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