I have created a countdown timer that decreases by 1 every second but I wish to pause the timer when the script receives a message telling the timer to stop. I am unsure as to how I can stop it but the timer itself does work. This is what I have so far:
In my code, where would I put this code.
Also, is there a way to stop the timer without cancelling it?
In my code, where would I put this code.
In the case of your code, self.a is the handle. So wherever you want to cancel the code you would do timer.cancel(self.a)
Also, is there a way to stop the timer without cancelling it?
No. But if you save timer information from its callback function you can use it to resume a timer by creating a new timer with its leftover time.
local time_total = 1
local time_left = 0
local my_timer = timer.delay(time_total, false, function(self, handle, time_elapsed)
time_left = time_total - time_elapsed
end)
timer.delay(0.5, false, function(self, handle, time_elapsed)
timer.cancel(my_timer)
local new_timer = timer.delay(time_left, false, function(self, handle, time_elapsed)
print("works!")
end)
end)
Something like that but you would need to test and adapt to your use case.
Why not make use of your self.stop?
If you only need a quick pause and resume you can ignore the timer, let it do it’s job.
function down(...)
if self.stop then return end
self.time = self.time -1
...
end
set self.stop to false if you want to resume.
I need it to stop until the player presses the resume button (in my case the key “p”).
Also note that the timer uses the dt from the current colleciton proxy.
So, if you’ve paused the current collection proxy using set_time_step == 0, then the timer won’t advance.
Thank you everyone