Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

[Mod Tutorial] ComputerCraft How To Make a Locked Door (1/18/12)


  • Please log in to reply
65 replies to this topic

#1

Onionnion
  • Minecraft: Onnionion

Posted 19 January 2012 - 04:42 AM

IMPORTANT NOTICE:

Any questions or issues with CC, please ask on the #computercraft channel on the esper.net IRC server, or here: http://webchat.esper.net/?channels=#computercraft

I can NOT guarantee responses or being able to help with each individual question, but there is always someone there willing to help (including myself).


ComputerCraft Locked Door Tutorial

for CC 1.2+


I see a lot of requests in ComputerCraft's thread about how to make a locked door so I decided to make this my next tutorial. If you do not have ComputerCraft then go here to get it and check out my introductory tutorial on ComputerCraft and Lua here. If you are coming from that tutorial, then great! You are about to learn a bunch of new things that can help expand your scripting possibilities!

A locked door? Finally! I've been requesting this for a while!
Well we're all busy with our own lives. Dan is busy working on the mod and adding awesomeness while the rest of us are supporting and giving ideas and helping however we can while working on our own projects. Reading scripts and doing your own research is key to learning how to script, or even program! I myself am still a beginner with programming and scripting and I teach what I learn, although now I am farther ahead than a simple door-locking script. A big tip I can give is that you can't always learn just by doing some tutorials; look atthe language's reference sheets and manuals, go see more than one tutorial. Just a tip to help learn, but enough of this. Let's get started!

Yay!
Indeed!

First I have to explain a few things; let's start with strings. We're used to working with just numbers from our last tutorial and just numbers, nothing about letters or anything else. So what is a string? A string is variable that can hold information other than just numbers such as symbols and letters, but remember it can hold numbers as well (anything, really).

Let's look at a new function: read()
'read()' lets the user make inputs and whatever is inputted is a string, so if I wanted have the user input a name to be printed on the screen, I'd write:
write("Enter your name: ")
name = read()
print("Hello "..name.."!")


What's that 'write' function you just put there?
'write()' is a function that means the same as 'print()' except 'write()' doesn't put put a blank line after it. I didn't mention this before but 'print()' means "put this text on the screen and end it by starting a blank line". Open your Lua command prompt in Minecraft and enter 'print("Hello")'. Notice how the terminal goes to the next line after the printed text? Now try 'write("Hello")'. This time it printed (or wrote) the text on the screen but after the text it didn't start a new line, it just continued on the same line.
The usefulness of this is to make inputs look nicer so if you have “Enter a number: “ followed by a 'read()', it will all be on the same line. Pretty nice, isn't it? There are many other ways to make the terminal look nicer for your programs. For example, let's say you wanted to have two or more inputs but you wanted to ask for the inputs on the same line. This brings up 'term.setCursorPos(<position on screen>)'. Just as it says, it sets the typing cursor's position on the terminal. The terminal works with X/Y axis just like how you learned in Algebra/Geometry in school except imagine the Y axis mirrored; 8 on the Y is a lower line than 3 on the Y. The top-left corner of the terminal is 1,1 (the CC terminal doesn't do zeros or negatives) and it's 18 lines tall and 50 spaces wide. Where you set the cursor is where the beginning of the text you're writing or printing will be. What if we wanted to start printing something again at the top after we already printed some text? How to do that would be 'term.setCursorPos(1,1)' which sets the cursor back to the top-left corner. After this we can print some more text.

Butwait, the text we're writing is overwriting the text that wasalreadythere and is mixing and it's all weird and stuff!
That's because we didn't clear the terminal yet. All we did was set the cursor's position and that's all that function does. Clearing the terminal is very easy, though, so don't fret! The function for clearing the terminal is 'term.clear()'. Running this clears the entire terminalof all text but does NOT move the cursor back to 1,1. If we wanted to do both we would writethis:
term.clear()
term.setCursorPos(1,1)


Why did you put 'term.clear()' first?
When you run 'term.clear()', it runs just like print and ends by starting on a new line, so whenever we want to clear the screen and put the cursor back at the top we must clear first and then set the cursor's position (remember, programs read from top to bottom just like how we would read it!).

I get all that, but what about the locking door?
That's next! This is a little something that'll be part of the program andit can also help you make better things for the user to look at likewhat you see here:
Posted Image

--------------------------------------------------------------------------------------------------

So you've got this nice little building full of diamond you wish to protect, right? All the noobs running around, stealing and griefing, you don't want your Diamond stash to just disappear, right?

Posted Image

Create a small building like you see above (no, you don't need the
:Diamond: in there) or even just a small entrance shape in some way, or just simply put a door next to a computer.

