Jump to content




os.startTimer much shorter than expected


  • You cannot reply to this topic
3 replies to this topic

#1 martok111

  • Members
  • 4 posts

Posted 01 April 2017 - 04:28 PM

I'm trying to create a 1.5 hour delay in my program using os.startTimer(5400). My understanding is that this runs a timer for 5400 seconds (90 minutes). Instead, the timer event triggers after less than a minute.

rednet.open("top")
completed = 1
while true do
  if completed ~= 0 then
	redstone.setOutput("bottom", false)
	timer = os.startTimer(3600)
	print("Started ", timer, " ", os.time())
	completed = 0
  end
  local event = os.pullEvent()
  print(event)
  print(timer)
  print(os.time())
  if (event == "redstone" and redstone.getInput("right") == true) or event == "timer" then
	redstone.setOutput("bottom", true)
	os.sleep(60)
	redstone.setOutput("bottom", false)
	os.sleep(30)
	rednet.broadcast("farm", "farm")
	print("Planting")
	os.cancelTimer(timer)
	completed = 1
  end
end

Through debug outputs, I've determined that it is the timer event causes it, and the timer id is the one expected. I've also added os.time as an output and the time difference is about 0.6. oddly this was the same whether the delay was 5400 or 3600.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 April 2017 - 11:43 PM

Your code is not checking that the timers which complete have the ID of the original. Try it like this:

local timerLength = 3600

rednet.open("top")
local timer = os.startTimer(timerLength)
redstone.setOutput("bottom", false)

while true do
	local event, par1 = os.pullEvent()
	
	if (event == "redstone" and rs.getInput("right")) or (event == "timer" and par1 == timer) then
		redstone.setOutput("bottom", true)
		sleep(60)
		redstone.setOutput("bottom", false)
		sleep(30)
		
		rednet.broadcast("farm", "farm")
		print("Planting")
		
		os.cancelTimer(timer)
		timer = os.startTimer(timerLength)
	end
end


#3 martok111

  • Members
  • 4 posts

Posted 02 April 2017 - 04:06 PM

Oh! I understand now. Took me until now to realize the difference. Thank you!!

Seems to be working perfectly now!

I wonder where the other timer event was coming from.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 April 2017 - 11:42 PM

Rednet generates them every now and then. Because the repeat script exists, it's possible for computers to receive multiple copies of the same message; hence as each message comes in, systems note their UUIDs and start a timer. If they see more copies of the message with the same UUID before that timer completes, they know to ignore them.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users