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

Server boot screen problem

Started by Lukeblade123, 15 February 2014 - 11:26 PM
Lukeblade123 #1
Posted 16 February 2014 - 12:26 AM
Hello everyone I am trying to create a boot screen for my servers so while they are booting if I want them to receive an os upgrade I can do so easily or if I don't do anything within the allotted amount of time it will boot as normal. But when I load this I get what you would expect but the timer gets stuck on 9. Any help would be greatly appreciated.


term.clear()
term.setCursorPos(1,1)
print("RSW Enterprise Edition Mail Server v1.0")
sleep(2)
seconds = 10
wait = 63
i = seconds
while true do
  while i <= seconds and i > 0 do
				i = i - 1
	term.clear()
	term.setCursorPos(1,1)
			   print("What would you like to do?")
print("[1] Update Server OS")
print("[2] Shutdown")
print("[3] or wait "..i.." seconds to load current os")
write("> ")
local input = read()
if input == "2" then
os.shutdown()
elseif input == "1" then
shell.run("receivefile")
				sleep (1)
  end
  if i == 0 then
		i = seconds
		shell.run("\mailserver", "6", "back")
  end
  sleep(wait)
end
end
ikke009 #2
Posted 16 February 2014 - 06:49 AM
Due to the sleep(wait) near the end of the script it waits 63 seconds before counting down 1 "second"
So the countdown timer would take 630 second to go down from 10 to 1.
Bomb Bloke #3
Posted 16 February 2014 - 08:16 AM
That, and the use of read(). That particular function waits until the user types something in and presses return; while it's doing that the rest of your script is sitting there doing not much, and so your timer isn't going to count down.

This situation calls for os.startTimer(). This queues an event which fires after a specified amount of time. You then rig your script to wait for events - if you get keyboard input, you act on that, otherwise if the timer expires you update the display.

Eg:

local myTimer, seconds, i  = os.startTimer(1), 10, 10
local myEvent, par1

term.clear()
term.setCursorPos(1,1)
print("What would you like to do?")
print("[1] Update Server OS")
print("[2] Shutdown")
print()
write("> ")

while true do
	term.setCursorPos(1,4)
	term.clearLine()
	print("[3] or wait "..i.." seconds to load current os")
	
	myEvent, par1 = os.pullEvent()  -- Capture the results of an incoming event.

	if myEvent == "timer" and par1 == myTimer then  -- The event was our timer expiring.
		i = i - 1
		
		if i == 0 then
			i = seconds
			shell.run("\mailserver", "6", "back")
		end
		
		myTimer = os.startTimer(1)  -- Queue another timer to fire a second from now.
		
	else if myEvent == "char" and par1 == "1" then
		shell.run("receivefile")
		
		i = seconds
		myTimer = os.startTimer(1)
	
	else if myEvent == "char" and par1 == "2" then
		os.shutdown()
	end
end