Want to add a timer which counts how many time take to complete a level

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
kiyo
Prole
Posts: 19
Joined: Thu Dec 08, 2022 12:49 pm

Want to add a timer which counts how many time take to complete a level

Post by kiyo »

I want to add a clock which shows how many minutes or seconds past to complete a level. How can I achieve this?
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Want to add a timer which counts how many time take to complete a level

Post by GVovkiv »

Best way that i can think of:

Code: Select all

local time = 0

love.update = function(dt)
	time = time + dt -- counted in miliseconds. 1 = 1 real seconds
end
Then you can translate this time to minutes, hours, etc:

Code: Select all

print(time / 60) --floor it or ceil?
To get amount of minutes

Code: Select all

print((time / 60) / 60)
Amount of hours
And so on
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Want to add a timer which counts how many time take to complete a level

Post by Bigfoot71 »

If there is no need to continuously display the timer you can also just retrieve the time at departure and arrival and subtract the departure time from the arrival time.

You can then round the result if you want to display it as an integer or if you want to display it as a floating number you can use string.format. Here is a more complete example:

Code: Select all

local t_start = os.clock()

for i = 1, 10000 do end -- "simulate" the game

local t_end = os.clock()

local total = t_end - t_start

print(string.format("%.6f Sec",total))  -- `.6f` means 6 digits after the decimal
print(math.floor(total+.5))     -- Or we round the number
You can then convert also to minutes, hours etc as GVovkiv said ;)

Edit: to round the minutes "closer to reality" you can round as I showed in my example.

Code: Select all


local seconds = 59 -- We want it to be worth 1 minutes
local minutes = math.floor(seconds/60+.01666666666666672) -- because 59/60=0.98333... S/60+(1-59/60)

-- Note: But it can be redundant to search for the right value
-- for and it will only work for a value less than 60
-- so you can also use the modulo operator with a condition

minutes = math.floor(seconds / 60)
if seconds % 60 == 59 then
  minutes = minutes + 1
end

Sorry, I don't have the right term in English, but otherwise, to be completely accurate, you should just use math.floor if 59 seconds isn't a minute for you. You know everything like that ^^

As a bonus, a function to correctly convert seconds into hours, minutes, seconds. I don't want to confuse you with my rounding stories, so everything is clear for you!

Code: Select all

function getTimeFromSec(seconds)
    local hours = math.floor(seconds/3600)
    seconds = seconds-(hours*3600)
    local minutes = math.floor(seconds/60)
    seconds = seconds-(minutes*60)
    return string.format("%d:%02d:%02d", hours, minutes, seconds)    -- Displays as follows: H:M:S    -- %02d is used to display "00" if empty or "01" for 1.
end
My avatar code for the curious :D V1, V2, V3.
User avatar
kiyo
Prole
Posts: 19
Joined: Thu Dec 08, 2022 12:49 pm

Re: Want to add a timer which counts how many time take to complete a level

Post by kiyo »

Thank you all.... Here is my solution...

Code: Select all


function love.load()
    -- Initialize everything
    timer = 0
    maxTimer = 1
    hour = 0
    minute = 0
    seconds = 0
end

function love.update(dt)
    increaseTime(dt)                                      -- call the function increaseTime
end

function love.draw()

end

function increaseTime(dt)
    timer = timer + dt                                     -- increase timer every frame

    if timer >= maxTimer then                              -- if 1 second past reset timer and increase seconds
        seconds = seconds + 1
        timer = 0
    end

    if seconds >= 60 then                                  -- if 1 minute past reset seconds and increase minute
        seconds = 0
        minute = minute + 1
    end
    
    if minute >= 60 then                                   -- if 1 hour past reset minute and increase hour
        minute = 0
        hour = hour + 1
    end
    -- print(timer)
    print(hour,minute,seconds)                             -- print(hour,minute,seconds)
end


User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Want to add a timer which counts how many time take to complete a level

Post by Bigfoot71 »

Your code may produce slight inaccuracies over time, I'd rather do something like this:

Code: Select all

function increaseTime(dt)

    timer = timer + dt

    if timer >= maxTimer then
        seconds = seconds + 1
        timer = timer - maxTimer
    end

    if seconds >= 60 then
        seconds = seconds - 60
        minute = minute + 1
    end
    
    if minute >= 60 then
        minute = minute - 60
        hour = hour + 1
    end

    print(hour,minute,seconds)

end
But if you're not up to the milliseconds your code already makes coffee ^^
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Want to add a timer which counts how many time take to complete a level

Post by darkfrei »

Set time

Code: Select all

function love.mousepressed(x, y, button, istouch)
  T = love.timer.getTime()
end
Get delta time:

Code: Select all

function love.mousereleased(x, y, button)
   DT = love.timer.getTime() - T
end
Show time and delta time:

Code: Select all

function love.draw()
  if T then
    love.graphics.print(T, 0, 0)
  end
  if DT then
    love.graphics.print(DT, 0, 20)
  end
end
Also:
https://stackoverflow.com/a/45376848

Code: Select all

function disp_time(time)
  local days = math.floor(time/86400)
  local hours = math.floor(math.mod(time, 86400)/3600)
  local minutes = math.floor(math.mod(time,3600)/60)
  local seconds = math.floor(math.mod(time,60))
  return string.format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
end
(not tested)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
kiyo
Prole
Posts: 19
Joined: Thu Dec 08, 2022 12:49 pm

Re: Want to add a timer which counts how many time take to complete a level

Post by kiyo »

Bigfoot71 wrote: Mon Jan 23, 2023 3:48 pm Your code may produce slight inaccuracies over time, I'd rather do something like this:

Code: Select all

function increaseTime(dt)

    timer = timer + dt

    if timer >= maxTimer then
        seconds = seconds + 1
        timer = timer - maxTimer
    end

    if seconds >= 60 then
        seconds = seconds - 60
        minute = minute + 1
    end
    
    if minute >= 60 then
        minute = minute - 60
        hour = hour + 1
    end

    print(hour,minute,seconds)

end
But if you're not up to the milliseconds your code already makes coffee ^^


Thank you, this works like a charm
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 14 guests