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

Clock+Text

Started by Minimanx, 23 October 2012 - 06:59 PM
Minimanx #1
Posted 23 October 2012 - 08:59 PM
Hello
I am working on a singleplayer server in tekkit and I build a little iron house with an elevator. This house acts sort of like an entrance.
So I started working on the design and I figured "Hey, let's put a monitor here!" and so I did. Now I have a 5x3 block monitor (I'm not going to make it shorter than 3 but I can change the width to 3 if that's better for what I'm about to ask).
So I was wondering how to make a clock and some text above it saying like "Welcome to blablabla". I did try to set it up but I could only get either a clock or some text. I want both (and I think the text has to be a larger size).
Please do help me if you know a code that will work for me! ^^
jag #2
Posted 23 October 2012 - 09:23 PM
Text resizing?
monitor.setTextScale()

Clock?
print(textutils.formatTime(os.clock(), true))

Now to the program:

local monitor = peripheral.wrap( "top" )
monitor.clear()
monitor.setTextScale(2)
monitor.setCursorPos(1,1)
monitor.write("Welcome to blablabla")

while true do
  monitor.setCursorPos(1,2)
  monitor.clearLine()
  monitor.write(textutils.formatTime(os.clock(), true))
  sleep(.5)
end

Play around with it to get the size you want!
Minimanx #3
Posted 23 October 2012 - 11:11 PM
Thanks for your help, it works with some problems though.
The clock is acting weird. It's like it's counting seconds and milliseconds only the milliseconds take "leaps" like 9, 37, 87, 4, 25, etc. just very fast. Also I want the text after "Welcome to.." to be underneath it since there isn't enough room horizontally.
I don't know if you know how to fix these problems, if you do though please do leave a comment.
Thank you.
remiX #4
Posted 24 October 2012 - 12:17 AM
Woah thats weird, I think you might have to set the time to local. I don't know but this works.


local monitor = peripheral.wrap( "top" )
monitor.clear()
monitor.setTextScale(2)
monitor.setCursorPos(1,4)
monitor.write("Welcome to blablabla")

while true do
  monitor.setCursorPos(1,2)
  monitor.clearLine()
  local MCTime = textutils.formatTime(os.time(), false )
  monitor.write(MCTime)
  sleep(.5)
end
ChunLing #5
Posted 24 October 2012 - 01:20 AM
The important bit is the change from os.clock() to os.time(), these are very different functions. os.time() is the minecraft world time, while os.clock() is how long a program has been running. They both return float values, but one is seconds the computer has been running and the other is (in game) hours since midnight (in game).

I say this though I just look at the sky to see the time and use print() calls for all my debugging needs.
Edited on 23 October 2012 - 11:22 PM
Minimanx #6
Posted 24 October 2012 - 01:33 AM
Thank you guys, really appreciate it. Everything works just fine now! ^_^/>/>
I do have one last question though if you can bear with me.. As I was saying I'm playing this on singleplayer and everytime i leave and join my server again I have to actually type "monitor top (program)" for it to show/start on the monitor. Is there a way to make it automatically start?
(And if there's an easy way to center the clock on the screen feel free to let my know as well xD)
ChunLing #7
Posted 24 October 2012 - 04:06 AM
Yes, have your program write a startup file so that it starts itself on a reboot.
local hndl = fs.open("startup","w")
hndl.writeLine("shell.run("programname","stringparameter","..nprm..")") -- nprm represents a number parameter
hndl.close()

As for the auto centering, you can just get the monitor size and divide it by half, then write to that location.
Minimanx #8
Posted 24 October 2012 - 03:51 PM
Hmmm, the startup code doesn't seem to be working. When I copy it in it says "clock :2: attempt to concatenate nil and string" =/
ChunLing #9
Posted 24 October 2012 - 08:21 PM
Yeah…you need to remove the parameters you're not using. I'm guessing clock doesn't use any parameters, so the line should just be:
hndl.writeLine("shell.run("programname")")
Sorry about the confusion.
Minimanx #10
Posted 24 October 2012 - 09:57 PM
Ah, there we go. Everything works splendidly now! Thank you very much for your help! ^_^/>/>
ChunLing #11
Posted 25 October 2012 - 01:06 AM
Another tip, while it is correct enough to use "" to designate a string, if a string contains a lot of "'s and stuff, double brackets [[]] can be a more effective way to handle those kinds of strings. I should have done that for the previous example.