ComputerCraft Coding; Programs and Tips
ComputerCraft not only allows the player to use the computer, it also allows the player to edit the computer, make new programs, and control more things than they could ever imagine. For example, you could monitor your outside security system by controlling power flow to Tesla Coils, make your door password protected, or even turn on all your machines with a single password. Below are a few coding samples that you could use. The official ComputerCraft Wiki is found at this link: computercraft.info/wiki/Main_Page
The computers mainscreen is the place where you can type in a program name and the program starts("Worm"). However, typing code into this zone will do nothing at all, as the computer only accepts program names in this zone. To create or edit a program so you can type in code, use "edit <programname>" to bring it up. To create a program, just type "edit <newprogramname>". The code that is in all programs is hidden, so there will be nothing there if you type in edit startup, for example. To start making the program different, type in the code you want. Once you have built a program, use "ctrl" on your actual computer to bring up the "save exit" screen and use the arrow keys and enter to select.
All computer programs are made up of code, which is alot of jibber-jabber to most people. Once you know it, it is very simple. Lua uses the simplest form of code, mainly "if", "then", and "else" statements. There are also two types of variables; "string"(used for storing text) and "int"(used for storing numbers). These last two commands can be accessed at all times of the code, and are assigned to different values. For example, "local password = "boom" says that the keyword "program" stands for "boom". You will see this later in the programs. For more advanced commands, try API.
Door Lock Program
Type this code exactly as it is after you type into the main screen "edit startup";
local password = "bacon" (or whatever you want) local side = "left" (whatever side the door is on) local opentime = 5 (says how long the door remains open) while true do term.clear() (clears the screen) term.setCursorPos(1,1) (selects where the user will start typing) print("Password: ") input = read("*") (says that input will be read with *****) if input == password then (says that if the input is the same as the password, then do what comes next) rs.setOutput(side,true) (sets the redstone to "true", or "on") sleep(opentime) (pauses the program for the set time) rs.setOutput(side,false) (turns off the redstone, closing the door) else (this starts a new "if" statement, aka if the password was wrong) print("Password Incorrect") rs.setOutput(side,false) sleep(30) end end
Simple Double Password Computer Startup
This is used for a double password on your computer. Use "edit startup" to access this. The below has been re-formatted to have two passwords.
local password = "bacon" local passcode = "password" (using keyword 'password' twice will make the computer confused and the code won't work) while true do term.clear() term.setCursorPos(1,1) write("Password: ") input = read("*") if input == password then term.clear() term.setCursorPos(1,1) print("Password2: ") input = read("*") if input == passcode then break else print("Password Incorrect") end end (if code doesn't work, adding an extra end sometimes works)
Turtle Programs and API
The API list is over 45 lines long, and that would spam this page, but the link is here.
Turtle's have their own GUI, which means that they have the same capacity for making programs as computers. Thus, to make a new program, type into the turtle "edit programname".
This program is the simplest to make, and isn't that hard to remake. The program is an infinite loop, and the turtle will mine the block in front of it until it runs out of gas or goes out of the chunk.
while turtle.detect() do turtle.dig() turtle.forward() end
Another version going down;
while turtle.detectDown() do turtle.digDown() turtle.down() end
A one-lane staircase going up.
while turtle.detectDown() do turtle.place() turtle.up() turtle.forward() end