Difference between revisions of "Making a Password Protected Door"

From ComputerCraft Wiki
Jump to: navigation, search
m (Fix formatting of inline code blocks)
 
(73 intermediate revisions by 34 users not shown)
Line 4: Line 4:
  
 
__TOC__
 
__TOC__
= Easy tutorial with screenshots =
+
= How to make it =
  
 
A password protected door is actually pretty easy, if you break it into steps.
 
A password protected door is actually pretty easy, if you break it into steps.
Line 10: Line 10:
  
 
First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:
 
First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:
[[File:Tutorial1.png]]
 
[[File:cTutorial2.png]]
 
  
Once you're done with the basics, open the computer and edit the "startup" file. (type in edit startup)
+
The front: [[File:Tutorial1.png|border|200px]]
This will make it so the program will be executed when the computer boots.
+
  
Once you access the startup file, enter in these two lines.
+
The back: [[File:cTutorial2.png|border|200px]]
[[File:CTutorial3.png]]
+
  
The first line defines a function, so that we can repeat the program if a password is entered,
+
<br/>
and the second line is assigning whatever the user types to the variable, "t",
+
So for example, if I typed "qwerty", it would be t = "qwerty". It also overrides anything else the user types in,
+
so if I typed in "programs", it would just be t = "programs", and not act as the programs command in the regular
+
OS. This can very useful.
+
  
 +
Alternatively, you can put the computer adjacent to the iron door without using redstone. Once you're done with the basics, open the computer and edit the "startup" file (type in <code>edit startup</code>).
 +
This will make it so the program will be executed when the computer boots.
  
After you're done with the previous lines, enter in the following line:
+
Once you access the startup file, enter in these five lines.
[[File:CTutorial4.png]]
+
  
This makes it so the program checks if the variable that we declared earlier, "t", equals "password". (You can change password to what you want the actual password to be.)
 
  
 +
