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

Timed inputs

Started by Jman, 16 January 2013 - 12:17 PM
Jman #1
Posted 16 January 2013 - 01:17 PM
Okay, I for one am a lua wiz kid, but I have been having trouble trying to create a specific Timed input. What I'm saying is that, I am trying to create a startup program, with a countdown timer with a 10 second duration, but during them 10 seconds there is an option to press the 'Enter' key and prevent the timer from reaching 0 and firing the 'shell.run(CMD)' program, example: when you turn on your computer sometimes it gives you 30 seconds to choose Safe Mode, Normal, ect. except instead when I press enter it stops the timer and closes the program. I have attempted many times but have failed. If you think you could do it then mind providing something that I could use. Here is what I have for one of my attempts:
function clear()
   term.clear()
   term.setCursorPos(1,1)
end
clear()
t=10
while true do
print("Loading Files... Please Wait A Moment..."..t)
print("Press 'Enter' to cancel.")
sleep(1)
t = t - 1
event, override = os.pullEvent("key")
if override == 28 then	  -- 28 is the 'Enter' key
print("Boot Ceased.")
break
elseif t == 0 then
shell.run(CMD)
break
end
end
theoriginalbit #2
Posted 16 January 2013 - 01:19 PM
sounds like you have been putting a lot of work into this, show us what you have got and we can help improve on it :)/>
Jman #3
Posted 16 January 2013 - 02:11 PM
[xml]function clear()
term.clear()
term.setCursorPos(1,1)
end
clear()
t=10
while true do
print("Loading Files… Please Wait A Moment…"..t)
print("Press 'Enter' to cancel.")
sleep(1)
t = t - 1
event, override = os.pullEvent("key")
if override == 28 then – 28 is the 'Enter' key
print("Boot Ceased.")
break
elseif t == 0 then
shell.run(CMD)
break
end
end
[/xml]
Jman #4
Posted 16 January 2013 - 02:13 PM
aparrently my enter key just move the time along. EX: I press enter, forced to wait the sleep count, value = 9; press enter again, value = 8, wait sleep duration.
theoriginalbit #5
Posted 16 January 2013 - 02:21 PM

function clear()
   term.clear()
   term.setCursorPos(1,1)
end
clear()
t=10
while true do
print("Loading Files... Please Wait A Moment..."..t)
print("Press 'Enter' to cancel.")
sleep(1)
t = t - 1
event, override = os.pullEvent("key")
if override == 28 then	  -- 28 is the 'Enter' key
print("Boot Ceased.")
break
elseif t == 0 then
shell.run(CMD)
break
end
end
That will just sit there waiting for a key to be pressed before it event counts down… it would have to be something more like this


local timeout = os.startTimer( 10 )
while true do
  local event = { os.pullEvent() }
  if ( event[1] == "timer" and event[2] == timeout ) then
    shell.run("CMD")
    break
  elseif ( event[1] == "key" or event[2] == keys.enter ) then
    break
  end
