Difference between revisions of "Making a Password Protected Door"

From ComputerCraft Wiki
Jump to: navigation, search
(No Termination)
Line 84: Line 84:
  
 
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:
<code>
+
<code>os.pullEvent() = os.pullEventRaw</code>
 
+
function os.pullEvent()
+
return os.pullEventRaw()
+
end
+
 
+
</code>
+
  
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Revision as of 06:29, 14 April 2012

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.


Easy tutorial with screenshots

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: Tutorial1.png CTutorial2.png

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 two lines. CTutorial3.png

The first line defines a function, so that we can repeat the program if a password is entered, 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.


After you're done with the previous lines, enter in the following line: 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.)


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:

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.

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.


And now the finishing touches:

CTutorial6.png

This adds the "Ends" to the if which is inside the function, and the function. So we've declared our function. Now to start it, we have the computer call the function after it declares it.

Well, you should have your own password protected door! 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)


Alternative code

Use Ctrl + T to terminate the program. You could set it up as a startup script to make it run automatically at boot time.

local function pass()
    term.clear()
    term.setCursorPos(1, 1)
    textutils.slowWrite("Enter password: ")
    t = io.read()
    
    -- Replace 'password' by the one you want
    if t == "password" then
        textutils.slowWrite("Access granted!")
        redstone.setOutput("back", true)
        sleep(2)
        redstone.setOutput("back", false)
    else
        textutils.slowWrite("Access denied!")
        sleep(2)
    end
end

while true do
    pass()
end

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