Let's get into the actual scripting now! Remember how we created that emptyfile in /.minecraft/mods/Computercraft? Let's go there again and makeanother file and name it 'lock.lua'. Open it in your IDE (Notepad++,gedit, etc.) and copy/paste this into it, but read through it after pasting it! This is exactly what I mean by reading scripts and code,read it yourself and try to get an understanding of what it's doing before reading the explanation. Trust me, it helps learn a lot.

Enough of that, here's the script:
term.clear()
term.setCursorPos(1,1)

password = "cheese"  --Sets the password
debug = "applesareyummm" --Sets debugging password
write("Enter your password: ")
input = read()  --Input the password

term.clear()  --Clearing the terminal, and...
term.setCursorPos(1,1)  --...THEN setting the cursor position
if input == password then
	print("Password Correct!")  --If the input is correct!
	rs.setOutput("left",true)  --Change this and the setOutput to the side the door is on
	sleep(2)  --Pause for 2 seconds
	rs.setOutput("left",false)
	os.shutdown()  --Shut down the computer
elseif input == debug then
	exit()  --Exits the program
else
	print("Password Incorrect!")  --If the input is incorrect! (Oh Noes!)
	sleep(2)
	os.shutdown()  --Shut down the computer
end  --Ends the 'if' block


Now the fun part of learning what the heck is going on. Feel free to try running it and see what you can make of it.

First a few potentially confusing things such as the 'rs' in front of 'setOuput'. It means the same as redstone, just shorter. It's just much easier and more productive than typing redstone every time. There are many other things that can be made simpler, such as the 'write' and 'print' functions; you actually don't need the parentheses for them but it really comes down to personal preference, same to redstone functions.


What's that 'elseif' thing?
Think about 'if' and 'else', 'if' means "if this is true then do this", 'else' means "if none of the above is true then do this". 'elseif' means almost the same as 'if' except think of it like "if 'if' isn't true, then check if this is true, and if it is then do this". It lets you check multiple things (yes, that means you can have multiple 'elseif's in it) and in that order. Just remember that once something is true in an entire 'if' block that will then move on past the rest of the entire block, so if I have an 'if' with 3 'elseif' and an 'else' and the first 'elseif' is true, it will skip past the last 'else'. I really shouldn't have to explain it this much and you should have gotten exactly what it meant at first because, remember, IT'S LOGICAL! :rolleyes: So to explain how I'm using it here, if the input is equal to the debug string, then it will exit the program and go to the computer's shell. This is just a little bypass in case you wanted to access the computer Posted Image


The comments explain 'os.shutdown()'. It shuts the computer down, of course! Why do I shut down the computer? Because if you don't shutdown the computer, it will go to the command prompt which means whoever is at the computer can simply go to the Lua terminal and turn on a redstone output (Oh Noes, your :Diamond:!)!


Wait, but what if someone goes up to the computer and starts it up, wouldn't that have it go to its command prompt thingie?
Don't worry, as there's a good workaround for that! Each computer has an ID and that ID is assigned a folder in your world's save folder. Yes, that means a computer can have a file but no other will (the same applies to floppies)!