[[Loops#The_While_loop|while]] true do
 +
  [[term_(API)|term]].[[term.clear|clear]]()
 +
  [[term_(API)|term]].[[term.setCursorPos|setCursorPos]](1, 1)
 +
  [[print]]("Please Enter Password:")
 +
  local input = [[read]]("*")
  
But now you'll need to put in what to do, depending on if the user typed in the right password.
 
This next set of lines will do that:
 
  
[[File:CTutorial5.png]]
 
  
Now, this maay seem complicated, but it's really simple. The four lines after the if line, determine what the computer will do if the password is correct. So if it's correct, It'll set the redstone output from it's back to true (on), and open the door. (this is the "redstone.setOutput("back", true)" command.) It'll then wait two seconds, and turn the redstone back off again. It'll then repeat the function, so you don't need to reboot the computer.
+
The first line opens a loop that will continue running forever, or until we explicitly exit it with the 'break' keyword. The second and third lines clear the screen of whatever may have been on it, and then set the cursor position to the top-left corner of the screen. This ensures that each time the program loops, the text appears in the same place. The fifth line assigns whatever the user types to the variable 'input'. So for example, if I typed "qwerty", it would be assigning "qwerty" to the <code>input</code> variable. In the read call on the third line (<code>read("*")</code>), the asterisk character will be used to replace each letter typed by the user, so that the password stays hidden.
  
However, the last two lines dictate what the computer will do if the password is wrong, which is really just not opening the door, and repeating the function.
+
Here are the next few lines added on:
  
  
And now the finishing touches:
+
[[Loops#The_While_loop|while]] true do
 +
  [[term_(API)|term]].[[term.clear|clear]]()
 +
  [[term_(API)|term]].[[term.setCursorPos|setCursorPos]](1, 1)
 +
  [[print]]("Please Enter Password:")
 +
  input = [[read]]("*")
 +
  if input == "password" then
 +
  [[redstone_(API)|redstone]].[[redstone.setOutput|setOutput]]("back", true)
 +
  [[os.sleep|sleep]](2)
 +
  [[redstone_(API)|redstone]].[[redstone.setOutput|setOutput]]("back", false)
 +
  end
 +
end
  
[[File:CTutorial6.png]]
+
''where "password" is the password needed to open the door. You can modify it.''
  
This adds the "Ends" to the if which is inside the function, and the function. So we've declared our function.
+
The first line we add, <code>if input == "password" then</code> checks whether or not the <code>input</code> variable contains the string "password". The three lines immediately following are only executed if the password is correct. So if it's correct, <code>redstone.setOutput("back", true)</code> sets the redstone output on the back side to true (on), which opens the door. The program then wait two seconds (<code>sleep(2)</code>), and turns the redstone output back off again. The program then reaches the end of the loop and repeats.
Now to start it, we have the computer call the function after it declares it.
+
  
Well, you should have your own password protected door!
+
Please note that this is the basic format for a computer protection program as well. We would put this program in the startup file and instead of outputting redstone signals if the password was correct, we would use the break keyword instead to exit the loop and allow access to the rest of the computer.
Feel free to change the script around, maybe add a few success/failure messages, etc. etc.
+
  
(NOTE: If the program fails somehow, Pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally)
+
Note also that if the program fails somehow, pressing and holding Ctrl + T will terminate it and allow you to edit it, see the [http://computercraft.info/wiki/index.php?title=Making_a_Password_Protected_Door#Stop_people_from_terminating_your_lock No termination section] to stop people from exiting your lock.
  
 +
= Easier to understand code =
  
= Alternative code =
+
Type <code>edit startup</code> and type in this code:
 +
(Use Ctrl + T to terminate the program, all code is explained in the comments, the --'s. It's good to read the comments, so you actually know what the code does, you do not need to have the comments in your code, of course).
  
Use Ctrl + T to terminate the program. You could set it up as a [[Startup|startup script]] to make it run automatically at boot time.
+
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
<code>
+
local password = "bacon" -- Change bacon to what you want your password to be. Be sure to leave the "s around it, though
local function pass()
+
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()
+
[[Loops#The_While_loop|while]] true do
    term.setCursorPos(1, 1)
+
  [[term_(API)|term]].[[term.clear|clear]]() -- Clears the screen
    textutils.slowWrite("Enter password: ")
+
  [[term_(API)|term]].[[term.setCursorPos|setCursorPos]](1,1) -- Fixes the cursor position
    t = io.read()
+
  [[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 censors out the password
    -- Replace 'password' by the one you want
+
  if input == password then -- Checks if the user inputted the correct password
    if t == "password" then
+
  [[term_(API)|term]].[[term.clear|clear]]() -- Already explained up top
        textutils.slowWrite("Access granted!")
+
  [[term_(API)|term]].[[term.setCursorPos|setCursorPos]](1,1)
        redstone.setOutput("back", true)
+
  [[print]]("Password correct!") -- Prints 'Password correct!' to the screen
        sleep(2)
+
  [[redstone_(API)|rs]].[[redstone.setOutput|setOutput]](side,true) -- Output a redstone current to the side you specified
        redstone.setOutput("back", false)
+
  [[os.sleep|sleep]](opentime) -- Wait the amount of seconds you specifed, then..
    else
+
  [[redstone_(API)|rs]].[[redstone.setOutput|setOutput]](side,false) -- Stop outputting a redstone current
        textutils.slowWrite("Access denied!")
+
  else -- Checks if the user didn't input the correct password
        sleep(2)
+
  [[print]]("Password incorrect!") -- Prints 'Password incorrect!' to the screen
    end
+
  [[os.sleep|sleep]](2) -- Waits 2 seconds
 +
  end
 
  end
 
  end
+
 
while true do
+
= Stop people from terminating your lock =
    pass()
+
end
+
</code>
+
= No Termination =
+
  
 
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:
 
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:
<pre><code>os.pullEvent() = os.pullEventRaw</code></pre>
 
  
 +
os.pullEvent = os.pullEventRaw
 +
 +
If you don't add this code to the top of your program, someone could terminate the lock and enter in a command and they can unlock your door without knowing the password. Adding the code is useful in Multiplayer.
 +
 +
To re-enable the ability to terminate the program, you must first make a copy of <code>os.pullEvent</code>, then you can restore it later.
 +
 +
local pullEvent = os.pullEvent
 +
os.pullEvent = os.pullEventRaw
 +
 +
-- If the desired conditions are met, you can run this line to allow the program to be terminated.
 +
os.pullEvent = pullEvent
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 22:05, 12 September 2017

This tutorial covers on how to make a computer output redstone current when the right password is typed in. The current is then used to trigger an iron door.


How to make it

A password protected door is actually pretty easy, if you break it into steps.


First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:

The front: Tutorial1.png

The back: CTutorial2.png


Alternatively, you can put the computer adjacent to the iron door without using redstone. Once you're done with the basics, open the computer and edit the "startup" file (type in edit startup). This will make it so the program will be executed when the computer boots.

Once you access the startup file, enter in these five lines.


while true do
 term.clear()
 term.setCursorPos(1, 1)
 print("Please Enter Password:")
 local input = read("*")


The first line opens a loop that will continue running forever, or until we explicitly exit it with the 'break' keyword. The second and third lines clear the screen of whatever may have been on it, and then set the cursor position to the top-left corner of the screen. This ensures that each time the program loops, the text appears in the same place. The fifth line assigns whatever the user types to the variable 'input'. So for example, if I typed "qwerty", it would be assigning "qwerty" to the input variable. In the read call on the third line (read("*")), the asterisk character will be used to replace each letter typed by the user, so that the password stays hidden.

Here are the next few lines added on:


while true do
 term.clear()
 term.setCursorPos(1, 1)
 print("Please Enter Password:")
 input = read("*")
 if input == "password" then
  redstone.setOutput("back", true)
  sleep(2)
  redstone.setOutput("back", false)
 end
end

where "password" is the password needed to open the door. You can modify it.

The first line we add, if input == "password" then checks whether or not the input variable contains the string "password". The three lines immediately following are only executed if the password is correct. So if it's correct, redstone.setOutput("back", true) sets the redstone output on the back side to true (on), which opens the door. The program then wait two seconds (sleep(2)), and turns the redstone output back off again. The program then reaches the end of the loop and repeats.

Please note that this is the basic format for a computer protection program as well. We would put this program in the startup file and instead of outputting redstone signals if the password was correct, we would use the break keyword instead to exit the loop and allow access to the rest of the computer.

Note also that if the program fails somehow, pressing and holding Ctrl + T will terminate it and allow you to edit it, see the No termination section to stop people from exiting your lock.

Easier to understand code

Type edit startup and type in this code: (Use Ctrl + T to terminate the program, all code is explained in the comments, the --'s. It's good to read the comments, so you actually know what the code does, you do not need to have the comments in your code, of course).

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
while true do 
 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 censors 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
 else -- Checks if the user didn't input the correct password
  print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
  sleep(2) -- Waits 2 seconds
 end
end

Stop people from terminating your lock

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

If you don't add this code to the top of your program, someone could terminate the lock and enter in a command and they can unlock your door without knowing the password. Adding the code is useful in Multiplayer.

To re-enable the ability to terminate the program, you must first make a copy of os.pullEvent, then you can restore it later.

local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
-- If the desired conditions are met, you can run this line to allow the program to be terminated.
os.pullEvent = pullEvent