Duncan Geere is going to be taking class this morning, students. If you missed any notes for this lesson, you can find them here. Please open you Minecrafts to page 57.
Welcome to the fourth, and final, coding tutorial for ComputerCraft! If you’ve been following the series, you’ll now know how to navigate the operating system, save and load programs, use monitors, use disk drives, and send turtles scurrying around to do your bidding.
What’s left? Well, I’ll wrap up a few loose ends by introducing you to some nifty coding shortcuts called functions, tell you how to wirelessly communicate between computers using rednet modems, and explain how bundled cables work so you can make complex creations.
This week, I won’t list the requirements that you need because it’ll get a bit lengthy. Either cheat to obtain the materials you need, or send a turtle off mining for you.
Right, we’ll start with functions. A function is a way of defining something that you’ll need to do quite often in a program – it’s a way of telling the computer: “When I give this command, do this list of things.” The code looks a bit like this:
Let’s put something in and give it a try. In a new program on your mining turtle, write:
I’m not going to go through this line-by-line, because we’ve seen almost all of these commands before. What should be apparent, however, is that we’re nesting functions within each other – creating building blocks that put together a larger program.
If you fuel and run your turtle, it should now dig three three-block-long trenches. You could easily modify the program to dig out the rows to put water in for a farm, if you wanted to.
Next up, let’s use functions in something more practical. How about making a turtle that places a railway for you, of a length defined by the user? Make some rails, then try this simple program.
print("Railway placer 1.0 running.")
print("How long would you like your railway?")
length = tonumber(read())
print("Creating railway "..length.." long.")
print("Place fuel in slot 16 and rails")
print("in slot 15. Press any key to begin.")
Here, we're using a function to automating the rail process, then repeating it the number of times that the user inputs. The rest is just the user interface.
Okay, next we'll talk about modems. Modems let computers in different parts of your base talk to each other, and you can also craft a wireless turtle which accepts commands remotely. They use something called Rednet to communicate wirelessly.
Rednet is pretty simple to use. There are three main functions. rednet.broadcast() sends a message to all computers in range, rednet.send() will send a message to a specific machine, and rednet.receive() will listen out messages being sent across the network.
To see it in action, hook up two computers with modems. Modems are built by surrounding a redstone torch with stone blocks, a bit like this:
Place it on your computers by right-clicking a side, but you'll need to crouch first with shift so that you don't accidentally open up the interface instead.
Then go to one of the machines and type "lua". This opens up the Lua interactive shell - a way to input commands without putting them into a program and running them first. It'll change the prompt to "lua>". Once you see that, type the following on one of the machines:
In that, I'm assuming you placed it on the top of the computer. Change "top" to "right", "left", "back", "front" or "bottom" if you put it somewhere else. You'll know if it's working because the modem will emit a (very faint) red light. Then type:
The computer will appear to freeze, but all is well. You've basically just told it to listen out for messages for sixty seconds. Now go to the other machine and open the port in the same way as the first command. Then type:
Return to the first machine, and you should see the text appear. Now type "exit()" to get out of the Lua shell and type "id" to find out the number of the computer. This is important, so make a note of it. Then go back into the Lua shell and tell the machine to receive messages for a little longer - 120 seconds or so. Go to the other machine and type:
rednet.send(#, "Secret message")
Where # is replaced by the ID of the first computer. So if it was ID 48, you'd type rednet.send(48, "Secret message"). Return to the first machine again, and you should see it appear just like before - but if you had a third machine hooked up to the network then it wouldn't appear on that machine at all.
You'll notice some numbers around the message too. The first is the ID of the computer that sent the message - useful to identify different users. The second is the distance to the sender, which might seem like a weird number, but if you've got a lost wireless turtle, it can be invaluable in tracking it down.
You can do an awful lot with Rednet modems. Some ideas include a file server or a chat system on your multiplayer server (though be aware of range - you might need some network repeaters, or even cables, to connect long-distance locations. Range of rednet is 64 blocks, or 16 in a thunderstorm.
Speaking of cables, that's the last piece of our puzzle here. If you've got a computer with a disk drive, monitor and modem attached, that only leaves two sides to output redstone signals from - not very flexible if you want to make a complex system. Thankfully, there's a solution - bundled cables.
These are a nifty addition to the ComputerCrafter's toolkit, but they're a little fiddly to use compared to normal redstone. They're filled with 16 different outputs, marked by different colours. To get them to work, you need to tell the computer which part of the wire to use, and you can do that in two ways.
The scary way is to use exponents of two. That's explained quite well in this forum thread. The far easier way is to use the colours API built into ComputerCraft. With this, you can simply type the colour instead, in the format colors.x where x is the colour you're after. Watch the US/UK spelling!
To test them out, trail some bundled cable away from your computer, and set some different coloured cables coming off it which connect to redstone torches or iron doors or whatever you like. Let's say you've got a white cable, a yellow cable and a red cable, attached to the back of your computer.
Typing redstone.setBundledOutput(back,colors.white) will activate the white side, redstone.setBundledOutput(back,colors.yellow) the yellow, and redstone.setBundledOutput(back,colors.red) the red. Any commands you issue after that, will apply to that colour until you change it. To turn all colours off, just send redstone.setBundledOutput(back,0). With this, you can really ramp up the intricacy of your computer networks.
While we've only touched on the basics of coding here, you should now know enough to write your own programs from scratch - checking out the ComputerCraft wiki and forums for any commands you don't know.
Once you're happy with that, move on up to advanced computers and monitors, and maybe even install alternative operating systems like KREOS which include user management systems, antivirus software and even web browsers. One day, you might even code one yourself.
The only limits are your imagination. Go forth and code!