This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ebernerd's profile picture

[Psst... Help!] RedNet

Started by ebernerd, 11 February 2013 - 12:10 PM
ebernerd #1
Posted 11 February 2013 - 01:10 PM
So, I know there are a bunch of email systems, and I want to make one similar, but I will edit it some.

I download and survey all of their code, and it makes no sense. Could someone please help me understand how to use RedNet?
Djerun #2
Posted 11 February 2013 - 01:57 PM
for a computer to be able to use rednet you first need to tell it that there is a peripheral able to do so and on which side it is
rednet.open(side)
side can be "top", "left","right","front","back" or "bottom"
if you don't know which side the modem is on you can use
peripheral.getType(side) == "modem"
to check all sides until you found one. there are programs in rom/progrograms on your computer that use such a method (I think gps does for example)
when rednet is open you can send or wait for messages
sending is done with
rednet.broadcast(message)
which sends it to all computers listening that are in range and
rednet.send(ID, message)
which only sends that message to the computer with the ID specified
waiting for a message is accomplished with
id, message, distance = rednet.receive(timeout)
which will wait for a message for timeout seconds (leave it empty if you want to wait forever)
id will receive the sending computers id
message the message sent
and distance the distance from sender to receiver
until a message arrives or the timeout is reached the computer yields

if you want to receive more than one message you should put the receiving and processing of the message in a loop
also if you want to access the message on the same computer while still receiving you need to run a mail reader parallel to the receiver eg with
parallel.waitForAny(receiverFunction(), readerFunction())
where the receiver would probably be in a while true loop and the reader which would control all user input would return when the user wants to exit which would (in case of waitForAny) kill the reveiver as well

for more information on the rednet function see http://computercraft...ednet_%28API%29

almost forgot:
rednet.close(side)
stops using the modem do that when your program is done using rednet