rednet.receive

From ComputerCraft Wiki
Jump to: navigation, search


Grid Redstone.png  Function rednet.receive
Waits for timeout seconds, or until a rednet_message event is found in the event queue (with a matching protocol, if specified). The system must have a modem that's been readied for rednet usage in order for this to function; messages may then be sent to it from remote systems via rednet.send() / rednet.broadcast().

Due to the nature of how events are pulled, using this function will generally cause the system to discard and ignore any other events that occur until the message is received or the time out ends (though Ctrl+T can generally be used to terminate it, if need be). Likewise, if the system is not specifically listening when a given message is sent, chances are high that another "event-eating" function (eg a turtle movement call or most anything else that pauses code execution, such as "sleep") will discard it, making it impossible to "receive". This issue can be bypassed using co-routines - notably, the parallel API offers easy work-arounds.

If a time out value is specified and reached, then nil is returned. Note that protocols are only supported by ComputerCraft 1.6 or later - versions prior returned the distance between the sender and receiver instead (information that can still be gained by inspecting modem_message events directly, but which may not be "accurate" depending on whether any Rednet repeaters are active on your network).

This command listens on modem channel numbers equal to the system's own ID and 65535 (the latter of which is used for broadcasts). More channels may be opened and listened on by dealing with your modems directly via their own API.
Syntax rednet.receive([ [string protocolFilter, ] number timeout])
Returns number senderID, any message, string protocol
Part of ComputerCraft
API rednet

Examples

Grid paper.png  Example
Prints 2 - 4 messages, depending on what's received.
Code
-- Waits until someone sends a message to this computer, then displays it.
local senderId, message, protocol = rednet.receive()
print(message)

-- Waits five seconds or until someone sends a message to this computer, then displays it.
senderId, message, protocol = rednet.receive(5)
print(message)

-- Waits until someone sends a message to this computer with a protocol of "futons", then displays it.
senderId, message, protocol = rednet.receive("futons")
print(message)

-- Waits five seconds or until someone sends a message to this computer with a protocol of "futons", then displays it.
senderId, message, protocol = rednet.receive("futons",5)
print(message)


Grid disk.png Rednet API Functions
rednet.open - rednet.close - rednet.send - rednet.broadcast - rednet.receive - rednet.isOpen - rednet.host - rednet.unhost - rednet.lookup