peripheral
The Peripheral API is for interacting with peripherals connected to the computer, such as the Disk Drive, the Advanced Monitor and Monitor.
Each peripheral block has a name, either referring to the side the peripheral can be found on, or a name on an adjacent wired network.
If the peripheral is next to the computer, its side is either front
,
back
, left
, right
, top
or bottom
. If the peripheral is attached by
a cable, its side will follow the format type_id
, for example printer_0
.
Peripheral functions are called methods, a term borrowed from Java.
getNames() | Provides a list of all peripherals available. |
---|---|
isPresent(name) | Determines if a peripheral is present with the given name. |
getType(peripheral) | Get the type of a wrapped peripheral, or a peripheral with the given name. |
getMethods(name) | Get all available methods for the peripheral with the given name. |
getName(peripheral) | Get the name of a peripheral wrapped with peripheral.wrap. |
call(name, method, ...) | Call a method on the peripheral with the given name. |
wrap(name) | Get a table containing functions pointing to the peripheral's methods, which can then be called as if using peripheral.call. |
find(ty [, filter]) | Find all peripherals of a specific type, and return the wrapped peripherals. |
- getNames()Source
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.
Returns
- { string... } A list of the names of all attached peripherals.
- isPresent(name)Source
Determines if a peripheral is present with the given name.
Parameters
- name string The side or network name that you want to check.
Returns
- boolean If a peripheral is present with the given name.
Usage
peripheral.isPresent("top")
peripheral.isPresent("monitor_0")
- getType(peripheral)Source
Get the type of a wrapped peripheral, or a peripheral with the given name.
Parameters
Returns
- string | nil The peripheral's type, or
nil
if it is not present.
- string | nil The peripheral's type, or
- getMethods(name)Source
Get all available methods for the peripheral with the given name.
Parameters
- name string The name of the peripheral to find.
Returns
- { string... } | nil A list of methods provided by this peripheral, or
nil
if it is not present.
- getName(peripheral)Source
Get the name of a peripheral wrapped with peripheral.wrap.
Parameters
- peripheral table The peripheral to get the name of.
Returns
- string The name of the given peripheral.
- call(name, method, ...)Source
Call a method on the peripheral with the given name.
Parameters
- name string The name of the peripheral to invoke the method on.
- method string The name of the method
- ... Additional arguments to pass to the method
Returns
- The return values of the peripheral method.
Usage
Open the modem on the top of this computer.
peripheral.call("top", "open", 1)
- wrap(name)Source
Get a table containing functions pointing to the peripheral's methods, which can then be called as if using peripheral.call.
Parameters
- name string The name of the peripheral to wrap.
Returns
- table | nil The table containing the peripheral's methods, or
nil
if there is no peripheral present with the given name.
Usage
peripheral.wrap("top").open(1)
- find(ty [, filter])Source
Find all peripherals of a specific type, and return the wrapped peripherals.
Parameters
- ty string The type of peripheral to look for.
- filter? function(name: string, wrapped: table):boolean A filter function, which takes the peripheral's name and wrapped table and returns if it should be included in the result.
Returns
- table... 0 or more wrapped peripherals matching the given filters.
Usage
{ peripheral.find("monitor") }
peripheral.find("modem", rednet.open)