Difference between revisions of "Rednet Tutorial"

From ComputerCraft Wiki
Jump to: navigation, search
(Created page with "Rednet is a wonderfull thing that allows computers and turtles interact with each other, like internet. == Getting started == Attatch a wireless modem to your computer Modem...")
 
Line 1: Line 1:
Rednet is a wonderfull thing that allows computers and turtles interact with each other, like internet.
+
Rednet is a wonderfull thing that allows computers and turtles interact with each other, like the internet.
  
 
== Getting started ==
 
== Getting started ==
Attatch a wireless modem to your computer
 
  
Modem is made with 8 stone surrounding redstone torch.
+
* Attach a [[Modem|wireless modem]] to your computer.
 +
 
 +
A modem is made with 8 stone surrounding redstone torch.
  
 
== Rednet scripts ==
 
== Rednet scripts ==
Rednet uses some strings to interact with other computers
+
Rednet uses some methods to interact with other computers.
  
Here is a basic program that runs most of them. Need 2 pcs.
+
Here is a basic program that runs most of them. It requires 2 PCs.
  
PC1
+
PC1
  rednet.open("right") --change right to side of modem
+
  rednet.open("right") --enable the modem attached to the right side of the PC
  rednet.broadcast("Hello world")
+
  rednet.broadcast("Hello world") --send "Hello world" over rednet to all PCs in range
 
  print("PC1 - Hello world")
 
  print("PC1 - Hello world")
  rednet.receive()
+
  id,message = rednet.receive() --wait until something is received over rednet
  if ip == 2 and message == "Hello from pc2" then
+
  if id == 2 and message == "Hello from pc2" then
 
   write("PC2 -")
 
   write("PC2 -")
 
   print(message)
 
   print(message)
   rednet.send("2","How are you")
+
   rednet.send(2,"How are you") --Send a message only to the PC with ID 2
 
   print("PC1 - How are you")
 
   print("PC1 - How are you")
   rednet.receive(10)
+
   id,message = rednet.receive(10) --Wait until a message arrives or 10 seconds pass
 
   if message == "Fine thanks" then
 
   if message == "Fine thanks" then
   print("PC2 - Fine thanks"
+
   print("PC2 - Fine thanks")
 
   end
 
   end
 
  end
 
  end
 
  print("disconnecting")
 
  print("disconnecting")
  rednet.close("right")
+
  rednet.close("right") --disable modem on the right side of the PC
  
PC2
+
PC2
  rednet.open("right")
+
  rednet.open("right") --enable modem on the right side of the PC
  rednet.receive()
+
  id,message = rednet.receive() --wait until a mesage is received
  if ip == 1 and message == "Hello world" then
+
  if id == 1 and message == "Hello world" then
   rednet.send("1","Hello from pc2")
+
   rednet.send(1,"Hello from pc2") -- send a message to only the PC with ID 1
   rednet.receive()
+
   rednet.receive() -- Wait until a message is received
 
   if message == "How are you" then
 
   if message == "How are you" then
   rednet.broadcast("Fine thanks")
+
   rednet.broadcast("Fine thanks") -- Send to all PCs in range
 
   end
 
   end
 
  end
 
  end
  rednet.close("right")
+
  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? ==
  
Open Pc2 first after that open pc1 and then run the program
+
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!
==what?==
+
When computer sends information with rednet it sends the id and message. This can be used in receiving end to activate some strings if message is correct and id is correct. Hope this helped!
+

Revision as of 20:53, 7 July 2012

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!