Log In  

Im trying to make an inverse or reverse for this function to countdown. How do I make a countdown timer?

function _update()
print(time())
if time() >= 10 then
time=0
end
end

P#108642 2022-03-15 05:37 ( Edited 2022-03-15 05:39)

-- at the start there is no countdown
countdown = nil

-- call this function to start the countdown
function start_countdown()
 countdown = 10
end

function _update()
 if countdown ~= nil then
  countdown = countdown - 1
  if countdown == 0 then
   countdown = nil -- stop the countdown
   -- do something here
  end
 end
end
P#108645 2022-03-15 08:23 ( Edited 2022-03-15 08:23)

dredds solution is good, but you might want your countdown to go in seconds, where as theirs counts down 1 per frame (30 per second). This version counts down in seconds. Not sure why you'd want to revert to nil, so I just made it count to zero then stop there.

countdown=0

function start_countdown()
  countdown = 10
end

function _update()
  countdown=max(0,countdown-1/30)
  print(ceil(countdown))
end
P#108669 2022-03-15 15:27 ( Edited 2022-03-15 15:36)

[Please log in to post a comment]