This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ComputerCrafter's profile picture

Computer Craft Coding Tutorial

Started by ComputerCrafter, 09 February 2013 - 12:18 PM
ComputerCrafter #1
Posted 09 February 2013 - 01:18 PM
I wasnt sure if anyone had made a thread about this, but I will be explaining the easy basics of all you need to know in computercraft. Please give me feedback on how to make this tutorial better!

This is a WIP (Work In Progress) It might not be done yet :)/> Suggest what I should do next!

Here, I will be explaining a few things. How to get around in your computer, some syntax, how to make files and edit them, and more :)/> Lets start.

Getting Around in CC.

We will be learning:
  1. How to move around programs in CC
  2. How to make a computer
  3. How to look at how many programs you have
  4. How to edit programs

Getting around in computercraft can be quite simple. After making a computer, using this recipe (s = stone, r = redstone, g = glass panes):

s s s
s r s
s g s

Place it down and open it up. Let's get started! First, type

> ls
(LS). This will bring up all the programs that are in your computer currently. If you are just making a new computer, all you will see is "rom". You must be wondering, what is this rom you speak of? Well, let's find out. Type


> cd rom

it will look somewhat like the above code. this brings you into another folder! Congrats, you entered your first folder. Now if you type ls again, you see there are more programs! Lets try opening a file. Type


> edit startup

(This is really important ^^^, you will use it to create programs with ease later on, so remember it!)

This will bring you to a file. You are now editing your first file! This is where the magic happens, where you code all your stuff! This file that you are in will do what you program it to do every time you turn on your computer. To exit your file, press ctrl (usually found in the bottom left corner) and press right on your arrow keys once. Press enter. You should be in the rom folder again. One useful little tool is the previous tool. you can type


> cd ..

and it will bring you to the previous page (our home page, if you will). This is a lot to take in, and only the basics. I covered a lot, but might cover more later in the tutorial. That is it for navigating!



Basic Syntax - Printing, Math, Running a program

We will be learning:
  1. How to print stuff to a computer
  2. How to do math with a computer
  3. How to run a program

Now that you know how to look around in your computer, let's create our first program! Go ahead and open up your computer. Once you get there, let's make a program. Remember that edit command we used earlier? This is where it comes in handy! Go ahead and think of a name for your program. My program name is going to be "math". Go ahead and make yours anything you like.


> edit math

This will make a program called math, and put us inside it! Now, let's put some code in there. We have all been in 1st grade right? This should be easy then! We all know that an addition problem is written using two numbers and a plus sign, right? Well, go ahead and do that! My program looks like:


1+1

Oh wait, that won't work! We have to learn about the print syntax! This is simple. Look at it this way: Your computer likes to interact with you. It wants to talk with you, but has no mouth! So in order to talk with you, it needs to write it out. That is what it does with print! It returns what you said in the program. So lets try this out. The syntax for print looks like:


print()

Yep. That easy. Now, I know you are excited to run your first program. Wait wait, but if you run it now, it doesnt know what to do! You need to give it words to say. Well, your computer has gone through all those grades of math, so it understands 1+1! Lets try it now!


print(1+1)

Your code should look similar to mine. Now to try it out. Press ctrl again and press enter. This saves your program, so you dont lose it! Now press ctrl again and press right once, so you are hovering over exit, and press enter. You are now at the homepage. To run a program, simply type


> programnamehere
So for me it would look like
> math

Ok, so what did I get…. 2! Computers grow up so fast… Well, while I go and tear up in a corner, you mess around with your newly found program! It knows all kinds of math!


print(1+1)
print(1-1)
print(1*1)
print(1/1)

Those all work :)/> So have fun, and see you in the next part!

Basic Syntax 2 - Variables, Strings.

We will be learning:
  1. Variables
  2. How to print variables
  3. Strings

So. You are excited for our next lesson? Well, here it is! We are gonna learn variables! Get started by opening your computer and making a new program. You remember how to do that right? Well, if you dont, go back to the previous lesson :)/> Make a new program, and name it anything you like. My program will be named variable. Start by declaring some variables. Variables are things that you can store things in. Think of a variable like a bank. You have multiple different banks, and you store your stuff in there, but only numbers and strings, no money. And no, I dont mean a piece of string, I mean a string! Most of our things we want the computer to say using print will use strings! Strings go in quotation marks, like this:


