Rednet Tutorial

From ComputerCraft Wiki
Revision as of 20:53, 7 July 2012 by Pinkishu (Talk | contribs)

Jump to: navigation, search

Rednet is a wonderfull thing that allows computers and turtles interact with each other, like the internet.

Getting started

A modem is made with 8 stone surrounding redstone torch.

Rednet scripts

Rednet uses some methods to interact with other computers.

Here is a basic program that runs most of them. It requires 2 PCs.

PC1

rednet.open("right") --enable the modem attached to the right side of the PC
rednet.broadcast("Hello world") --send "Hello world" over rednet to all PCs in range
print("PC1 - Hello world")
id,message = rednet.receive() --wait until something is received over rednet
if id == 2 and message == "Hello from pc2" then
 write("PC2 -")
 print(message)
 rednet.send(2,"How are you") --Send a message only to the PC with ID 2
 print("PC1 - How are you")
 id,message = rednet.receive(10) --Wait until a message arrives or 10 seconds pass
 if message == "Fine thanks" then
  print("PC2 - Fine thanks")
 end
end
print("disconnecting")
rednet.close("right") --disable modem on the right side of the PC

PC2

rednet.open("right") --enable modem on the right side of the PC
id,message = rednet.receive() --wait until a mesage is received
if id == 1 and message == "Hello world" then
 rednet.send(1,"Hello from pc2") -- send a message to only the PC with ID 1
 rednet.receive() -- Wait until a message is received
 if message == "How are you" then
  rednet.broadcast("Fine thanks") -- Send to all PCs in range
 end
end
rednet.close("right") -- disable modem on the right side of the PC

Open PC2 first after that open PC1 and then run the program

What?

When computer sends information with rednet it sends the its id and a message. This can be used at the receiving end to do things if the message and sender id are correct. Hope this helped!