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!
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:
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:
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?
Create a small building like you see above (no, you don't need the 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! 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
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 !)!
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 ! 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