print("Hello World!")

Now that we have that handeled, lets declare a variable! My variable will be called myVariable.


local myVariable = 10

This is what the syntax for a variable looks like! The stuff after the = sign is the stuff that will be stored in the variable. I used a number, but you can use our newly learned strings. Remember to use quotation marks! So now that we have a variable, lets make the computer say our variable! Do you know how to do this? Well, it looks like this!


print(YourVariableNameHere)
So for me it would be
print(myVariable)

Now run your code and see what happens! The stuff you stored in the variable should show up! Pretty cool, right? That is all for this lesson, see you in the next one!

Basic Syntax 3 - Booleans, If statements.

We will be learning:
  1. If Statement Syntax
  2. Booleans
  3. Operators
Did you have fun running your math program? Good, because we are starting to learn more and more about computercraft. During these lessons, feel free to go back and re learn something if you forgot how to do it. We are going to learn about the syntax for an if statement. Make a new program called if. Now make 2 variables. For these two variables, we will set booleans! What are booleans, you ask? Well, dont get scared of this new word, it is just a fancy way to say "true or false". So, make a variable and set it to false. Make another variable and set it to true. Now we make an if statement. Dont be scared, just follow what I do!


local myVariable = true
local yourVariable = false
if then
end

Altogether, you should have what I have above. Now I will introduce my best friends, operators! The two operators we are using today are the or and the and operators. It is easy to do this, all you need is a little help. What we can do is check our variables to see if they are true or false! Let's try it out!


local myVariable = true
local yourVariable = false
if myVariable and yourVariable then --This means if myVariable and yourVariable are both true, then run the if statement.
print("We are both true!")
elseif myVariable or yourVariable then --If they arent both true then check if either of them are true
print("One of us is true!")
else --When all else fails, do this code.
print("We are both false :(/>/>/>/>/>/>")
end

Now run your code. If you followed my code, you should get a message from the computer saying "One of us is true!" Mess around with the variables a bit, switch the booleans and see what happens.

Basic Syntax 4 - For Loops.

Hello! It's been a while! Time for some more learning. We have for loops now, this is gonna be fun! This is a little similar to if statements. Lets take a look.


local var hello = true

for hello do
print("Hello!")
hello = false
end


Now, as the header says, this is a loop. Whenever hello is true, print Hello!, and then set it false. But of course, you don't have to set it to false in that statement, for then it only runs once! You can just make the variable hello false somewhere else. For example:


local var IAmTrue = true

local var IAmFalse = false
for IAmTrue = true do
print("I am true!")
IAmFalse = true
end

for IAmFalse do
print("Now I am true!")
IAmTrue = false
end


That's all! See you next time!

Basic Syntax 5 - User Input

Wow, it's been a while since I wrote one of these! Lets see, where did we leave off… Oh yes! For loops… Now we should do user input. Lets get started!


print("Enter your name: ")
local input = read()
print("Your name is ", input, "!")


Woah, this is new… Lets see… So we make a new local variable named input, and set it to read(). Then we print "Enter your name", they enter their name, and then it prints: "Your name is (name here)!"
Cranium #2
Posted 09 February 2013 - 01:44 PM
It looks good right now. You should definitely add to this when you can.
ComputerCrafter #3
Posted 09 February 2013 - 04:30 PM
Updated the coding :)/> Thanks for the feedback :)/>
ComputerCrafter #4
Posted 12 February 2013 - 08:35 AM
Updated some stuff and added more sections.
hoanganhlam #5
Posted 14 February 2013 - 10:24 PM
amazing. thank u
makeme #6
Posted 15 February 2013 - 09:44 AM
Hey i was wondering if you( or someone) would explain tArgs as I'm clueless and cant find how to use them anywhere?

– thanks in advance
theoriginalbit #7
Posted 15 February 2013 - 10:18 AM
Hey i was wondering if you( or someone) would explain tArgs as I'm clueless and cant find how to use them anywhere?

– thanks in advance
tArgs is just a variable name. you can call it anything you wish. you could call it 'args' or 'argv' or 'makeme'…

Quite simply people call it tArgs becuase its a t(able of)Arg(ument)s. All usages of this particular variable name is to gather the runtime arguments, so basically anything else that is typed in with the program when running it from command line. However whenever you see the … appear in code it means that it can accept any amount of parameters

