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

Computer Basics I

Started by Lyqyd, 01 September 2013 - 02:27 AM
Lyqyd #1
Posted 01 September 2013 - 04:27 AM
This is a tutorial in the Ask a Pro Renewal Project series.

This tutorial has no prerequisite tutorials.

Next suggested tutorial:
Computer Basics II

In this tutorial, we will craft our first computer or turtle, explore the command line and lua prompt, create a startup file, and learn the cd and list commands. To begin, you will need a copy of Minecraft with the appropriate version of ComputerCraft installed.

To craft a computer, you will need seven smooth stone, one redstone dust, and one glass pane. Arrange these items in the crafting grid like so:



Note that you can replace the smooth stone with gold ingots to craft an advanced computer, which adds a colored screen and mouse support. Once a computer is crafted, it cannot be upgraded into an advanced computer. Select your new computer and place it in the world like any other block. Right-click on any side of the computer block to open the computer's interface. You should see a black rectangle with a grey (or gold) border with some text in the upper-left corner, like this:



This is called the terminal or command line interface. We can type into this and the computer will receive the keystrokes. Let's type in `list` and hit the Enter key. We'll see the four letters appear on the screen as we type them, and when we hit Enter, the computer will try to run the "list" program. It's a valid program, so we will get to see the output it prints, which looks something like this:



The list program shows the files and folders in the current directory. If we are using an advanced computer, the folders' names will be colored lime green, while the file names are white. On a normal computer, the files' and folders' names are all in white, of course. As you can see, we only have a folder named "rom" on this computer. The rom folder is present on all computers. It is also read-only, which means that you cannot make any changes to any files or folders in it, or add any new files or folders to it.

Let's see what's inside the rom folder. The first way to figure out what's inside the rom folder is simply to tell the list program to show the contents of that folder instead of the current folder. We can run "list" again, but this time give it an argument telling it a specific folder to show the contents of. Many of the programs in ComputerCraft accept arguments to change their behavior, and some require them to work at all. Type in `list rom` to show the contents of the rom folder:



Now we can see the contents of the rom folder. But there's another way to get there which can be handy at times. We can change our current directory with the "cd" (change directory) program. Since we're in the main folder (or root) of the computer's drive, we type `cd rom` to change our current directory to the rom folder:



Now our prompt looks a little different. It's been hard to tell so far, but the prompt has been showing us our current directory all along. Now that our current directory is the rom folder, it has changed to "rom>". Now if we type `list` again, with no arguments, it will show us the current directory's contents. Now that the current directory is the rom folder, it will show us the same results as when we ran `list rom` earlier.



We can return to the main folder of the computer by using the `cd ..` command. Running `cd ..` always changes the directory to be one level higher than the current directory, if possible. If our current directory was /rom/programs, running `cd ..` would change our current directory to be /rom. Running it again would change our current directory to be the main directory. Let's go ahead and run `cd ..` now:



Note that our prompt has gone back to being just ">", since we are back at the root of the drive. Feel free to play around with navigating through the directories a little bit to familiarize yourself with these two commands. There is one way to get yourself into trouble with the cd command, so I'll demonstrate that problem. In this example, we've changed our current directory to /rom/help:



We try to use `cd ..` to get out of this folder to keep exploring, but wait, what?



An error message?! Why did the cd program suddenly break? Well, the answer is that the help directory contains a bunch of plain-text help files to provide information on each of the programs that ComputerCraft comes with. The shell program that creates the command-line interface that we are using looks for the programs that we tell it to run in the current directory first. Since there is a file named "cd" in the /rom/help directory, which is our current directory, it tries to run that file. But that file is just a plain text help file, so when it tries to run it as a program, it just throws an error and stops!

To fix the problem, we have to tell the shell exactly which "cd" program to try to run, so that it won't try to run the help file for cd instead. The cd program is actually located at /rom/programs/cd, so we type in `/rom/programs/cd ..` to escape the help directory.



Let's move on to the Lua prompt. The "lua" program starts another command-line interface, but this one is a little different than the shell. In the Lua prompt, we can run little bits of actual lua code directly, instead of running program files with Lua in them like the shell does. Type `cd /` to change to the root directory directly, then type `lua` to start the Lua prompt.



You should see a screen a little like this one. Take note of the "Call exit() to exit." line. We'll run a couple of simple lua lines, then use the exit() function to exit the lua prompt. Type in `x = 1 + 1` and hit Enter. The prompt will accept the line and prompt you for another. Type `x`, and the value of the variable (2) should be printed on the next line, something like this:



