Difference between revisions of "Peripheral.getNames"

From ComputerCraft Wiki
Jump to: navigation, search
(Created page)
 
(Fleshed out somewhat + added example.)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{NeedsWork|This page needs an example.}}
 
 
{{lowercase}}
 
{{lowercase}}
 
{{Function
 
{{Function
 
|name=peripheral.getNames
 
|name=peripheral.getNames
 
|args=
 
|args=
|returns=Table of all sides that have a peripheral present.
+
|returns=A numerically indexed table of all peripherals present.
 
|api=peripheral
 
|api=peripheral
 
|addon=ComputerCraft
 
|addon=ComputerCraft
 +
|desc=Provides a list of all peripherals available.<br><br>
 +
If a device is located directly next to the system, then its name will be listed as the side it is attached to. If a device is attached via a [[Wired Modem]], then it'll be reported according to its name on the wired network.<br><br>
 +
For example, if you have a wired modem on the left of a computer, and a monitor attached to that, then the first peripheral returned will be "left" (the wired modem) and the second will be along the lines of "monitor_x" (the monitor).
 +
|examples=
 +
{{Example
 +
|desc=Prints a list of all attached peripherals, along with their type (using [[Peripheral.getType|peripheral.getType()]]).
 +
|code=local periList = peripheral.getNames()
 +
 +
for i = 1, #periList do
 +
print("I have a "..peripheral.getType(periList[i]).." attached as \""..periList[i].."\".")
 +
end
 +
|output=Depending on the peripherals available, along the lines of:<br>
 +
<i>I have a modem attached as "left".<br>
 +
I have a monitor attached as "monitor_0".<br>
 +
I have a turtle attached as "top".<br>
 +
etc...</i>
 +
}}
 +
 
}}
 
}}
 
[[Category:Lua_Core_Functions]]
 
[[Category:Lua_Core_Functions]]

Latest revision as of 05:10, 18 February 2014


Grid Redstone.png  Function peripheral.getNames
Provides a list of all peripherals available.

If a device is located directly next to the system, then its name will be listed as the side it is attached to. If a device is attached via a Wired Modem, then it'll be reported according to its name on the wired network.

For example, if you have a wired modem on the left of a computer, and a monitor attached to that, then the first peripheral returned will be "left" (the wired modem) and the second will be along the lines of "monitor_x" (the monitor).
Syntax peripheral.getNames()
Returns A numerically indexed table of all peripherals present.
Part of ComputerCraft
API peripheral

Examples

Grid paper.png  Example
Prints a list of all attached peripherals, along with their type (using peripheral.getType()).
Code
local periList = peripheral.getNames()

for i = 1, #periList do
	print("I have a "..peripheral.getType(periList[i]).." attached as \""..periList[i].."\".")
end
Output Depending on the peripherals available, along the lines of:

I have a modem attached as "left".
I have a monitor attached as "monitor_0".
I have a turtle attached as "top".

etc...