Now this

local tArgs = ...
print(tArgs)
if you run the program with this
test arg1 arg2 arg3
it will only print 'arg1'. this this because we are telling Lua, get the first argument and put it into tArgs. Now if we did this

local tArgs1, tArgs2 = ...
print(tArgs1)
print(tArgs2)
running it in the same way it would print, 'arg1 arg2'. however this is annoying to have so many variables to the arguments, so this is why we tell Lua to get the all and put them in a table, with

local tArgs = {...}
now this means that all the runtime arguments are now in a table. ( curly braces are tables ). now if we were to do this

local tArgs = {...}
print(table.concat(tArgs, " "))
and run it with the same as before, it would print
'arg1 arg2 arg3'
now you can also find out how many arguments the user has provided too with table.getn or the # operator, by doing this

local tArgs = {...}
print(table.getn(tArgs))
print(#tArgs)
using the same way to run it, would print out '3 3'

I hope this is making sense so far. :)/>

now there is one other time we can use … and that is in functions themselves. like so

local function doSomething( ... )
  local params = {...}
  print(table.concat(params," "))
end
now what this means is that we can actually run that 'doSomething' function with multiple parameters and not get unexpected results. for example if we had this code

local function doSomething( param )
  print(param)
end

doSomething( 1 ) -- this prints 1
doSomething( "Hello" ) -- this prints Hello
doSomething( 1, "Hello" ) -- this only prints 1
doSomething( 1, "Hello", "World" ) -- this still only prints 1
so now if we make the one change and add in the … operator if becomes this


local function doSomething( ... )
  local params = { ... }
  print( table.concat( params, " " ) )
end

doSomething( 1 ) -- this prints 1
doSomething( "Hello" ) -- this prints Hello
doSomething( 1, "Hello" ) -- this prints 1 Hello
doSomething( 1, "Hello", "World" ) -- this prints 1 Hello World

Has this all made sense?
makeme #8
Posted 15 February 2013 - 12:12 PM
-SNIP-

thanks I was thinking tArgs was pre-set and was what got the info from the prompt :/
theoriginalbit #9
Posted 15 February 2013 - 12:14 PM
-SNIP-
thanks I was thinking tArgs was pre-set and was what got the info from the prompt :/
no problems. thats ok everyone has to learn at some point :)/>
ThatNewb #10
Posted 12 March 2013 - 09:25 AM
Perfect tutorial for first time users.
KingSlee #11
Posted 16 March 2013 - 01:19 AM
Hi guys,

I got up to the last part of the tutorial but couldn't seem to get the calculator program working. I titled it "calc" and am using an advanced (gold) computer. I get the error message:"bios:338: [string "calc"]:1: unexpected symbol." I've checked it multiple times and have copied exactly off the tutorial but it still returns the error message each time, any ideas?
theoriginalbit #12
Posted 16 March 2013 - 01:32 AM
Hi guys,

I got up to the last part of the tutorial but couldn't seem to get the calculator program working. I titled it "calc" and am using an advanced (gold) computer. I get the error message:"bios:338: [string "calc"]:1: unexpected symbol." I've checked it multiple times and have copied exactly off the tutorial but it still returns the error message each time, any ideas?
Its the fault of the stupid forum software. it shows up like this for us

local input = read()[/size][/size][/size][/size]
[size=6][size=2][size=3][size=4]term.clear()
but those size tags should be ignored, they are not Lua. it should be

local input = read()
term.clear()
Cheesety210 #13
Posted 29 March 2013 - 03:51 PM
Consider making a video, if you do make a video, use audio(voice, specifically, I guess you could use a robot voice generator). Tutorials without voice are hard to follow, because voice lets people explain more, when people type onto a screen for a tutorial they don't explain much, they just type directions (usually).
ComputerCrafter #14
Posted 06 May 2013 - 02:03 PM
Consider making a video, if you do make a video, use audio(voice, specifically, I guess you could use a robot voice generator). Tutorials without voice are hard to follow, because voice lets people explain more, when people type onto a screen for a tutorial they don't explain much, they just type directions (usually).

