ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: Cod0fDuty on Dec 16, 2021, 11:56 AM

Title: How to pause a timer, and then resume it again?
Post by: Cod0fDuty on Dec 16, 2021, 11:56 AM
Hello,
I'm currently making a redstone timer program, but I can't seem to figure out a way to pause a timer by a redstone input, and then resuming it (not resetting the timer) after another redstone signal is detected.

I'm still pretty new to Lua and CC, and any help or suggestions would be greatly appreciated :)! Thanks in advance.
Title: How to pause a timer, and then resume it again?
Post by: Lupus590 on Dec 17, 2021, 08:33 AM
The best way to do a timer in CC is to use the os.startTimer (https://tweaked.cc/module/os.html#v:startTimer) function. This will return an ID that you will want to keep for two reasons, it's how you will recognise the timer when it goes off (https://tweaked.cc/event/timer.html), and it will allow you to cancel the timer (https://tweaked.cc/module/os.html#v:cancelTimer) (which you will want to do).

You will also want to make a note of the time (https://tweaked.cc/module/os.html#v:clock) that you queued the timer and compare that you when you cancel/pause the timer. When you receive (https://tweaked.cc/event/redstone.html) the second redstone signal (https://tweaked.cc/module/redstone.html#v:getInput) you will want to queue a new timer with a shorter duration to account for the time that it has already run.