• Making the seconds on a timer show properly?
    6 replies, posted
Hey everyone. I am trying to make a countdown timer on my HUD for 5 minutes. It works perfectly. But when the seconds part of the 5 min timer: _________ that part VV 4:23 _________ comes underneath 10 seconds it shows this: 4:5 instead of: 4:05 How can I make it show that? Here is my code: [lua] timer.Create("clRoundTimer", 300, 1, function() end) function getTimer() timer.Start("clRoundTimer") end net.Receive("roundTimerStart", getTimer) function paintTimerHUD() local timeLeft = timer.TimeLeft("clRoundTimer") local timeText = "" if timer.TimeLeft("clRoundTimer") == nil then timeText = "0:0" else timeText = tostring(math.floor(timeLeft/60))..":"..tostring(math.floor(timeLeft%60)) end draw.SimpleTextOutlined(timeText, "HUDNumber5", ScrW()/2, ScrH()/2, Color(255,255,255,255), 0, 0, 1.5, Color(0,0,0,255)) end hook.Add("HUDPaint", "paintTimerHUD", paintTimerHUD) [/lua] Any help is appreciated :D - Fillipuster
Use a formatted string: string.format [url]http://www.cplusplus.com/reference/cstdio/printf/[/url]
Thanks, but anyone willing to do a quick explanation of how to do this exactly. I learn much better from that.
You can also use string.ToMinutesSeconds( time_in_seconds ) to have it do it for you. The idea behind it is fairly simple: [code] minutes = math.floor( time / 60 ) seconds = time % 60 output = string.format( "%02d:%02d", minutes, seconds )[/code] We use %02d to tell the object that it is a number ( d ), should always be at least 2 in length ( 2 ) and to pad missing columns with 0 ( 0 ) Which means it handles things like: 00:01 32:10 04:19 All automatically.
[QUOTE=Kogitsune;45601160]You can also use string.ToMinutesSeconds( time_in_seconds ) to have it do it for you. The idea behind it is fairly simple: [code] minutes = math.floor( time / 60 ) seconds = time % 60 output = string.format( "%02d:%02d", minutes, seconds )[/code] We use %02d to tell the object that it is a number ( d ), should always be at least 2 in length ( 2 ) and to pad missing columns with 0 ( 0 ) Which means it handles things like: 00:01 32:10 04:19 All automatically.[/QUOTE] Awesome! This is perfect. There is just one problem.. Now it posts time like this: 4:60 instead 5:00 Any fix?
[code]time = math.Round( time )[/code] Before setting up minutes or seconds. What's happening is that %d is rounding your number - seconds is fractional at this point and likely is something like 4.568 - if we round it beforehand, that doesn't happen.
[QUOTE=Kogitsune;45601583][code]time = math.Round( time )[/code] Before setting up minutes or seconds. What's happening is that %d is rounding your number - seconds is fractional at this point and likely is something like 4.568 - if we round it beforehand, that doesn't happen.[/QUOTE] Thanks, your awesome! :D
Sorry, you need to Log In to post a reply to this thread.