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

[lua][question]Recieving input while in a loop.

Started by luingar, 17 July 2012 - 10:05 PM
luingar #1
Posted 18 July 2012 - 12:05 AM
Right now, i'm trying to create a door lock script from scratch. Right now, it works very well, but is still vulnerable to inserting a disk and restarting it. I've got a bit of code that checks for disks and if they exist, erases and ejects them, but unfortunately the program only runs across that snippet of code when it's not nessicary (before the user enters the password)

What I would like to do is have the whole thing in a loop that runs 4 times per second, but .25 seconds it not nearly long enough for a user to enter a password.
How could I impliment a password system that loops while the user is still entering the password? I considered some kinda thing that checks if the user entered an input and if so, saving it into a variable and when the input is enter, combining the variables into one variable and checking it against the password, but is there a better way?

Here's my current code, for reference.
Spoiler

oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
while true do
if fs.exists("disk") then
  k = fs.open("disk/startup", "No")
  k.writeLine("write no")
  k.close()
  disk.eject("back")
  disk.eject("bottom")
  disk.eject("top")
  disk.eject("front")
  disk.eject("left")
  disk.eject("right")
else
  end
local side = "back"
local password = "limes"
local opentime = 5
term.clear()
term.setCursorPos(1,1)
write("Enter Password Now")
local input = read("*")
if input == password then
term.clear()
term.setCursorPos(1,1)
rs.setOutput(side,true)
sleep(opentime)
rs.setOutput(side,false)
os.reboot()
else
term.clear()
term.setCursorPos(1,1)
print("Password incorrect!")
sleep(5)
os.reboot()
os.reboot()
end
end
os.pullEvent = oldPullEvent
MysticT #2
Posted 18 July 2012 - 12:13 AM
Well, it's pretty much a waste of time, since someone can just shutdown the computer (holding Ctrl-S) insert the disk and then turn on the computer.
Anyway, you can use parallel to run two functions at the same time.
Example:

local function checkDisks()
  while true do
	disk.eject("left")
	disk.eject("right")
	-- etc.
	sleep(0.25)
  end
end

local function lock()
  while true do
	term.clear()
	term.setCursorPos(1, 1)
	write("Enter password: ")
	local pass = read("*")
	if pass == "YourPassword" then
	  -- open door
	else
	  print("Wrong Password")
	  sleep(1)
	end
  end
end

parallel.waitForAny(lock, checkDisks)

Also, this line:

k = fs.open("disk/startup", "No")
Won't work (and maybe throw an error), since "No" is not a valid mode.
luingar #3
Posted 18 July 2012 - 12:53 AM
I did not know you could do that. thanks a lot. Also I edited the code a bit ago and hadn't tested it in game yet, which is why that line was like that lol. Thanks a LOT.
and while they could do that, if they do insert the disk while it's on the computer will erase it which will be very annoying at least.