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

password door with trap

Started by Brian87, 18 February 2014 - 10:33 AM
Brian87 #1
Posted 18 February 2014 - 11:33 AM
Hello, I'm trying to make a program which opens a door if the password is correct. Also when the password is incorrect the door opens, but if you step on a pressure plate the computer sends redstone signal to the dispenser which shoots arrows. But how do I make the computer send a signal to the left, while it's sending a signal to the door on its right? I have also tried parallel.waitForAll, but it doesn't work


The code looks like this:


password = "123"
while true do
term.clear()
term.setCursorPos(1,1)
write("Password: ")
local input = read("*")
if input == password then
  term.clear()
  term.setCursorPos(1,1)
  print("Password correct!")
  rs.setOutput("right", true)
  sleep(3)
  rs.setOutput("right", false)
elseif input ~= password then
  term.clear()
  term.setCursorPos(1,1)
  print("Password correct!")
  rs.setOutput("right", true)
  while sleep(3) == true do
   if rs.getInput("back") then
	rs.setOutput("left")
   end
  end
  rs.setOutput("right", false)
end
end
CometWolf #2
Posted 18 February 2014 - 01:33 PM

  while sleep(3) == true do
   if rs.getInput("back") then
		rs.setOutput("left")
   end
  end
Sleep dosen't return any values, and your computer dosen't run while it's waiting for the give time to pass either. So this won't work at all.

To get around this problem, you'll have to use events.
http://www.computerc...3-event-basics/


local timerId = os.startTimer(3) --start a timer
while true do -- never ending loop
  local tEvent = {os.pullEvent()} -- get events
  if tEvent[1] == "timer" and tEvent[2] == timerId then --timer time is up
	break --exit the loop
  elseif tEvent[1] == "redstone" then --redstone change detected
	if rs.getInput"back" then --check the back side
	  rs.setOutput("left",true)'
	end
  end
end
Edited on 18 February 2014 - 02:44 PM
Brian87 #3
Posted 18 February 2014 - 02:36 PM
Thank you, it worked very nice! :D/>