Difference between revisions of "Peripheral.find"

From ComputerCraft Wiki
Jump to: navigation, search
(Undo revision 7172 by Wyvern67 (talk) | kinda unnecessary)
 
Line 6: Line 6:
 
|api=peripheral
 
|api=peripheral
 
|addon=ComputerCraft
 
|addon=ComputerCraft
−
|desc=Introduced by ComputerCraft 1.6, returns a list of handles for connected peripherals that match the supplied type (essentially performing as a filtered version of [[peripheral.wrap|peripheral.wrap()]]).<br><br>
+
|desc=Introduced by ComputerCraft 1.6, returns a list of handles for connected peripherals that match the supplied type (essentially performing as a filtered version of [[peripheral.wrap|peripheral.wrap()]]). If multiple peripherals are found, they are all returned as separate values, in the same order [[peripheral.getNames]]() returns. A custom function may additionally be passed as a parameter - if it's supplied, each matching peripheral's name and handle will be passed to it during the search, and they'll only be included in the final results if it returns <var>true</var>.
−
 
+
−
A custom function may additionally be passed as a parameter - if it's supplied, each matching peripheral's name and handle will be passed to it during the search, and they'll only be included in the final results if it returns "true".
+
 
|examples=
 
|examples=
 
{{Example
 
{{Example
Line 20: Line 18:
 
}}
 
}}
 
{{Example
 
{{Example
−
|desc=Finds 2 '''advanced''' monitors and writes 'Hello' on the first and 'World' on the second.  
+
|desc=Finds 2 '''advanced''' monitors and writes 'Hello' on the first and 'World' on the second.
−
|code= local mon1, mon2 = '''peripheral.find("monitor", function(name, object) return object.isColour() end)'''
+
|code= local mon1, mon2 = '''peripheral.find("monitor", function(name, object) return object.[[term.isColor|isColour]]() end)'''
 
   
 
   
 
  if mon1 then
 
  if mon1 then
Line 34: Line 32:
 
}}
 
}}
 
}}
 
}}
−
 
−
{{Example
 
−
|desc=If six monitors were to be attached to a computer, peripheral.find("monitor") would return their handles in that order: Bottom, top, back, front, right and left.
 
−
That order always stays the same. The bottom peripheral will always have the priority and be returned first.
 
−
peripheral.getNames() follows the same return pattern. It could be used to find out the order in which the peripheral handles will be returned when using peripheral.find().
 
−
|code= local bottomMonitor, topMonitor, backMonitor, frontMonitor, rightMonitor, leftMonitor = '''peripheral.find("monitor")'''
 
−
textutils.tabulate(peripheral.getNames())
 
−
 
−
|output= Will print the string 'bottom top    back  front  right  left' on the terminal.
 
−
}}
 
−
 
−
 
  
 
[[Category:Lua_Core_Functions]]
 
[[Category:Lua_Core_Functions]]

Latest revision as of 16:18, 1 August 2015


Grid Redstone.png  Function peripheral.find
Introduced by ComputerCraft 1.6, returns a list of handles for connected peripherals that match the supplied type (essentially performing as a filtered version of peripheral.wrap()). If multiple peripherals are found, they are all returned as separate values, in the same order peripheral.getNames() returns. A custom function may additionally be passed as a parameter - if it's supplied, each matching peripheral's name and handle will be passed to it during the search, and they'll only be included in the final results if it returns true.
Syntax peripheral.find(string type [, function fnFilter( name, object )])
Returns table handle1, table handle2, ... handles to peripherals of the given type
Part of ComputerCraft
API peripheral

Examples

Grid paper.png  Example
Finds a monitor and writes 'Hello' on it.
Code
local monitor = peripheral.find("monitor")

if monitor then
  monitor.write("Hello")
end
Output If there is a monitor connected 'Hello' will be written on it.



Grid paper.png  Example
Finds 2 advanced monitors and writes 'Hello' on the first and 'World' on the second.
Code
local mon1, mon2 = peripheral.find("monitor", function(name, object) return object.isColour() end)

if mon1 then
  mon1.setTextColour(colours.blue)
  mon1.write("Hello")
end
if mon2 then
  mon2.setTextColour(colours.red)
  mon2.write("World")
end
Output If it finds 1 advanced monitor, 'Hello' will be written on it. If it finds 2 advanced monitors, 'Hello' will be written on the first and 'World' on the second.