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

Rednet.find

Started by willwac, 27 November 2014 - 08:14 PM
willwac #1
Posted 27 November 2014 - 09:14 PM
Here is a solution to using the rednet api without knowing what side a modem is on.

rednet.find = function()
  local sides = {"top","bottom","left","right","front","back"}
  local trueSide = 0
  for s=1, 6 do if peripheral.getType(sides[s]) == "modem" then trueSide = sides[s] end end
  if trueSide ~= 0 then return trueSide end
end
This program creates a global function "rednet.find()" which will return the side of the first modem it finds.
Edited on 27 November 2014 - 09:56 PM
Dog #2
Posted 27 November 2014 - 10:19 PM
I have some thoughts you may wish to consider, since you're basically duplicating some built in functionality…

If you're just looking to wrap the first modem in CC 1.6+ there is already a 'find' feature that makes this much simpler…

local newModem = peripheral.find("modem")

If you want to keep track of the modem side (or you're using CC 1.58 or earlier) this is works much like your code, but avoids the extra table and variable…

for _, side in pairs(rs.getSides()) do
  if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then return side end
end

Neither of those solutions determines whether the modem is wired or wireless, though. The following would return the side only if the modem is wireless…

for _, side in pairs(rs.getSides()) do
  if peripheral.isPresent(side) and peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then return side end
end
Edited on 27 November 2014 - 09:24 PM
Lyqyd #3
Posted 27 November 2014 - 10:30 PM
I saw your report. Feel free to change the topic title yourself. Just use the full editor on the first post.
willwac #4
Posted 27 November 2014 - 10:57 PM
I have some thoughts you may wish to consider, since you're basically duplicating some built in functionality…

If you're just looking to wrap the first modem in CC 1.6+ there is already a 'find' feature that makes this much simpler…

local newModem = peripheral.find("modem")

If you want to keep track of the modem side (or you're using CC 1.58 or earlier) this is works much like your code, but avoids the extra table and variable…

for _, side in pairs(rs.getSides()) do
  if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then return side end
end

Neither of those solutions determines whether the modem is wired or wireless, though. The following would return the side only if the modem is wireless…

for _, side in pairs(rs.getSides()) do
  if peripheral.isPresent(side) and peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then return side end
end
I saw your report. Feel free to change the topic title yourself. Just use the full editor on the first post.
Thanks for the feedback.
I forgot about "redstone.getSides()"
Dog #5
Posted 27 November 2014 - 11:14 PM
You're welcome :)/> Feel free to use any of that code as you see fit.
ElvishJerricco #6
Posted 28 November 2014 - 08:06 PM
OpenPeripherals does add a block that lets you connect modems over network cables instead of on sides, so you should account for that.


function rednet.find()
    for i,v in ipairs(peripheral.getNames()) do
        if peripheral.getType(v) == "modem" then
            return v
        end
    end
end