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

peripheral.find

Started by wilcomega, 08 September 2014 - 05:44 PM
wilcomega #1
Posted 08 September 2014 - 07:44 PM
instead of wrapping it for you, it should return the side where the found peripheral is on.

why?
because when you are using rednet, wanting it to find a modem for you so you dont have to hardcode a side in. when you try to do this, it returns the modem object instead of the side. wich is not usable with rednet.
this will also allow the user to decide what to do with the side, not just to use the peripheral, but also to give instructions, for example "please insert a valid module into the right side sensor". or of course this is usefull for rednet.

this change is fairly easy to change, just remove the part were it wraps the peripheral and return the side instead

how will this affect my programs?
change
local p = peripheral.find("monitor")
to
local p = peripheral.wrap(periperal.find("monitor"))

PLEASE leave a reply with what you think, as i fond it nice when people accually tell me what they think instead of just looking at the post ;)/>
Lyqyd #2
Posted 08 September 2014 - 07:50 PM
You can rather easily do this yourself.


local names = {}

local function filter(name)
  table.insert(names, name)
  return true
end

peripheral.find("monitor", filter)

local mon = peripheral.wrap(names[1])
Bomb Bloke #3
Posted 09 September 2014 - 02:14 AM
Personally, I prefer to put the "side" data into the peripheral tables themselves:

local p = peripheral.find("monitor", function(name, object) object.side = name return true end)

p.side ends up containing, well, you know.
Edited on 09 September 2014 - 12:15 AM
TheOddByte #4
Posted 10 September 2014 - 08:38 PM
You have some good examples above, you could also do this easily with peripheral.getNames

local function find( _type )
    for _, name in ipairs( peripheral.getNames() ) do
        if peripheral.getType( name ) == _type then
            return name
        end
    end
end
Lyqyd #5
Posted 10 September 2014 - 10:04 PM
That would only return the first match, though. The nice thing about peripheral.find is that it returns all matching peripherals.
Agent Silence #6
Posted 12 September 2014 - 07:59 PM
  • for i,v in pairs(peripheral.getNames()) do
  • if peripheral.getType(v) == "modem" then
  • rednet.open(v)
  • end
  • end

Wouldn't that work?
TheOddByte #7
Posted 14 September 2014 - 03:40 PM
That would only return the first match, though. The nice thing about peripheral.find is that it returns all matching peripherals.
Then you could easily modify

local function find( _type )
    local p = {}
    for _, name in ipairs( peripheral.getNames() ) do
        if peripheral.getType( name ) == _type then
            table.insert( p, name )
        end
    end
    return #p > 0 and true, p or false
end

--# Example usage
local success, monitors = find( "monitor" )