Peripheral API

Functionperipheral.call
Calls a peripheral's function directly, at the specified side or network name.
Syntax peripheral.call(
  • side or network name : string
  • function name : string
  • function arguments... : any
)
Returns anything
Part of CC:Tweaked
API peripheral
See also peripheral.wrap
ExampleCheck if the modem on top of the computer is wireless.
Check if the modem on top of the computer is wireless.
Code
<nowiki>
peripheral.call("top", "isWireless")
</nowiki>
Output true
Functionperipheral.find
Find a peripheral on the network.
Syntax peripheral.find(
  • type : string
)
Returns table
API peripheral
ExampleFind a monitor.
Find a monitor on the wired network.
Code
<nowiki>local monitor = peripheral.find(monitor)</nowiki>
Output table
Functionperipheral.getMethods
Returns a table listing the names of all the methods (roughly, operations it is capable of doing) supported by a given peripheral. Any of the methods it lists can be called using the peripheral object returned by peripheral.wrap and peripheral.find, or using peripheral.call, for the given peripheral.
Syntax peripheral.getMethods(
  • peripheral : string
)
Returns table
Part of CC:Tweaked (source)
API peripheral
ExampleList methods of the peripheral on the left
This prints all the methods of whatever peripheral is on the left of the computer this is run on. You can replace "left" with another side or a peripheral's network name to list its methods instead
Code
<nowiki>
for _, method in pairs(peripheral.getMethods "left") do
    print(method)
end
    </nowiki>
Output Depends on the peripheral on the left. If there is none it will error. As an example, for a modem, the methods it lists will include transmit, isOpen, isWireless, open, close, and closeAll.
Functionperipheral.getNames
Returns the network names of all peripherals connected to the computer. You can pass these to peripheral.getType, peripheral.getMethods, peripheral.isPresent, and peripheral.wrap.
Syntax peripheral.getNames()
Returns table
Part of CC:Tweaked (source)
API peripheral
ExampleList peripherals and types
Prints the network name and type of all connected peripherals.
Code
<nowiki>
for _, name in pairs(peripheral.getNames()) do
    print(name, peripheral.getType(name))
end
    </nowiki>
Functionperipheral.getType
Returns the type of peripheral attached at the specified side or network name. Returns nil when no peripheral is attached at the specified side or network name.
Syntax peripheral.getType(
  • side or network name : string
)
Returns boolean result
Part of Lua (source)
API peripheral
ExampleGet peripheral type
Gets and prints the type of peripheral attached to the top side.
Code
print(peripheral.getType("top"))
Output If a wireless modem was attached, the output would be 'modem'.
Functionperipheral.isPresent
Returns if there's a peripheral attached at the specified side or network name.
Syntax peripheral.isPresent(
  • side or network name : string
)
Returns boolean result
Part of Lua (source)
API peripheral
ExampleCheck if there is a peripheral attached on top.
Checks and prints if there is a peripheral attached to the top side.
Code
print(peripheral.isPresent("top"))
Output true
Functionperipheral.wrap
Allows you to use a peripheral’s functions at the specified side or network name.
Syntax peripheral.wrap(
  • side or network name : string
)
Returns boolean result
Part of Lua (source)
API peripheral
ExampleOpen the modem right of the computer/turtle.
Open the modem right of the computer/turtle.
Code
<nowiki>
local modem = peripheral.wrap("modem_1234")
rednet.open(modem)
</nowiki>
Output true
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.