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 v1.2 (very) Basic Lua Tutorial
A simple introduction to Lua in ComputerCraft
Please reply and give suggestions, and yes this tutorial works with the latest version of CC as well!
News update for March 26!
Boring introduction stuff:
Hold on, hold on, I gotta say this bit: This tutorial was made with the intention that you already have ComputerCraft installed. If not, please go here to get it. The first half was also made with the intention that you have no programming experience whatsoever, although I suggest you look up a little something about Lua as I can't explain every little thing myself but I'll try to as much as I can. Also, experience playing with a terminal or command prompt will help, too! If you have coded before, whether a little or a lot, then feel free to skip ahead to where I explain the programming for ComputerCraft (it has a red dotted-line above it, but check the second spoiler below the blue line for the setup I have you make). I also suggest you have Notepad++, gedit, or something like them that can view script or code with its syntax (pretty much formatting for the text in code, makes it easier to read and edit the code; called an IDE (Integrated Development Environment)). Yes, we will make our own programs!
P.S. Please excuse formatting problems as the text editor on this forum loves to give me headaches.
Alright, alright, I got that, can we get started?
First let's build a computer. True you can spawn one easily with the TooManyItems mod (one of my favorite mods), but just in case you don't use that you can make one is with a crafting table and by placing regular-old boring stone in all but the bottom and middle slots, one redstone dust in the middle, and a glass panel at the bottom (see photo in spoiler below). Congrats you've just made your first computer!
Feel free to place it anywhere with some open space around it so we can play with some redstone. I suggest placing it atop one block so it's at eye-level (see other picture above). Once it's placed it's ready to go, but before we start working with the computer we'll add something that we can work off of in the computer: Redstone! Put a block of anything redstone can be placed on behind the computer and place redstone and blocks as shown below (you'll see how this will come into play once we start the Lua scripting, we're just getting this out of the way while so we can keep moving with the computer content). Make sure you also get the two blocks, dust, and switch that are to the right of our computer.
Alright, let's go to our computer now!
Stand in front of it and right-click it and a console should open up that you can type in. Type 'hello' (without the hyphens). See how it just said hello back? Wonderful! Now type 'help' and it'll give you a list of other things you can type in and it'll do. Make sure you also try 'programs' and it'll list all the programs that can be run. Go ahead, feel free to play around with it, I'll wait here. No really, go ahead (make sure you try 'adventure'!).
Back yet? Good, we can finally start programming!
In the console, type 'lua', this brings up your Lua command prompt that allows you to input individual commands in Lua like, for example, to turn on a redstone current.
Let's start with something basic. Type this into your prompt and press enter:
It should have printed the same text you put between the quotes. 'print()' is a function and a function is something that does something; in this case it prints text on the screen. Easy, right?
Let's do some math, shall we? No, not you, make the computer do it! Enter this:
Did it output '4'? If it did then congrats your computer was made by someone who knows what they're doing! If not..well I'm not sure how to help you from here .
How about something a bit more complicated?
Did you get '8'? Good. In scripting or programming, '*' (star above your 8 key) means to multiply and to divide is:
Simple? Well let's see..
'18.666666'? Yes, Lua can handle decimals! Isn't scripting awesome? It's almost ready to do your homework, right? Well it can't do everything but it can sure do a lot!
Now try this:
a = 2
b = 3
You just assigned some variables. Variables, as you should have learned in school, are pretty much representatives for a value. Let's add them:
We should have known before even entering those variables that it would output 5 and that's what it did! Remember when I said how variables are representatives for values? That means we can do something like 'print(a+b )' and it will output that, right? Let's try that:
print(a+B) -- It should be a+b but MCF's text editor is a pain in the arse and won't change it.
It should have outputted 5 again. I know, it's awesome!
I'm sure by now you want to know how to turn on that torch that we setup earlier, so let's do that.
Enter this into your prompt:
If you made the setup from earlier, the redstone torch should now glow awesomely marking your amazing win!
Alright, so what the heck does this even mean?
This whole statement is called a facility (also commonly referred to as a function); the first part of this facility ('redstone') states what it's going to play with and the second part ('setOutput') is saying what it's going to do with redstone. The last part ('("back",true)') states where and whether to turn on or off. It's pretty much saying, "This is what I want to interact with and this is what I'm going to do with it" (the last two parts listed above act together).
So how do we turn it off?
Well think about it: the first part is saying what I want to interact with and the second is what I want to do with it, the second part starts with setOutput so it's about choosing a redstone output, then the paranthensis says '"back"' and 'true'. The '"back"' says where and what do you think the "true" does?
Go ahead, enter:
That torch we celebrated turning on is now off. Yes, the second statement in the parentheses says if that output is either on or off; 'true' = on, 'false' = off.
ZOMG THIS IS SOOOO COOL DUDE! So I can make computer programs inside Minecraft that'll do stuff with redstone?
Well how do I do it???????!!1!!! I wanna know!!!
Well for this it'll require going outside of Minecraft and to make use of that IDE I mentioned at the start. We'll also start making use of that switch so we can detect inputs!
Remember back when you installed this mod, the '.minecraft' folder? Go to that and inside your 'mods' folder, go to 'ComputerCraft' and inside that go to 'lua', then 'rom', and finally 'programs'. Do the names of these files look familiar? Well they should as these are the programs you played with earlier. Feel free to open one or two up with your IDE to look at the code, but don't edit anything!
Let's create a folder and call it 'custom'. Go into this folder and create a text file (or for Linux, empty document), call it 'power_check.lua', and open it in your IDE.
Alright, now we can start scripting something! Type this into your IDE (I suggest you actually type it, not just copy/paste; it's also ok if your IDE displays it with different colors):
-- This is a comment. A comment is just a piece of text in your code or script that will be ignored. -- They're useful for writing notes. In Lua, this is one way to write comments, exactly how you see it with two dashes. -- Everything after the two dashes is ignored. Don't type these first 3 lines as these are just explaining and have nothing to do with the script. if redstone.getInput("right",true) then --Checks if this is true (example of a comment note) redstone.setOutput("back",true) print("Turning power on!") else redstone.setOutput("back",false) print("Turning power off!") endDon't worry about what it all means yet, I'll explain that when we run it. Make sure you save it!
Let's go back into Minecraft and in our computer enter 'exit()' if you have not yet exited the Lua command prompt. Type 'cd rom/programs/custom' (this is where that experience I mentioned earlier helps a lot) and type 'ls'. It should output the text 'power_check.lua'. 'cd' changes the directory your computer is looking in and 'ls' shows the files that are in that directory.
Enter 'power_check.lua' and see what happens. Well it outputted some text to you: 'Turning power off!' Let's try that again with the switch to your right turned on.
What happened this time? It outputted text saying 'Turning power off!' Leave your computer by pressing Esc and look at what happened. Go ahead and play with it more.
Well what does this mean??
Well let's look back at the code. You should have recognized a couple things like 'print' and the redstone functions. Read over it and think about what each little thing might mean (hint: Scripting is VERY logical! Don't think too hard!).
Let's start with the very first line you typed, it started with 'if'. If means to check if something is true and if it is it will do what's between 'then' and 'else', 'elseif', or 'end'.
I just said it checks if something is true, well what did it check? It checked 'redstone.getInput("right",true)'. Read over that a few times and if you paid attention earlier you should quickly understand what's going on with that function. Remember my hint, this is all VERY logical!
The next two lines you should already know, it's the 'redstone' and 'print' functions!
The next line says 'else'. This is saying, "if all of what was checked was false, then do this:". The next two lines are nearly the same as the last two, slightly modified.
The last line says 'end', this ends the entire 'if' block and the program automatically quits when there's no more code to run (note how it reads top-to-bottom, just like we do).
Go ahead, play with this script a little and see what else you can do!
More coming soon! Go to this thread to look at other programs and try to learn off them, reading other code always helps you learn!
Check out the next tutorial on how to make a locked door with CC, here!
ComputerCraft Basic Lua Tutorial by Onionnion (Ryan B.) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.