Type `exit()` to quit the Lua prompt when you're ready. We're back to our familiar command-line. Let's create a startup file next. When a computer is booted up, it will run the Lua code in its startup file. This is commonly used to run a program that you want the computer to do each time it starts, like a door lock program. Type `edit startup` to open the editor. We're starting up the edit program with "startup" as its argument. This tells the edit program that we want to edit the file named "startup" in the current directory. Since that file doesn't exist, it will open up a blank editor. When we save the file in the editor, it will automatically create the file since it did not exist already. The edit program is one of the programs in ComputerCraft that requires an argument when you start it so that it knows what file you're wanting to edit.



We have a blank file, since this is a new computer. Let's make it display a line of text using the print() function. We add `print("Hello, World!")` to the file. To save the file, we hit Ctrl like it says on the bottom line of the screen (on some keyboards, this will be AltGr or another key). A small menu pops up on the bottom line. We can either hit Enter or the S key to save the file. The bottom line will change to "Saved as startup" if all went well:



Now we hit Ctrl again to bring up the menu, and either hit the E key to exit, or use the right arrow key to select Exit and hit Enter. We've now created a startup file and are back to the command line prompt. Let's reboot the computer to see what our startup file will have changed. Type `reboot` and hit Enter. The computer will reboot, and when it comes up, there's our printed line!



In this tutorial, we learned how to create a computer in ComputerCraft. We learned how to use the shell and lua prompts to run programs or bits of lua code. We learned how to use two of the programs ComputerCraft comes with, cd and list. We know how to create, edit and save files in the edit program, and we have learned what the startup file is and does. Thanks for reading!
Zudo #2
Posted 01 September 2013 - 04:47 AM
My friend is reading this, and he has never used CC before! This is a very nice tutorial for beginners. (On behalf of my friend, thanks!)
BigSHinyToys #3
Posted 02 September 2013 - 08:22 AM
Some of the image links are not working ? Is anyone else having this problem?
Symmetryc #4
Posted 02 September 2013 - 09:11 AM
Some of the image links are not working ? Is anyone else having this problem?
Yeah, some of them are showing up with some photo error looking thing.
Lyqyd #5
Posted 02 September 2013 - 06:52 PM
I haven't had any issues with the images. Try doing a forced reload of the page or clearing your cache and let me know if that resolves it. If not, I may have to look into alternate image hosting solutions.
theoriginalbit #6
Posted 02 September 2013 - 06:55 PM
I haven't had any issues with the images. Try doing a forced reload of the page or clearing your cache and let me know if that resolves it. If not, I may have to look into alternate image hosting solutions.
I've got the "The image you are requesting does not exist or is no longer available." image from imgur coming up in place of:
  • `list rom`
  • `cd /rom/help`
  • `cd ..` bug
Lyqyd #7
Posted 03 September 2013 - 02:04 AM
Okay, those three images had somehow disappeared from the album. I've re-uploaded them, so let me know if there are any further issues with the images.
ATMunn #8
Posted 16 October 2013 - 09:28 PM
Lyqyd, I know this isn't related to the tutorial, but I need to ask you something.
I just got my computer craft forums account today, so I can't post a new topic. The reason why this is a problem is: I want to post a new tutorial, but can't, because I can't post a new topic. Can you help me?
theoriginalbit #9
Posted 16 October 2013 - 09:32 PM
Lyqyd, I know this isn't related to the tutorial, but I need to ask you something.
I just got my computer craft forums account today, so I can't post a new topic. The reason why this is a problem is: I want to post a new tutorial, but can't, because I can't post a new topic. Can you help me?
You should learn to read stickies. Especially the forum rules sticky which is global, so it's everywhere! There you will find your answer.
ATMunn #10
Posted 17 October 2013 - 01:45 PM
I have no idea what a sticky is.
Lyqyd #11
Posted 17 October 2013 - 02:28 PM
Sticky (or pinned) posts always appear at the top of the topic list, with other topics listed below them. Announcements, like the Forum Guidelines announcement you should have read before posting anything, appear above the topic list.
ATMunn #12
Posted 18 October 2013 - 04:09 PM
Nevermind, I can post topics now :D/>
theoriginalbit #13
Posted 18 October 2013 - 04:33 PM
Nevermind, I can post topics now :D/>
No, don't just "nevermind" you need to read the forum rules, or it could potentially get you on the ban list very quickly. it's always a good idea to know exactly what the rules are when you're on forums!
Kristopher_Jones #14
Posted 29 October 2015 - 03:37 PM
Thank you!
muralimude #15
Posted 08 March 2016 - 10:04 AM
thank you for your basics
GiantNuker #16
Posted 03 April 2016 - 07:49 PM
basicly linux shell :P/>