When I have more time I will. I will also update this tutorial.. Haven't gotten around to it lately.
H4X0RZ #15
Posted 06 May 2013 - 05:53 PM
I think you have copied the calculator part from the wiki, right?
ComputerCrafter #16
Posted 06 May 2013 - 06:06 PM
Partially, I was trying to get ideas on what to build. I felt that a calculator was a good example and worked on making it my own. I will probably replace the calculator as I didn't have much time to work on it and it is dysfunctional. More tutorial coming today most likely.
Nerezza #17
Posted 06 May 2013 - 09:27 PM
This is great! Thank you for taking the time out to post this information. I have no programming experience and this really helped me learn some basics of LUA and how ComputerCraft works. I'll admit I got a bit confused when it came to the business and frustrated when it didn't work, so glad to discover that that code was irrelevant. Still can't seem to get the code working though, but this might be as you mentioned? I get calc:6: attempt to call nil. Also when I try to run calc (what I named it) I have to press enter a second time for the text to come up.
ComputerCrafter #18
Posted 09 May 2013 - 03:25 PM
This is great! Thank you for taking the time out to post this information. I have no programming experience and this really helped me learn some basics of LUA and how ComputerCraft works. I'll admit I got a bit confused when it came to the business and frustrated when it didn't work, so glad to discover that that code was irrelevant. Still can't seem to get the code working though, but this might be as you mentioned? I get calc:6: attempt to call nil. Also when I try to run calc (what I named it) I have to press enter a second time for the text to come up.

Sorry about that, the forum can mess up and add html tags (). I will probably be replacing the calculator soon and adding more stuff, but for now use this as a review: http://computercraft.info/wiki/Calculator_Tutorial
sjele #19
Posted 10 May 2013 - 03:44 AM
In your calculator tutorial, you would be better of by teaching the use of if-elseif-else, and not if-if-if-if. Meaning it would then be like this:


--[[...]]--
local result
if op == "add" then
  result = num1+num2
elseif op == "subtract" then
  result = num1-num2
elseif op == "multiply" then
  result = num1*num2
elseif op == "divide" then
  result = num1/num2
else
  result = "Unknown math mode, you can use: add, subtract, multiply and divide"
end

print("The answer is: "..result)

Also i shrunk it down to one print statement, much cleaner this way. (IMO)

Everyone should use local vars whereever they can, teaching them how to use those is much better then only using globals.
here is some info about local vars:
http://www.lua.org/pil/4.2.html
http://www.computerc...__fromsearch__1
Allso if i remmber right, bubba recently answerd a post about local/global, it was good, can't find it now :(/>/>

You could allso use the ternary operator to do this, but thats not really practical in this case, but it still does the job:

print("\nThe answer is "..(op == "add" and num1+num2 or (op=="div"and num1/num2 or (op == "mul" and num1*num2 or (op=="sub" and num1-num2 or "Unknown mode!")))))
Calculator using ternary:
Spoiler


local op
local num1
local num2
local goodModes = "add sub div mul"

repeat
  term.clear()
  term.setCursorPos(1,1)
  write("What math mode would you like to use: ")
  op = read()
until goodModes:find(op)

repeat
  term.setCursorPos(1,2)
  term.clearLine()
  write("First number: ")
  num1 = tonumber(read())
until type(num1) == "number"  
repeat
  term.setCursorPos(1, 3)
  term.clearLine()
  write("Scond number: ")
  num2 = tonumber(read())
until type(num2) == "number"

print("\nThe answer is "..(op == "add" and num1+num2 or (op=="div"and num1/num2 or (op == "mul" and num1*num2 or (op=="sub" and num1-num2 or "Unknown mode!")))))
RandomMcSomethin #20
Posted 14 August 2013 - 09:17 PM
I need lots of tutorials, I am a noob!
General_Led #21
Posted 15 August 2013 - 08:35 PM
That was very helpful thanks a lot.
ComputerCrafter #22
Posted 01 September 2013 - 05:24 PM
More stuffs added, more to come
NOTUSEDPLEASEDELETE #23
Posted 03 September 2013 - 03:10 AM
Might want to add masking read().
eg. read("*") for a password field.
ComputerCrafter #24
Posted 03 September 2013 - 12:31 PM
Might want to add masking read().
eg. read("*") for a password field.

Might add for a project, such as a password locked door. Or a password locked account to something.
ZaverSLO #25
Posted 05 September 2013 - 12:48 PM
Great tutorial, please make more! Thank you for your time.