Jump to content




looking for a clock(for in game time) that can activate redstone at night and turn it off in the day

help

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

#1 rick3333

  • Members
  • 30 posts

Posted 13 January 2013 - 09:06 PM

Hi
I am looking for a clock(for in game time) that can activate redstone at night and turn it off in the day.
because I can not figure out the coding for it.

edit: i figured out the code

View Postcrazyguymgd, on 13 January 2013 - 09:19 PM, said:

os.time() returns the current in-game time. I believe morning is 6 and night is 18. so...
time = os.time()
if time < 6 or time > 18 then
  -- redstone output that says it's night
end
would tell you if it's night. then you just run that when you want to know if it's night...
then i got the code to repeat it self with this code.



while true do
time = os.time()
if time < 6 or time > 18 then
rs.setOutput("back",true)-- redstone output that says it's night
end
sleep(0.1)
end

thanks for your help crazyguymgd

#2 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 13 January 2013 - 09:19 PM

os.time() returns the current in-game time. I believe morning is 6 and night is 18. so...
time = os.time()
if time < 6 or time > 18 then
  -- redstone output that says it's night
end
would tell you if it's night. then you just run that when you want to know if it's night...

#3 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

Posted 13 January 2013 - 09:20 PM

Use os.time() to get the time and check if it's greater than 19 or something (19 = 7:00PM).

EDIT: Argh, ninja'd!

Edited by RunasSudo, 13 January 2013 - 09:20 PM.


#4 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 13 January 2013 - 09:21 PM

View PostRunasSudo, on 13 January 2013 - 09:20 PM, said:

Use os.time() to get the time and check if it's greater than 19 or something (19 = 7:00PM).

EDIT: Argh, ninja'd!

:)

#5 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 13 January 2013 - 09:29 PM

For this purpose I like to use "os.startTimer()" and "os.pullEvent('timer')".

#6 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 13 January 2013 - 09:33 PM

View PostOrwell, on 13 January 2013 - 09:29 PM, said:

For this purpose I like to use "os.startTimer()" and "os.pullEvent('timer')".

why do you like to do this?

#7 RunasSudo-AWOLindefinitely

  • Signature Abuser
  • 249 posts
  • Location/dev/earth1aus5

Posted 13 January 2013 - 09:42 PM

Presumably because it's slightly more accurate. I don't see how that would tell you time time of day though. Are you sure you're posting in the right thread?

#8 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 13 January 2013 - 10:00 PM

Total confusion, I meant os.setAlarm() instead of os.startTimer()..

I couldn't find the other thread where I posted this so I'll remake it quickly:
local dawnTime = 6
local sunsetTime = 18
local dawn = os.setAlarm(dawnTime)
local sunset = os.setAlarm(sunsetTime)

local function atDawn()
  -- Do stuff at dawn.
end

local function atSunset()
  -- Do stuff at sunset
end

while true do
  local e,alarm = os.pullEvent('alarm')
  if alarm == dawn then
	dawn = os.setAlarm(dawnTime)
	atDawn()
  elseif timer == sunset then
	sunset = os.setAlarm(sunsetTime)
	atSunset()
  end
end

It's slightly better than spamming os.time() in a while loop, regardless of how long you'd make your sleep there. This loop essentially does a sleep() with the exact amount of timer until dawn/sunset.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 January 2013 - 10:03 PM

View PostOrwell, on 13 January 2013 - 10:00 PM, said:

I couldn't find the other thread where I posted this so I'll remake it quickly:
local dawnTime = 6
local sunsetTime = 18
local dawn = os.setAlarm(dawnTime)
local sunset = os.startAlarm(sunsetTime)

local function atDawn()
  -- Do stuff at dawn.
end

local function atSunset()
  -- Do stuff at sunset
end

while true do
  local e,timer = os.pullEvent('timer')
  if timer == dawn then
	dawn = os.startAlarm(dawnTime)
	atDawn()
  elseif timer == sunset then
	sunset = os.startAlarm(sunsetTime)
	atSunset()
  end
end

It's slightly better than spamming os.timer() in a while loop, regardless of how long you'd make your sleep there. This loop essentially does a sleep() with the exact amount of timer until dawn/sunset.
Would that not just have a timer that ran for 6 and 18 seconds from startup? what if the computer got unloaded?

#10 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 13 January 2013 - 10:07 PM

View PostTheOriginalBIT, on 13 January 2013 - 10:03 PM, said:

View PostOrwell, on 13 January 2013 - 10:00 PM, said:

* snip *
Would that not just have a timer that ran for 6 and 18 seconds from startup? what if the computer got unloaded?
Mind that I edited my post. I confused the timer methods with the alarm methods. :P Except that I used os.startTimer() instead of os.setTimer() and 'timer' instead of 'alarm' in the version you quoted, that should work. <_< setAlarm() queues an event at specified minecraft time.

Edit: (damn, early in the morning for me, I've hit the edit button like 20 times by now)

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 January 2013 - 10:10 PM

View PostOrwell, on 13 January 2013 - 10:07 PM, said:

View PostTheOriginalBIT, on 13 January 2013 - 10:03 PM, said:

View PostOrwell, on 13 January 2013 - 10:00 PM, said:

* snip *
Would that not just have a timer that ran for 6 and 18 seconds from startup? what if the computer got unloaded?
Mind that I edited my post. I confused the timer methods with the alarm methods. :P Except that I used os.startTimer() instead of os.setTimer() and 'timer' instead of 'alarm' in the version you quoted, that should work. <_< setAlarm() queues an event at specified minecraft time.

Edit: (damn, early in the morning for me, I've hit the edit button like 20 times by now)
Haha thats ok, I was just making sure I wasn't misunderstanding the code...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users