end
Jman #6
Posted 16 January 2013 - 02:29 PM
Okay that solved one problem but how may I still contain the {Loading Files… Please Wait A Moment…(#)} output so I know how much time I have remaining and have the additional text, my friends are also going to be using this computer so they need to know which key to press if the script develops a unlikely bug or needs to be changed without having to close the game and reach into the save folder.
theoriginalbit #7
Posted 16 January 2013 - 02:34 PM
Something like this should work…

local timeout = os.startTimer( 1 )
local seconds = 0
local maxSec = 10

local cursorX, cursorY = term.getCursorPos()
while true do
  term.setCursorPos( cursorX, cursorY )
  term.clearLine() -- new line to print may be shorter than old one
  print( "Loading Files... Please wait a moment... "..( maxSec - seconds ) )
  term.clearLine() -- clears next line
  print( "Press enter to cancel..."
  local event = { os.pullEvent() }
  if ( event[1] == "timer" and event[2] == timeout ) then
	seconds = seconds + 1
	
	if seconds == maxSec then
	  shell.run("CMD")
	  break
	else
	  timeout = os.startTimer( 1 )
	end
  elseif ( event[1] == "key" or event[2] == keys.enter ) then
    break
  end
end
Jman #8
Posted 16 January 2013 - 02:39 PM
okay so if I read correctly that counts up, right? So if I change
seconds = seconds + 1
to
seconds = seconds -1

then it should countdown right?
Wait nvm I didn't read the script all the way through, it does count down.
Thank You by the way, probally would have never thought of using local and dual events to run the timer and enter key input simultaneously.
theoriginalbit #9
Posted 16 January 2013 - 02:46 PM
it counts up to 10… but the output on the screen will be counting down… doesn't really matter how the counting is done really… was just my personal choice…
Jman #10
Posted 16 January 2013 - 03:50 PM
While I'm at it I have another issue, selection menus, never figured out how to make them but now I need them for my second menu, my first is just a key input receiver, determines whether a resident or visitor, each has their own menu, Residents have the admin command and the open door command but I need a selection system ,preferred indicators are (">") and (" *….Text_here….* "),just need to be able to toggle between my two options without having to script out a ton of If statements . Heres What I have:
--Console_Terms
Cl=term.clear()
Md=term.setCursorPos(22.5,1)
--Variables.st
T0 = "Main Menu"
T1 = " Open"
T2 = " Admin Control"
--Variables.hl
S1 = "*Open*"
S2 = "*Admin Control*"
--Welcome_Msg
w=true
while w == true do
term.clear()
term.setCursorPos(17.5,1)
print[[Residents press R
						 or
				  Visitors press V]]
--Welcome_input
while true do
  local event, c = os.pullEvent("char")
  c = string.lower(c)
  if c == "r" then
   a=true
   w=false
   break
  elseif c == "v" then
   b=true
   w=false
   break
  end
  end
end
--Resident_Menu.st_{Selection_Menu_Here}
while a == true do
term.clear()
term.setCursorPos(22.5,1)
print(T0)
print(S1)
print(T2)
while true do
event, msi = os.pullEvent("key")
if msi == 28 then
  Open=true
  a=false
  break
elseif msi == 208 then
  term.clear()
  term.setCursorPos(22.5,1)
  print(T0)
  print(T1)
  print(S2)
  while true do
  event, Admin = os.pullEvent("key")
  if admin == 28 then
   pass = "25439w4hjd134673"
   write = ("Password: ")
   input = read("*")
   sleep(1.5)
   if input == pass then
	print("Password Valid")
	term.clear()
	term.setCursorPos(1,1)
   elseif input ~= pass then
	print ("Try again OR Return to Menu?")
	--Selection Here--
   end
   end
  end
  end
end
end
--Open_Command
while Open == true do
rs.setOutput("back", false)
s=15
while true do
  term.clear()
  term.setCursorPos(22.5,1)
  print ("Opening Door")
  term.setCursorPos(22.5,2)
  print("Closing in.."..s)
  sleep(1)
  s = s-1
  if s == 0 then
   term.clear()
   term.setCursorPos(17.5,1)
   print "Times up! Closing Door!"
   rs.setOutput("back", true)
   sleep(2)
   Open=false
   os.reboot()
   break
  end
end
break
end
theoriginalbit #11
Posted 16 January 2013 - 03:54 PM
split each into functions…

then have

if player == "resident" then
  -- call resident function
elseif player == "visitor" then
  -- call visitor functions
else
  -- something else, or error, or don't event include it
end
Jman #12
Posted 16 January 2013 - 03:59 PM
Okay I understood most of that but how exactly do I throw my resident menu into functions, its all one big giant block, it interacts with 80% of the whole script. Been doing a large amount of basic script lately, been coding a game and I've been using basic script for over 2 months now so I'm beginning to forget a lot of non-basic scripting utilities for example dual events and local, plus if it helps clarify I've been coding npc script logs so pretty much all print commands.
theoriginalbit #13
Posted 16 January 2013 - 04:23 PM
function syntax is this

function functionName( )
  -- code in here
end
and to call it

functionName()


now I'm assuming that an resident can perform anything a visitor can

so id split the admin stuff into a function and if the person is validated as an resident, run the function…

but everyones implementation varies… so it comes down to personal choice of how you do it…
Jman #14
Posted 16 January 2013 - 05:35 PM
well acutaly, a resident has more freedom, it is going to be assigned account passwords so that your required to provide a password when a guest is around, there's a whole other program for this, it logs the users who enter the information in the entry computer so that if a visitor enters the premises all the vault doors and secret rooms are closed shut making them invisible to the visitors, these doors do not open til the visitor logs his/her self out of the system,its like a tally system, plus theres a back up button system so that the number in question is not removed until the exit buttons are walked across.
Jman #15
Posted 19 January 2013 - 12:41 PM
Now my secondary commands are broken, "Admin Control" and "Back", they just highlight the option above them when I press enter. Does anyone have an idea as to how to fix this, here's the code:
--Variables.st
T0 = "Main Menu"
T1 = " Open"
T2 = " Admin Control"
T3 = " Back"
--Variables.hl
S1 = "*Open*"
S2 = "*Admin Control*"
S3 = "*Back*"
--Console_Terms
function clear()
term.clear()
end
function Mid()
term.setCursorPos(22.5,1)
end
--Resident_Menu.Res1
function Res1()
  term.clear()
  term.setCursorPos(22.5,1)
  print(T0)
  print(S1)
  print(T2)
  while true do
  event, msi = os.pullEvent("key")
  if msi == 28 then
   Open=true
   break
  elseif msi == 208 or 200 then
   Res2()
   break
  end
  end
end
--Resident_Menu.Res2
function Res2()
term.clear()
term.setCursorPos(22.5,1)
print(T0)
print(T1)
print(S2)
while true do
  event, Admin = os.pullEvent("key")
  if Admin == 208 or 200 then
   term.clear()
   term.setCursorPos(1,1)
   Res1()
   break
  elseif Admin == 28 then
   term.clear()
   term.setCursorPos(1,1)
   admin=true
   break
  end
  end
end
--Admin_function
while admin == true do
   pass = "25439w4hjd134673"
   write ("Password: ")
   input = read("*")
   if input == pass then
	  sleep(1.5)
	  print("Password Valid")
	  term.clear()
	  term.setCursorPos(1,1)
	  break
   elseif input ~= pass then
	sleep(1.5)
	print ("Password Invalid.")
	Res1()
	break
   end
   end
--Visitor_Menu.Vis
function vis()
clear()
Mid()
print(T0)
print(S1)
print(T3)
while true do
  event, v = os.pullEvent("key")
  if v == 200 or 208 then
   vis1()
   break
  elseif v == 28 then
   Open=true
   break
  end
  end
end
--Vistor_Menu.vis1
function vis1()
clear()
Mid()
print(T0)
print(T1)
print(S3)
while true do
  event, s = os.pullEvent("key")
  if s == 200 then
   vis()
   break
  elseif s == 208 then
   vis()
   break
  elseif s == 28 then
   os.reboot()
   break
  end
  end
  end
--Welcome_Msg
w=true
while w == true do
term.clear()
term.setCursorPos(17.5,1)
print[[Residents press R
						 or
				  Visitors press V]]
while true do
  local event, c = os.pullEvent("char")
  c = string.lower(c)
  if c == "r" then
   w=false
   Res1()
   break
  elseif c == "v" then
   w=false
   vis()
   break
  end
  end
end
--Open_Command
while Open == true do
rs.setOutput("back", false)
s=15
while true do
  term.clear()
  term.setCursorPos(22.5,1)
  print ("Opening Door")
  term.setCursorPos(22.5,2)
  print("Closing in.."..s)
  sleep(1)
  s = s-1
  if s == 0 then
   term.clear()
   term.setCursorPos(17.5,1)
   print "Times up! Closing Door!"
   rs.setOutput("back", true)
   sleep(2)
   Open=false
   os.reboot()
   break
  end
end
break
end