So how can that help?
We're going to set the computer up so that everytime it starts up it'll run the lock program. To do this, we will copy our lock.lua program and paste it 4 directories up. We'll do this in the computer we'll be using as the lock, so enter that computer and use 'ls' and 'cd' to get to the directory with the lock.lua file (cd 'rom/programs/custom'?). Verify you're in the right folder with the files with 'ls'. Now we'll use the 'cp' program, which is short for copy (yes, we're copying the file). However, there is a startup file there already that we must first remove because the copy command only copies a file and does not overwrite. To delete a file is 'rm <file>' and to copy a file is 'cp <file being copied> <location and filename of new file>'

Enter this into your terminal: rm ../../../startup
And this: cp lock.lua ../../../startup

How this works is first there's the program, cp, then the name of the file you want to copy, then the paste save location and filename. Note how it's copying it as a file named startup and it doesn't have the .lua at the end. CC will only startup a file if its name is exactly 'startup'.
Also, if you go to your Lua folder you will notice the startup file is not there. In your computer, enter 'id' and it'll tell you the computer's ID number. Now go to '/.minecraft/saves/<world name>/computers' and you should see some folders with numbers as their titles (and if you have played with floppies yet, another folder titled that with the ID's of those disks inside that). If you look in the folder with the ID as the computer gave you, you should see the startup file that you copied in your computer. Whenever you start your computer it will start that file and then shutdown when it's done running.


Enough boring reading, I don't pay you to type, let's run this thing!
Hey, you're not paying me, but I'll gladly take donations Posted Image! But I'm tired of the constant typing anyways, let's get this thing running!

Go to your computer and enter 'reboot' (this, of course, reboots it, silly!). If it was all done correctly then the computer should load your program. Test it out, enter a password and make sure the door opens and closes correctly, enter an incorrect password and make sure it shuts back down.

All working? Great! Now it's your turn to add stuff! Change it, edit it, be creative and even try to re-script the whole thing.


-----------------------------------------------------------------------------------------------

Please give reviews and your thoughts and suggestions! There are many little changes and additions I'd like to make but cannot fit in at this time. I shall update A.S.A.P to make it as clear and understandable as possible!

Thanks for the support and for all the help from the guys on the IRC! I'm already thinking of ideas for future tutorials!

Check out this program for easy mass-production of floppies on a MP server: http://bit.ly/wHuEdL

Register or log in to remove.

#2

legomaniack
  • Location: The Computer
  • Minecraft: legomaniack

Posted 20 January 2012 - 01:28 AM

Awesome.
Creator of:
Posted Image

#3

noogii2k
    noogii2k

    Tree Puncher

  • Members
  • 35 posts

Posted 20 January 2012 - 09:15 PM

Very helpful, thanks can you help me with this script though?
n=1
while n <= 2 do
write("Lights? (1/0)")
lights = read()
if lights == 1
redstone.setOutput("back", true)
if lights == 0
redstone.setOutput("back", false)
clear
end

#4

XDki113r
    XDki113r

    Tree Puncher

  • Members
  • 15 posts

Posted 21 January 2012 - 07:20 PM

how about disks ?
How can we read on a floppy disk with the computer ?
Redstone Kit Link : Redstone Kits

#5

Onionnion
  • Minecraft: Onnionion

Posted 25 January 2012 - 04:31 AM

Oh wow, I never received a notification I had replies! Excuse me, I'll reply to the questions tomorrow! Thanks for the thanks! Encourages me to make more! :P

#6

DoubleDingo

Posted 25 January 2012 - 06:41 AM

after i try to initiate the "cp lock.lua ../../../startup" command, i get an "access denied" message. why is that?

#7

Patko0770
  • Location: Minecraft, Stormy Wolfley
  • Minecraft: Patko0770

Posted 25 January 2012 - 08:13 PM

can you please, make a video how to do it? because i dont understand that copying part   :(
Posted Image

#8

Bmsortiz
    Bmsortiz

    Out of the Water

  • Members
  • 3 posts

Posted 26 January 2012 - 03:20 PM

View PostDoubleDingo, on 25 January 2012 - 06:41 AM, said:

after i try to initiate the "cp lock.lua ../../../startup" command, i get an "access denied" message. why is that?
I was not able to copy the file over either but there is a somewhat easier way to do this go to the computer and type "edit startup" then the code we are using from this forum has to be typed in (I know its kinda hard I like to copy paste lol) Be sure to press ctrl and then save the changes then reboot the PC and it will ask for your password like it should! Also dont worry it only applies to the PC you edited the startup all others will still work the same.  Hope this helps!

#9

Onionnion
  • Minecraft: Onnionion

Posted 26 January 2012 - 04:20 PM

View PostDoubleDingo, on 25 January 2012 - 06:41 AM, said:

after i try to initiate the "cp lock.lua ../../../startup" command, i get an "access denied" message. why is that?


View PostPatko0770, on 25 January 2012 - 08:13 PM, said:

can you please, make a video how to do it? because i dont understand that copying part   :(


View PostBmsortiz, on 26 January 2012 - 03:20 PM, said:

I was not able to copy the file over either but there is a somewhat easier way to do this go to the computer and type "edit startup" then the code we are using from this forum has to be typed in (I know its kinda hard I like to copy paste lol) Be sure to press ctrl and then save the changes then reboot the PC and it will ask for your password like it should! Also dont worry it only applies to the PC you edited the startup all others will still work the same.  Hope this helps!

Oh jeez, forgive me please, I forgot something you have to do before you copy and that's to remove the first startup file because cp can't overwrite existing files.
Before you copy, run 'rm ../../../startup' and then you run the copy command. I'll add this to the tut asap once I'm home!

#10

xHDMLx
    xHDMLx

    Tree Puncher

  • Members
  • 21 posts
  • Minecraft: xHDMLx

Posted 26 January 2012 - 04:38 PM

Thanks this was very helpful, please take a diamond.  :rolleyes:

:Diamond:
Posted Image

#11

Bmsortiz
    Bmsortiz

    Out of the Water

  • Members
  • 3 posts

Posted 26 January 2012 - 04:45 PM

Oh that makes since I just thought that the file was in the wrong spot or something lol.  Gonna try this right now!!

#12

The_Purple_Dolphin
  • Location: P.A
  • Minecraft: shatley

Posted 28 January 2012 - 06:43 PM

i have a question. i'm trying to make a script to lock my computer unless i type the right password, but i need to know some kind of command of something to close the program and continue on with using the computer normally.

#13

Bmsortiz
    Bmsortiz

    Out of the Water

  • Members
  • 3 posts

Posted 30 January 2012 - 05:32 PM

View PostThe_Purple_Dolphin, on 28 January 2012 - 06:43 PM, said:

i have a question. i'm trying to make a script to lock my computer unless i type the right password, but i need to know some kind of command of something to close the program and continue on with using the computer normally.
The problem is the program to lock the door replaces the startup for that particular PC.  If you want you can set the PC to not shut down if the correct pass is typed

if input == password then
    print("Password Correct!")  --If the input is correct!
    rs.setOutput("left",true)  --Change this and the setOutput to the side the door is on
    sleep(2)  --Pause for 2 seconds
    rs.setOutput("left",false)
    os.shutdown()  --Shut down the computer

Note: This is only part of the code for this program the rest is on the 1st post of course :)

Just remove the os.shutdown() from the code shown here and after the pause period (might be good to extend it to 5)The PC will allow you to do everything else that you normally do on the PC.

#14

Onionnion
  • Minecraft: Onnionion

Posted 31 January 2012 - 04:27 AM

View PostThe_Purple_Dolphin, on 28 January 2012 - 06:43 PM, said:

i have a question. i'm trying to make a script to lock my computer unless i type the right password, but i need to know some kind of command of something to close the program and continue on with using the computer normally.


View PostBmsortiz, on 30 January 2012 - 05:32 PM, said:

The problem is the program to lock the door replaces the startup for that particular PC.  If you want you can set the PC to not shut down if the correct pass is typed

if input == password then
    print("Password Correct!")  --If the input is correct!
    rs.setOutput("left",true)  --Change this and the setOutput to the side the door is on
    sleep(2)  --Pause for 2 seconds
    rs.setOutput("left",false)
    os.shutdown()  --Shut down the computer

Note: This is only part of the code for this program the rest is on the 1st post of course :)

Just remove the os.shutdown() from the code shown here and after the pause period (might be good to extend it to 5)The PC will allow you to do everything else that you normally do on the PC.

There is a way to do this and I will add it to the tutorial, and that's the 'elseif' statement.

#15

Jkjkelly1
  • Minecraft: Jkjkelly1

Posted 03 February 2012 - 02:37 PM

nice!

#16

Onionnion
  • Minecraft: Onnionion

Posted 05 February 2012 - 04:22 AM

Added a small part for 'elseif'! Now enjoy this random flint & steel: Posted Image

#17

zertech
    zertech

    Iron Miner

  • Members
  • 282 posts

Posted 05 February 2012 - 09:43 PM

ive been messing around with this mod and trying to impliment some sort of loop such a s afor loop and also a repeat statement. I get it to work no problem however eventually after running for maybe 3 seconds or soy an error comes up saying "Too long without yielding",

it seems the mod can only handle a repeating statement iterating only so many times before it has to close the program. Does anyone know how to fix this.

Id like to make it so that the program can be waiting for input indefinitely.

#18

Onionnion
  • Minecraft: Onnionion

Posted 06 February 2012 - 04:54 AM

View Postzertech, on 05 February 2012 - 09:43 PM, said:

ive been messing around with this mod and trying to impliment some sort of loop such a s afor loop and also a repeat statement. I get it to work no problem however eventually after running for maybe 3 seconds or soy an error comes up saying &quot;Too long without yielding",

it seems the mod can only handle a repeating statement iterating only so many times before it has to close the program. Does anyone know how to fix this.

Id like to make it so that the program can be waiting for input indefinitely.

I think i know what you're talking about. That's easily doable with something called passEvent which is run inside a while true loop. Try looking it up in the help files for now and I'll check to see if there's been documentation on it yet or not. If not I'll produce some because it is an excellent tool.

#19

harobreen
  • Location: USA
  • Minecraft: aaronbasham

Posted 07 February 2012 - 12:00 AM

Would it be ok if i sold flopies with this on them in SMP?
Posted Image

#20

Onionnion
  • Minecraft: Onnionion

Posted 07 February 2012 - 12:46 AM

View Postharobreen, on 07 February 2012 - 12:00 AM, said:

Would it be ok if i sold flopies with this on them in SMP?

You typed the code yourself, aside from that I release this and all scripts with Creative Commons. Use it however you wish!

Posted Image