Jump to content

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

[Mod Tutorial] ComputerCraft v1.2 (very) Basic Lua Tutorial (Updated 1/1/12)


  • Please log in to reply
102 replies to this topic

#1

Onionnion
  • Minecraft: Onnionion

Posted 31 December 2011 - 03:14 AM

*
POPULAR

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 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:
Spoiler:
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!
  
Spoiler:
--------------------------------------------------------------------------------------------------------------------------

Spoiler:

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.
Spoiler:

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'!).
Spoiler:

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

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 Posted Image.

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 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:
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!

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!

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



Posted Image

ComputerCraft Basic Lua Tutorial by Onionnion (Ryan B.) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.



Register or log in to remove.

#2

streetstar5
  • Location: Harlem, NY

Posted 31 December 2011 - 06:55 AM

+1
Posted Image

#3

LastKossack

Posted 31 December 2011 - 10:53 PM

View Poststreetstar5, on 31 December 2011 - 06:55 AM, said:

+1

even when i have already knew this, is very helpfull for really newbs :D waiting for more to come!

Posted Image


#4

Onionnion
  • Minecraft: Onnionion

Posted 01 January 2012 - 02:33 AM

View PostLastKossack, on 31 December 2011 - 10:53 PM, said:

even when i have already knew this, is very helpfull for really newbs :D waiting for more to come!

Thank you! I'll add a lot more tomorrow since I'm busy tonight.

#5

Kirak95197
  • Minecraft: Kirak95197

Posted 01 January 2012 - 02:52 AM

To make a mod, do I have to learn Java, or can I use Lua?
Posted Image

#6

Onionnion
  • Minecraft: Onnionion

Posted 01 January 2012 - 07:43 AM

View PostKirak95197, on 01 January 2012 - 02:52 AM, said:

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!

Happy programming/scripting!

#7

bik1230
    bik1230

    Gold Miner

  • Members
  • 370 posts
  • Location: Minecraft
  • Minecraft: bik1230

Posted 01 January 2012 - 06:18 PM

+2

View PostSothe Cuslinde, on 15 January 2012 - 10:56 PM, said:

You know that isn't going to happen. LET THE STUPIDITY... BEGIN!!!!

#8

Onionnion
  • Minecraft: Onnionion

Posted 01 January 2012 - 11:20 PM

Just updated and added more!

#9

123sendodo
    123sendodo

    Lapis Lazuli Collector

  • Members
  • 1028 posts
  • Location: PeakVille
  • Minecraft: 123sendodo

Posted 02 January 2012 - 09:59 AM

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?)
Check Out My Dragons!

Also check out my about me page. I wanted ppl to know more about me

#10

Asymetrikon

Posted 02 January 2012 - 01:07 PM

Nice Thread. Computercraft is really awesome.

#11

HTxL
    HTxL

    Zombie Killer

  • Members
  • 177 posts

Posted 02 January 2012 - 06:17 PM

+1

View PostLastKossack, on 31 December 2011 - 10:53 PM, said:

even when i have already knew this, is very helpfull for really newbs :D waiting for more to come!
I agree :D

#12

Onionnion
  • Minecraft: Onnionion

Posted 03 January 2012 - 02:16 AM

View Post123sendodo, on 02 January 2012 - 09:59 AM, said:

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++).

#13

Builder4Free
  • Minecraft: pazzword

Posted 03 January 2012 - 09:44 PM

Nice work, you earned my (+1)
MOD EDIT: Builder4Free is awesome!

#14

taneorox
  • Location: Australia
  • Minecraft: taneorox

Posted 05 January 2012 - 02:16 AM

Where are the saves for the programs you make?
This is a great mod, i love it!
Posted Image

#15

taneorox
  • Location: Australia
  • Minecraft: taneorox

Posted 05 January 2012 - 02:38 AM

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"
Posted Image

#16

Onionnion
  • Minecraft: Onnionion

Posted 05 January 2012 - 03:07 AM

View Posttaneorox, on 05 January 2012 - 02:38 AM, said:

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.

Happy Scripting!

#17

Onionnion
  • Minecraft: Onnionion

Posted 05 January 2012 - 03:16 AM

View Posttaneorox, on 05 January 2012 - 02:16 AM, said:

Where are the saves for the programs you make?


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.

#18

Darkhog
    Darkhog

    Redstone Miner

  • Members
  • 642 posts

Posted 06 January 2012 - 03:04 AM

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.
Programmer isn't supposed to have a life. Programmer is supposed to program. If he can't devote every second of his life to code, he doesn't deserve to be programmer. Yeah, that was for you, Necro and others who didn't update their mods unless new MC update made your mod obsolete.
Posted Image

#19

Onionnion
  • Minecraft: Onnionion

Posted 06 January 2012 - 05:34 AM

View PostDarkhog, on 06 January 2012 - 03:04 AM, said:

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.

#20

Darkhog
    Darkhog

    Redstone Miner

  • Members
  • 642 posts

Posted 06 January 2012 - 12:20 PM

I know it's possible. The hardest part would be finding a way to properly encode/decode bytes and I don't speak about Int2Bin sort of deal, but to encode/decode into redstone impulses.

I've contacted Casper, maybe he can think about something.
Programmer isn't supposed to have a life. Programmer is supposed to program. If he can't devote every second of his life to code, he doesn't deserve to be programmer. Yeah, that was for you, Necro and others who didn't update their mods unless new MC update made your mod obsolete.
Posted Image