Difference between revisions of "Rednet Tutorial"
From ComputerCraft Wiki
Magiczocker (Talk | contribs) m |
|||
(13 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
− | Rednet is | + | Rednet is an API that allows computers and turtles interact with each other wirelessly. |
== Getting started == | == Getting started == | ||
+ | * Attach a [[Wireless Modem|wireless modem]] to your [[Computer|computer]]. | ||
+ | {{Crafting | ||
+ | |A1=stone |B1=stone |C1=stone | ||
+ | |A2=stone |B2=ender pearl |C2=stone | ||
+ | |A3=stone |B3=stone |C3=stone | ||
+ | |Output=Modem | ||
+ | }} | ||
− | + | A modem is made using 8 stone blocks surrounding a ender pearl. | |
− | + | ||
− | A modem is made | + | |
== Rednet scripts == | == Rednet scripts == | ||
− | Rednet uses | + | Rednet uses several methods to interact with other computers. |
Here is a basic program that runs most of them. It requires 2 PCs. | Here is a basic program that runs most of them. It requires 2 PCs. | ||
PC1 | PC1 | ||
− | rednet.open("right") --enable the modem attached to the right side of the PC | + | [[Rednet_(API)|rednet]].[[Rednet.open|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 | + | [[Rednet_(API)|rednet]].[[Rednet.broadcast|broadcast]]("Hello world") --send "Hello world" over rednet to all PCs in range |
− | print("PC1 - Hello world") | + | [[print]]("PC1 - Hello world") |
− | id,message = rednet.receive() --wait until something is received over rednet | + | id,message = [[Rednet_(API)|rednet]].[[Rednet.receive|receive]]() --wait until something is received over rednet |
if id == 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") --Send a message only to the PC with ID 2 | + | [[Rednet_(API)|rednet]].[[Rednet.send|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") |
− | id,message = rednet.receive(10) --Wait until a message arrives or 10 seconds pass | + | id,message = [[Rednet_(API)|rednet]].[[Rednet.receive|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") --disable modem on the right side of the PC | + | [[Rednet_(API)|rednet]].[[Rednet.close|close]]("right") --disable modem on the right side of the PC |
PC2 | PC2 | ||
− | rednet.open("right") --enable modem on the right side of the PC | + | [[Rednet_(API)|rednet]].[[Rednet.open|open]]("right") --enable modem on the right side of the PC |
− | id,message = rednet.receive() --wait until a mesage is received | + | id,message = [[Rednet_(API)|rednet]].[[Rednet.receive|receive]]() --wait until a mesage is received |
if id == 1 and message == "Hello world" then | 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_(API)|rednet]].[[Rednet.send|send]](1,"Hello from pc2") -- send a message to only the PC with ID 1 |
− | rednet.receive() -- Wait until a message is received | + | id,message = [[Rednet_(API)|rednet]].[[Rednet.receive|receive]]() -- Wait until a message is received |
if message == "How are you" then | if message == "How are you" then | ||
− | rednet.broadcast("Fine thanks") -- Send to all PCs in range | + | [[Rednet_(API)|rednet]].[[Rednet.broadcast|broadcast]]("Fine thanks") -- Send to all PCs in range |
end | end | ||
end | end | ||
− | rednet.close("right") -- disable modem on the right side of the PC | + | [[Rednet_(API)|rednet]].[[Rednet.close|close]]("right") -- disable modem on the right side of the PC |
− | Open PC2 first after that open PC1 and then run the program | + | Open PC2 first after that open PC1 and then run the program |
− | |||
− | + | [[Category:Tutorials]] |
Latest revision as of 06:12, 4 August 2020
Rednet is an API that allows computers and turtles interact with each other wirelessly.
Getting started
- Attach a wireless modem to your computer.
A modem is made using 8 stone blocks surrounding a ender pearl.
Rednet scripts
Rednet uses several 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 id,message = 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