Jump to content




os.setAlarm() ?

api java utility

6 replies to this topic

#1 Zudo

  • Members
  • 800 posts
  • LocationUK

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.

#2 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

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


#3 Harcole

  • New Members
  • 24 posts
  • LocationUK

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 by Harcole, 31 October 2012 - 02:22 PM.


#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

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


#5 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

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

#6 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 31 October 2012 - 02:03 PM

View PostMysticT, on 31 October 2012 - 01:58 PM, said:

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

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 31 October 2012 - 04:27 PM

View Postjag_e_nummer_ett, on 31 October 2012 - 02:03 PM, said:

Just a quick note:
It's os.setAlarm() not os.startAlarm(). Just a small typo
Oh, yeah didn't see that one :P/>

View Postjag_e_nummer_ett, on 31 October 2012 - 02:00 PM, said:

--(( 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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users