Difference between revisions of "Peripheral.find"

From ComputerCraft Wiki
Jump to: navigation, search
Line 6: Line 6:
 
|api=peripheral
 
|api=peripheral
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Returns a handle to a peripheral of the supplied type that can then be used to call methods, as if using [[peripheral.call]](side, method). The same handle is returned using [[peripheral.wrap]](). '''''Requires ComputerCraft 1.6 or later.'''''
+
|desc=Returns a list of handles to connected peripherals that match the supplied type and if the filter function supplied (optional) returns true once called with the peripheral name and peripheral handle as arguments. '''''Requires ComputerCraft 1.6 or later.'''''  
 
|examples=
 
|examples=
 
{{Example
 
{{Example
Line 16: Line 16:
 
  end
 
  end
 
|output=If there is a monitor connected 'Hello' will be written on it.
 
|output=If there is a monitor connected 'Hello' will be written on it.
 +
}}
 +
{{Example
 +
|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)'''
 +
 +
if mon1 then
 +
  mon1.[[term.setTextColor|setTextColour]]([[colors|colours]].blue)
 +
  mon1.[[term.write|write]]("Hello")
 +
end
 +
if mon2 then
 +
  mon2.[[term.setTextColor|setTextColour]]([[colors|colours]].red)
 +
  mon2.[[term.write|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.
 
}}
 
}}
 
}}
 
}}
  
 
[[Category:Lua_Core_Functions]]
 
[[Category:Lua_Core_Functions]]

Revision as of 02:48, 20 April 2014


Grid Redstone.png  Function peripheral.find
Returns a list of handles to connected peripherals that match the supplied type and if the filter function supplied (optional) returns true once called with the peripheral name and peripheral handle as arguments. Requires ComputerCraft 1.6 or later.
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.