Difference between revisions of "Making a Password Protected Door"

From ComputerCraft Wiki
Jump to: navigation, search
(How to make it)
(Removed inefficient reboots. Explained looping.)
Line 22: Line 22:
 
  sleep(opentime) -- Wait the amount of seconds you specifed, then..
 
  sleep(opentime) -- Wait the amount of seconds you specifed, then..
 
  rs.setOutput(side,false) -- Stop outputting a redstone current
 
  rs.setOutput(side,false) -- Stop outputting a redstone current
  os.reboot() -- Reboot the computer, reopening the lock
+
  os.shutdown() -- Shutdown the computer until the next time someone right clicks the computer
 
else -- Checks if the user didn't input the correct password
 
else -- Checks if the user didn't input the correct password
 
  term.clear()
 
  term.clear()
Line 28: Line 28:
 
  print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
 
  print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
 
  sleep(2) -- Waits 2 seconds
 
  sleep(2) -- Waits 2 seconds
  os.reboot() -- Reboot the computer, reopening the lock
+
  os.shutdown() -- Shutdown the computer until the next time someone right clicks the computer
 
end
 
end
 
</pre>
 
</pre>
  
 
(NOTE: If the program fails somehow, pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally. Visit the 'No Termination' section to stop people from exiting your lock.)
 
(NOTE: If the program fails somehow, pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally. Visit the 'No Termination' section to stop people from exiting your lock.)
 +
 +
= Alternative Ending =
 +
 +
If you want your program to start again when it gets to the end instead of shutting down the computer, add <pre>do while true</pre> before the first line, and add and extra <pre>end</pre> at the end so there are two of them. Then delete the second from last line <pre>os.shutdown</pre>
 +
do while (condition) ... end is a loop structure. While the condition equates to true, it will keep executing the code that lies between the do while (condition) and the end. We can make it always loop by simply writing true as the condition.
  
 
= No Termination =
 
= No Termination =

Revision as of 05:26, 15 April 2012

This tutorial covers on how to make a Computer output redstone current when the right password is typed in. The current is used to trigger an iron door on the left side of the Computer, you can change the side, of course.


How to make it

Here's the code for it, I'll explain what everything does in the comments. (the --'s). In your Computer, type 'edit startup'. Then write this code into there. (You don't need the comments (the --'s) in the final code)

local side = "left" -- Change left to whatever side your door / redstone is on, E.G: left, right, front, back, bottom, top. Be sure to leave the "s around it, though
local password = "bacon" -- Change bacon to what you want your password to be. Be sure to leave the "s around it, though
local opentime = 5 -- Change 5 to how long (in seconds) you want the redstone current to be on. Don't put "s around it, though
term.clear() -- Clears the screen
term.setCursorPos(1,1) -- Fixes the cursor position
write("Password: ") -- Prints 'Password: ' to the screen
local input = read("*") -- Makes the variable 'input' have the contents of what the user types in, the "*" part sensors out the password
if input == (password) then -- Checks if the user inputted the correct password
 term.clear() -- Already explained up top
 term.setCursorPos(1,1)
 print("Password correct!") -- Prints 'Password correct!' to the screen
 rs.setOutput(side,true) -- Output a redstone current to the side you specified
 sleep(opentime) -- Wait the amount of seconds you specifed, then..
 rs.setOutput(side,false) -- Stop outputting a redstone current
 os.shutdown() -- Shutdown the computer until the next time someone right clicks the computer
else -- Checks if the user didn't input the correct password
 term.clear()
 term.setCursorPos()
 print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
 sleep(2) -- Waits 2 seconds
 os.shutdown() -- Shutdown the computer until the next time someone right clicks the computer
end

(NOTE: If the program fails somehow, pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally. Visit the 'No Termination' section to stop people from exiting your lock.)

Alternative Ending

If you want your program to start again when it gets to the end instead of shutting down the computer, add
do while true
before the first line, and add and extra
end
at the end so there are two of them. Then delete the second from last line
os.shutdown

do while (condition) ... end is a loop structure. While the condition equates to true, it will keep executing the code that lies between the do while (condition) and the end. We can make it always loop by simply writing true as the condition.

No Termination

If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:

os.pullEvent = os.pullEventRaw