Advertisement
theoriginalbit

Peripheral Search

Nov 21st, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.53 KB | None | 0 0
  1. local function findPeripheral(_type, errMsg, callback)
  2.   callback = type(callback) == "function" and callback or function() return true end
  3.   local results = {}
  4.   for _,name in pairs(peripheral.getNames()) do
  5.     if peripheral.getType(name) == _type then
  6.       local periph = peripheral.wrap(name)
  7.       if callback(name, periph) then
  8.         results[#results+1] = periph
  9.       end
  10.     end
  11.   end
  12.   if #results > 0 then
  13.     return unpack(results)
  14.   end
  15.   error(errMsg or "Cannot find ".._type.." attached to this computer", 0)
  16. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement