17 posts
Posted 10 August 2015 - 06:47 AM
Hi.
Previously i made this clock:
moniter = peripheral.wrap("back")
moniter.setTextScale(3)
while true do
sleep(0.75)
moniter.setCursorPos(1,1)
moniter.clear()
moniter.write(textutils.formatTime(os.time(),true))
end
moniter.restore()
It works fine, but i want to make it better! (Hopefully it will all fit on a 3h 2w screen)
I tried to declear the time
local currenttime = textutils.formatTime(os.time()
and using if statments display message depending on the time, but it doesnt work!
sunrise is 6:00
midday is 12:00
night is 18:00
and midnight is 0:00
What I'm trying to do is: when its night (between 0:00 and 6:00) it will
display 'Night Time',
when its day (between 6:00 and 18:00) it would
display 'Day' and
when its midday/midnight (12:00 or 0:00) it will
display midday or midnight. with the clock in the top row all the time. Any ideas?
3057 posts
Location
United States of America
Posted 10 August 2015 - 04:21 PM
A couple things on your previous code,
1. Localize your variables. For example,
local monitor = peripheral.wrap( "back" )
2. Indent your code. Everything inside the while loop should have two spaces in front of it (or a tab, depending on the editor).
3. monitor.restore() isn't a function and would error if it was ever called. Ofc, you never end your while loop so… it's not needed. You can remove it entirely.
As for checking if it's night/day/etc, I'd use the time before formatting it - because you cannot use less-than or greater-than operations on strings, only numbers. Figure out what decimal you get from os.time() when it's 0:00, 6:00, and 12:00. Then you can use that knowledge to build an if/then statement.
1583 posts
Location
Germany
Posted 10 August 2015 - 04:45 PM
A couple things on your previous code,
1. Localize your variables. For example,
local monitor = peripheral.wrap( "back" )
2. Indent your code. Everything inside the while loop should have two spaces in front of it (or a tab, depending on the editor).
3. monitor.restore() isn't a function and would error if it was ever called. Ofc, you never end your while loop so… it's not needed. You can remove it entirely.
As for checking if it's night/day/etc, I'd use the time before formatting it - because you cannot use less-than or greater-than operations on strings, only numbers. Figure out what decimal you get from os.time() when it's 0:00, 6:00, and 12:00. Then you can use that knowledge to build an if/then statement.
You can use >/< on strings.
"stuff 1" < "stuff 2" would be true, and "stuff 1" > "stuff 2" would be false
957 posts
Location
Web Development
Posted 10 August 2015 - 05:06 PM
you cannot use less-than or greater-than operations on strings, only numbers
No, like H4X0RZ pointed out, you can.
Lua does so lexicographical.
Likewise, you can use the '+' and '-' operator on strings as well (if they only have numbers in them)
"2" + "2" --#> 4 (number)
"hi" + "hi" --#> Error (Attempt to preform arithmetic __add on string and string)
Edited on 10 August 2015 - 03:07 PM
3057 posts
Location
United States of America
Posted 10 August 2015 - 05:57 PM
You can, but only if the strings contain numbers only. textutils.formatTime returns "0:00", which would defiantly error if you tried to compare it.
3057 posts
Location
United States of America
Posted 10 August 2015 - 06:05 PM
Well, I thought comparing "1:00" against another number would error, but apparently not (just tested it). Weird, I had no idea Lua could compare stuff with non-number values in it…
1140 posts
Location
Kaunas, Lithuania
Posted 10 August 2015 - 08:39 PM
Either way, you'll want to compare the actual output of os.time(), rather than the formatted string.
17 posts
Posted 11 August 2015 - 02:29 AM
Does that mean i could declear the os.time() as a local variable:
local currenttime = os.time()
then compare os.time to other local variables such as:
local morning = 0600
local midday = 1200
local night = 1800
local midnight = 0000
if(time == morning) then
moniter.write('Morning')
end
if(time == midday) then
moniter.write('Midday')
end
if(time == night) then
moniter.write('Night')
end
2. Indent your code. Everything inside the while loop should have two spaces in front of it (or a tab, depending on the editor).
I do normaly indent my code but the program i was using, Backets (a html coder) didnt like it when i put in spaces, so i left them out, but when i put it into the computer i would indent it.
957 posts
Location
Web Development
Posted 11 August 2015 - 03:42 AM
Does that mean i could declear the os.time() as a local variable …
You're close, but each of those statements would only be true at a single point during the Minecraft day.
You'll want to check the time against a range of numbers, with the less than (<) or greater than (>) operators:
local morning = 1 --# 1:00 AM
local afternoon = 12 --# 12:00 PM
local night = 19 --# About 7:00 PM
local time
while true do --# Loops forever
time = os.time() --# Each time the loop exicutes, get the new time
if time >= morning and time < afternoon then --# time is between 1 and 12 (including 1, not including 12)
elseif time >= afternoon and time < night then --# time is between 12 and 19 (including 12, not 19)
elseif --# you get the idea
end
sleep(1) --# Make sure that the loop sleeps for at least 1 second. Theres no reason to update any faster
end
17 posts
Posted 11 August 2015 - 08:34 AM
Thanks (again) HPWebcamAble, that fixed everything. Only one thing i had to add a different screen (moniter2) and just put it on the other side of the computer and it now works! :)/>
For anyone else looking at this post and wants this code that has been discussed I have uploaded it to pastebin and you can get it using:
pastebin get 3zf98uGC
in a computer with a sreen on the "back" and "left" of the moniter!
This is a PIC!: