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

How To Integrate An Auto Updating Clock With A Login System?

Started by CitricThunder, 06 August 2013 - 01:37 AM
CitricThunder #1
Posted 06 August 2013 - 03:37 AM
I want to have a clock that auto updates on my screen during my login program without it interfering with the typing. Here is my code-

functions program

function terminal()
  term.clear()
  term.setCursorPos(1,1)
end
function cursor(x, y)
  term.setCursorPos(x, y)
end
function gui()
  cursor(38, 1)
  write("Computer ID: " ..os.computerID())
  cursor(40, 19)
  write("LogTech V1.0")
end
function time()
  time = os.time()
  time = textutils.formatTime(time, false)
  write("The time is "..time)
end

startup program

shell.run("functions")
terminal()
gui()
cursor(1, 19)
time() --Here is where i tried to output the time
cursor(1, 1)
write("Enter Username: ")
os.pullEvent = os.pullEventRaw
username = read()
if username == "Brad" then
  cursor(1, 2)
  write("Enter Password " ..username.. ": ")
  password = read('*')
  if password == "butters" then
	terminal()
	print("Welcome " ..username)
  else
	terminal()
	print("Wrong password/username combination")
	os.reboot()
  end
elseif username == "Ryan" then
  cursor(1, 2)
  write("Enter Password " ..username.. ": ")
  password = read('*')
  if password == "titties" then
	terminal()
	print("Welcome " ..username)
  else
	terminal()
	print("Wrong password/username combination")
	os.reboot()
  end
else
  print("Unrecognized Username")
  sleep(2)
  os.reboot()
end
Zudo #2
Posted 06 August 2013 - 03:39 AM
From what I know, you could use the parallel API to do this, but I'm not the one to advice you.
Vilsol #3
Posted 08 August 2013 - 12:00 PM
At first, please load your functions file as an api: os.loadAPI("functions")
In my OS I am using a simple parallel function. It will launch just as everything else is running: parallel.waitForAny(loop, clock)

In your case you have to put the cursor() function inside the time() function so it runs every time.
After that you want to put your whole startup file (starting from line "cursor(1, 1)") inside a function for example loop()
After that remove everything that is before the loop function except os.loadAPI…
At the very end of your file you want to add the function terminal() and gui()
Below them add a line like this: parallel.waitForAny(time, loop)

Note: If you are going to use the read() function then it will stop the clock!
You can make an event listener for key and then add that to a variable.
Lyqyd #4
Posted 08 August 2013 - 12:45 PM
If you use read in one coroutine and the clock is in the other when you're using a parallel call, the clock will continue to run just fine.
TheOddByte #5
Posted 08 August 2013 - 01:23 PM
You could also use a os.pullEvent todo this both for updating the clock and having a custom read function


local text = ""
cTimer = os.startTimer()
while true do
write(text)
evt, p1 = os.pullEvent()
if evt == "char" then text = text..p1 -- Here it adds the pressed char to the string

elseif evt == "key" then if p1 == 14 then text = string.sub(text,1,#text - 1) -- Removing a char from the string when backspace is pressed
elseif p1 == 57 then text = text.." " -- Adding a space when spacebar is pressed
elseif p1 == 28 then -- Code when enter is pressed
   end
elseif evt == "timer" then
 cTimer = os.startTimer(0.8) -- 1 second refresh rate
 --Update the clock here and print it
  end
    end
I'm not sure that this code works..
But I would suggest that you should probably use coroutines for this or the parallel API, Link to Lua coroutines: http://www.lua.org/pil/9.1.html
HurricaneCoder #6
Posted 08 August 2013 - 03:47 PM
ok you can use parallel.waitForAny() to make that clock auto update.