This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Salaveneus's profile picture

[Lua][Question] Automatic Shutdown?

Started by Salaveneus, 16 October 2012 - 03:29 AM
Salaveneus #1
Posted 16 October 2012 - 05:29 AM
Is there a way you could go about shutting down the Computer after say, 10 minutes of in-activity?

I know you could add a shell.run() to your startup that shuts it down after X amount of minutes no matter what.
I just would like to know if there is a way to do that, but whenever a button is pressed or something, the timer restarts?


So I got this done:

os.pullEvent = function(filter)
  local timer = os.startTimer(5)
  local evt, p1, p2, p3, p4, p5 = os.pullEventRaw()
  if evt == "terminate" then
		error("Terminate")
  elseif evt == "timer" and p1 == timer then
		os.shutdown()
  end
  timer = os.startTimer(5)
  return evt, p1, p2, p3, p4, p5
end
Now how do I run that paralell? Sorry, I do not know much about paralell API=/
Edited on 17 October 2012 - 12:47 AM
Sammich Lord #2
Posted 16 October 2012 - 06:01 AM
Well just use a timer in the OS API and just cancel the timer if a key is pressed. This is simple code but I highly doubt anybody on here will write it for you. You will also want to use the parallel API to run it the same time as shell.
Salaveneus #3
Posted 17 October 2012 - 01:52 AM
Alright thanks a ton =)
Sammich Lord #4
Posted 17 October 2012 - 04:01 AM
Turn your code into a function then do:

local function hello()
  shell.run("rom/programs/shell")
end
parallel.waitForAny(hello. yourFunctionNameHere)
That should work.