mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-14 04:00:30 +00:00
Some examples for rednet
This commit is contained in:
parent
31e6746bdf
commit
7bcc16bb40
@ -200,6 +200,12 @@ and returns if it should be included in the result.
|
|||||||
monitor.write("Hello")
|
monitor.write("Hello")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@usage Find all wireless modems connected to this computer.
|
||||||
|
|
||||||
|
local modems = { peripheral.find("modem", function(name, modem)
|
||||||
|
return modem.isWireless() -- Check this modem is wireless.
|
||||||
|
end) }
|
||||||
|
|
||||||
@usage This abuses the `filter` argument to call @{rednet.open} on every modem.
|
@usage This abuses the `filter` argument to call @{rednet.open} on every modem.
|
||||||
|
|
||||||
peripheral.find("modem", rednet.open)
|
peripheral.find("modem", rednet.open)
|
||||||
|
@ -28,15 +28,19 @@ local tReceivedMessages = {}
|
|||||||
local tReceivedMessageTimeouts = {}
|
local tReceivedMessageTimeouts = {}
|
||||||
local tHostnames = {}
|
local tHostnames = {}
|
||||||
|
|
||||||
--- Opens a modem with the given @{peripheral} name, allowing it to send and
|
--[[- Opens a modem with the given @{peripheral} name, allowing it to send and
|
||||||
-- receive messages over rednet.
|
receive messages over rednet.
|
||||||
--
|
|
||||||
-- This will open the modem on two channels: one which has the same
|
This will open the modem on two channels: one which has the same
|
||||||
-- @{os.getComputerID|ID} as the computer, and another on
|
@{os.getComputerID|ID} as the computer, and another on
|
||||||
-- @{CHANNEL_BROADCAST|the broadcast channel}.
|
@{CHANNEL_BROADCAST|the broadcast channel}.
|
||||||
--
|
|
||||||
-- @tparam string modem The name of the modem to open.
|
@tparam string modem The name of the modem to open.
|
||||||
-- @throws If there is no such modem with the given name
|
@throws If there is no such modem with the given name
|
||||||
|
@usage Open a wireless modem on the back of the computer.
|
||||||
|
|
||||||
|
rednet.open("back")
|
||||||
|
]]
|
||||||
function open(modem)
|
function open(modem)
|
||||||
expect(1, modem, "string")
|
expect(1, modem, "string")
|
||||||
if peripheral.getType(modem) ~= "modem" then
|
if peripheral.getType(modem) ~= "modem" then
|
||||||
@ -94,23 +98,27 @@ function isOpen(modem)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Allows a computer or turtle with an attached modem to send a message
|
--[[- Allows a computer or turtle with an attached modem to send a message
|
||||||
-- intended for a system with a specific ID. At least one such modem must first
|
intended for a system with a specific ID. At least one such modem must first
|
||||||
-- be @{rednet.open|opened} before sending is possible.
|
be @{rednet.open|opened} before sending is possible.
|
||||||
--
|
|
||||||
-- Assuming the target was in range and also had a correctly opened modem, it
|
Assuming the target was in range and also had a correctly opened modem, it
|
||||||
-- may then use @{rednet.receive} to collect the message.
|
may then use @{rednet.receive} to collect the message.
|
||||||
--
|
|
||||||
-- @tparam number nRecipient The ID of the receiving computer.
|
@tparam number nRecipient The ID of the receiving computer.
|
||||||
-- @param message The message to send. This should not contain coroutines or
|
@param message The message to send. This should not contain coroutines or
|
||||||
-- functions, as they will be converted to @{nil}.
|
functions, as they will be converted to @{nil}.
|
||||||
-- @tparam[opt] string sProtocol The "protocol" to send this message under. When
|
@tparam[opt] string sProtocol The "protocol" to send this message under. When
|
||||||
-- using @{rednet.receive} one can filter to only receive messages sent under a
|
using @{rednet.receive} one can filter to only receive messages sent under a
|
||||||
-- particular protocol.
|
particular protocol.
|
||||||
-- @treturn boolean If this message was successfully sent (i.e. if rednet is
|
@treturn boolean If this message was successfully sent (i.e. if rednet is
|
||||||
-- currently @{rednet.open|open}). Note, this does not guarantee the message was
|
currently @{rednet.open|open}). Note, this does not guarantee the message was
|
||||||
-- actually _received_.
|
actually _received_.
|
||||||
-- @see rednet.receive
|
@see rednet.receive
|
||||||
|
@usage Send a message to computer #2.
|
||||||
|
|
||||||
|
rednet.send(2, "Hello from rednet!")
|
||||||
|
]]
|
||||||
function send(nRecipient, message, sProtocol)
|
function send(nRecipient, message, sProtocol)
|
||||||
expect(1, nRecipient, "number")
|
expect(1, nRecipient, "number")
|
||||||
expect(3, sProtocol, "string", "nil")
|
expect(3, sProtocol, "string", "nil")
|
||||||
@ -163,20 +171,43 @@ function broadcast(message, sProtocol)
|
|||||||
send(CHANNEL_BROADCAST, message, sProtocol)
|
send(CHANNEL_BROADCAST, message, sProtocol)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Wait for a rednet message to be received, or until `nTimeout` seconds have
|
--[[- Wait for a rednet message to be received, or until `nTimeout` seconds have
|
||||||
-- elapsed.
|
elapsed.
|
||||||
--
|
|
||||||
-- @tparam[opt] string sProtocolFilter The protocol the received message must be
|
@tparam[opt] string sProtocolFilter The protocol the received message must be
|
||||||
-- sent with. If specified, any messages not sent under this protocol will be
|
sent with. If specified, any messages not sent under this protocol will be
|
||||||
-- discarded.
|
discarded.
|
||||||
-- @tparam[opt] number nTimeout The number of seconds to wait if no message is
|
@tparam[opt] number nTimeout The number of seconds to wait if no message is
|
||||||
-- received.
|
received.
|
||||||
-- @treturn[1] number The computer which sent this message
|
@treturn[1] number The computer which sent this message
|
||||||
-- @return[1] The received message
|
@return[1] The received message
|
||||||
-- @treturn[1] string|nil The protocol this message was sent under.
|
@treturn[1] string|nil The protocol this message was sent under.
|
||||||
-- @treturn[2] nil If the timeout elapsed and no message was received.
|
@treturn[2] nil If the timeout elapsed and no message was received.
|
||||||
-- @see rednet.broadcast
|
@see rednet.broadcast
|
||||||
-- @see rednet.send
|
@see rednet.send
|
||||||
|
@usage Receive a rednet message.
|
||||||
|
|
||||||
|
local id, message = rednet.receive()
|
||||||
|
print(("Computer %d sent message %s"):format(id, message))
|
||||||
|
|
||||||
|
@usage Receive a message, stopping after 5 seconds if no message was received.
|
||||||
|
|
||||||
|
local id, message = rednet.receive(nil, 5)
|
||||||
|
if not id then
|
||||||
|
printError("No message received")
|
||||||
|
else
|
||||||
|
print(("Computer %d sent message %s"):format(id, message))
|
||||||
|
end
|
||||||
|
|
||||||
|
@usage Receive a message from computer #2.
|
||||||
|
|
||||||
|
local id, message
|
||||||
|
repeat
|
||||||
|
id, message = rednet.receive()
|
||||||
|
until id == 2
|
||||||
|
|
||||||
|
print(message)
|
||||||
|
]]
|
||||||
function receive(sProtocolFilter, nTimeout)
|
function receive(sProtocolFilter, nTimeout)
|
||||||
-- The parameters used to be ( nTimeout ), detect this case for backwards compatibility
|
-- The parameters used to be ( nTimeout ), detect this case for backwards compatibility
|
||||||
if type(sProtocolFilter) == "number" and nTimeout == nil then
|
if type(sProtocolFilter) == "number" and nTimeout == nil then
|
||||||
|
Loading…
Reference in New Issue
Block a user