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

os.setAlarm() ?

Started by Zudo, 31 October 2012 - 11:30 AM
Zudo #1
Posted 31 October 2012 - 12:30 PM
Can someone help me with os.setAlarm() ?
I dont know how to run a command (eg print("TEST")) to run at the specified time.
jag #2
Posted 31 October 2012 - 01:27 PM
If you want to just print something after a delay:

sleep(seconds)
-- Everyone knows this one, but just to be clear

If you want to do something on a specific time:

-- Between 18.00 to 20.00
while true do
  if os.clock() > 18 and
  os.clock() < 20 then
	do something
  end
  sleep(.5)
end

If you want to have a delay and don't want to pause the entire program:

local curTime = os.time()
local delay = 5 -- minutes I think
while true do
  -- Your fancy program stuff

  if curTime == os.time() + delay then
	do something
  end

  -- More fancy stuff
end

Now if you already got a event system then you can take use of that:

-- Press enter to start clock
-- Press space to exit
local ev,p1,p2,p3
while true do
  ev,p1,p2,p3 = os.pullEvent()
  if ev == "key" then
	if p1 == 28 then
	  os.startTimer(5) -- 5 seconds I think
	  print("Timer set!")
	elseif p1 == 57 then
	  print("Canceling!")
	  break
	end
  elseif ev == "timer" then
	print("Timer has reached it's timeout!")
  end
  ev,p1,p2,p3 = nil
end
Harcole #3
Posted 31 October 2012 - 01:50 PM
The setAlarm() returns an ID value, I believe you use it with pullEvent(<alarmID>).

Removed bad code example! as below posts confirms this line… :P/>/>
Edited on 31 October 2012 - 01:22 PM
MysticT #4
Posted 31 October 2012 - 01:58 PM
If you want to use the alarm (wich in this case is better than the above options), you can do it like this:

local alarm = os.startAlarm(5)
while true do
  local evt, arg = os.pullEvent("alarm")
  if arg == alarm then
    print("It's 5:00, wake up!")
  end
end
jag #5
Posted 31 October 2012 - 02:00 PM
And if you just want clear help with os.setAlarm() then the wiki is a great place.
Otherwise, this is how it basically works:

--(( Basic format ))--
os.setAlarm(time)

--(( Will timeout when it's 18 o'clock in the world ))--
os.setAlarm(18.00)

--(( Will timeout 2 (in-game) hours later ))--
os.setAlarm(os.clock()+2)

--(( Basic event layout ))--
local ev,p1 = os.pullEvent("alarm")

--(( Event output ))--
Name: alarm
Parameter 1: A table that acts like it's unique ID

The difference between os.setAlarm() and os.startTimer() is just that os.setAlarm() fires on a specific time but os.startTimer() uses the relative time.

EDIT: Oh snap! I think I've been ninja'd
jag #6
Posted 31 October 2012 - 02:03 PM
If you want to use the alarm (wich in this case is better than the above options), you can do it like this:

local alarm = os.startAlarm(5)
while true do
  local evt, arg = os.pullEvent("alarm")
  if arg == alarm then
	print("It's 5:00, wake up!")
  end
end
Just a quick note:
It's os.setAlarm() not os.startAlarm(). Just a small typo
MysticT #7
Posted 31 October 2012 - 04:27 PM
Just a quick note:
It's os.setAlarm() not os.startAlarm(). Just a small typo
Oh, yeah didn't see that one :P/>/>


--(( Will timeout 2 (in-game) hours later ))--
os.setAlarm(os.clock()+2)
You may want to use os.time instead of os.clock. os.time returns the world time, os.clock returns the time (in seconds) since the computer started.