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!
A night ago (from 12/30/11) I got the ComputerCraft mod (found here) and when I started it I found a huge amount of potential for it and loved it. I had a lot of trouble figuring it out, though, and although my limited knowledge of C++ helped me read some code written on this thread it took me a while to catch onto simply how to turn on a redstone output. I also noticed that even though I Googled as hard as I could, I could not find a single guide or anything helpful besides seeing the API's in help in the terminal, so I decided that I'd write a tutorial on it. Over the day I put this together and I hope I helped a few beginners learn this as well.
Enough of that, let's get started! 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? Yes!
[Tutorial start] 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:
print("Hello, world!")
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:
2+2
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?
4*2
Did you get '8'? Good. In scripting or programming, '*' (star above your 8 key) means to multiply and to divide is:
8/2
Simple? Well let's see..
4+3*8/9+14-2
'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:
a+b
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 **** 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:
redstone.setOutput("back", true)
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:
redstone.setOutput("back", false)
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? Yes!
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!")
end
Don'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!
To make a mod, do I have to learn Java, or can I use Lua?
To make a mod is different from this because this is how to script in a mod called ComputerCraft. This allows you to do redstone interaction and other things in Minecraft via Lua scripting while making a mod for Minecraft would mean a bunch of file editing in the Minecraft Java classes and making such. Yes, to make a mod would require Java knowledge but this isn't for how to make a mod. There are many threads and tutorials about mod making so if you want to make a mod check those out!
Really works , good tutorial.
What about adding some password system or traps(Does really have traps make by lua?)?
And how to make porgrams that have names?
Where can we find the IDE?(Can you answer this first?)
Really works , good tutorial.
What about adding some password system or traps(Does really have traps make by lua?)?
And how to make porgrams that have names?
Where can we find the IDE?(Can you answer this first?)
The IDE can either be Notepad++, gedit, or any other you may prefer (found easy with a Google search). I linked those two at the top, check them out (if you use Windows I suggest Notepad++).
Whats wrong with this!?!? When it comes up to start with, the password is ALWAYS incorrect. If i type “startup” THEN type “pigie” it says password correct!! Please help!:
print (“Welcome, Starting up…”)
sleep (1)
print (“Enter Password to Continue:”)
password = read ()
if password == (correctpassword) then
print (“Password Correct”)
redstone.setOutput (“back”, true)
sleep (5)
redstone.setOutput (“back”, false)
else
print (“Password Incorrect”)
end
correctpassword = “pigie"
Whats wrong with this!?!? When it comes up to start with, the password is ALWAYS incorrect. If i type “startup” THEN type “pigie” it says password correct!! Please help!:
print (“Welcome, Starting up…”)
sleep (1)
print (“Enter Password to Continue:”)
password = read ()
if password == (correctpassword) then
print (“Password Correct”)
redstone.setOutput (“back”, true)
sleep (5)
redstone.setOutput (“back”, false)
else
print (“Password Incorrect”)
end
correctpassword = “pigie"
Programs are read from the top to the bottom by anything that uses them, it doesn't know what the variable 'correctpassword' contains when it reaches the 5th line. Put the last line at the top above everything else, then it'll know what it is when it gets there.
This is a small thing I should add into the introduction to programming as it does make a huge difference. I'll include it in my next update.
Also, add a 'os.shutdown()' after 'end', that'll have the computer turn back off after.
By how this tutorial goes, they are saved in the ComputerCraft folder in mods which is in .minecraft, exactly how you installed it. from the ComputerCraft folder, there's lua/rom. Anything in rom and farther into it is viewable by all computers you make. There is a way to manage computers individually so each has their own files, but that's for another time. I will include that in a future (but soon) update.
Has anyone networking code that works with redstone dust? Tried one on cc-get, but it turned out that it requires RedPower that I just can't use (Water Shader vs. MC Forge kind of deal). I just need basic functionality, sending/receiving strings.
For that to work with regular redstone I don't know. I think there's a way to do something as basic as that as I remember seeing it. I'll ask the other devs (I myself am a novice). Will share what I find.
Good idea, but you know what I want? Networking w/out redpower. On pure redstone dust. This should be harder than with rp, but also possible. The hardest thing is to figure out way to decode bytes into bits (not Int2Bin sort of deal, but to decode it into redstone pulses) and then catch them again on a server program.
That's honestly not something I know anything about, networking isn't really my area. However I know some guys on the team would be able to help. Come by the chat sometime and ask your question at: http://webchat.esper.net/?channels=#computercraft
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?
Yes!
[Tutorial start]
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:
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:
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?
Yes!
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):
Don'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!
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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!
------------------------------------------------------------------------------------------------------------
even when i have already knew this, is very helpfull for really newbs :biggrin.gif: waiting for more to come!
Thank you! I'll add a lot more tomorrow since I'm busy tonight.
To make a mod is different from this because this is how to script in a mod called ComputerCraft. This allows you to do redstone interaction and other things in Minecraft via Lua scripting while making a mod for Minecraft would mean a bunch of file editing in the Minecraft Java classes and making such. Yes, to make a mod would require Java knowledge but this isn't for how to make a mod. There are many threads and tutorials about mod making so if you want to make a mod check those out!
Happy programming/scripting!
What about adding some password system or traps(Does really have traps make by lua?)?
And how to make porgrams that have names?
Where can we find the IDE?(Can you answer this first?)
I agree :biggrin.gif:
The IDE can either be Notepad++, gedit, or any other you may prefer (found easy with a Google search). I linked those two at the top, check them out (if you use Windows I suggest Notepad++).
This is a great mod, i love it!
print (“Welcome, Starting up…”)
sleep (1)
print (“Enter Password to Continue:”)
password = read ()
if password == (correctpassword) then
print (“Password Correct”)
redstone.setOutput (“back”, true)
sleep (5)
redstone.setOutput (“back”, false)
else
print (“Password Incorrect”)
end
correctpassword = “pigie"
Programs are read from the top to the bottom by anything that uses them, it doesn't know what the variable 'correctpassword' contains when it reaches the 5th line. Put the last line at the top above everything else, then it'll know what it is when it gets there.
This is a small thing I should add into the introduction to programming as it does make a huge difference. I'll include it in my next update.
Also, add a 'os.shutdown()' after 'end', that'll have the computer turn back off after.
Happy Scripting!
By how this tutorial goes, they are saved in the ComputerCraft folder in mods which is in .minecraft, exactly how you installed it. from the ComputerCraft folder, there's lua/rom. Anything in rom and farther into it is viewable by all computers you make. There is a way to manage computers individually so each has their own files, but that's for another time. I will include that in a future (but soon) update.
For that to work with regular redstone I don't know. I think there's a way to do something as basic as that as I remember seeing it. I'll ask the other devs (I myself am a novice). Will share what I find.
Planning on it, I will take requests. Might also do redstone pulse counters (they're surprisingly easy and it brings up a great tool to learn).
That's honestly not something I know anything about, networking isn't really my area. However I know some guys on the team would be able to help. Come by the chat sometime and ask your question at: http://webchat.esper.net/?channels=#computercraft
NOT gate:
AND Gate:
OR gate:
I didn't test them because i have a terribly terribly slow laptop tell me if im wrong
I'ts creepy when you're stuck with a group of endermen