Using Peripherals from Other Mods

Started by KingofGamesYami, Aug 13, 2018, 01:28 am

Previous topic - Next topic

KingofGamesYami

Aug 13, 2018, 01:28 am Last Edit: Aug 13, 2018, 01:48 am by KingofGamesYami Reason: Add spaces
Reposted from here

This tutorial is not for using peripherals provided by computercraft, such as the printer, monitor, etc. This tutorial will not specifically target any particular peripheral. Instead, this tutorial will teach you how to identify and use any peripheral.
I will assume the readers of this tutorial know the basics of attaching a peripheral to a computer. Nevertheless, I will cover it briefly here

If the peripheral is physically in contact with the computer, it is considered 'attached'
If the peripheral is connected via networking cables, make sure the wired modem connected to the peripheral is red (note: the wired modem connected to the computer using the peripheral does not necessarily need to be turned on)

When encountering an unknown peripheral, the first thing to do is to list the methods available. Sometimes these can be displayed on screen, other times there are too many.
I would first try to show the methods on screen,
Code (lua) Select
local methods = peripheral.getMethods( "top" ) -- note: for the purpose of this tutorial, I will always assume the peripheral
-- is on the top. If your peripheral is placed elsewhere, you may have to change
-- some of the arguments
for k, v in pairs( methods ) do -- iterate through the methods available to the peripheral
  print( v ) -- print them to the screen
end
If this does not work, I would save them to a file which you can then peruse using the 'edit' program.
Code (lua) Select
local methods = peripheral.getMethods( "top" )
local file = fs.open( "output", "w" ) -- opens a file named 'output' in write mode
for k, v in pairs( methods ) do
  file.writeLine( v ) -- writes the method to a new line
end
file.close() -- closes the file (important!)
After running this program, you should use the built-in edit program on the file 'output',
Code Select
>edit output
Once you have a list of the methods available, there are a few options. If one of the functions sounds as if it might do what you want the peripheral to do, you should try calling it. If it throws an error, it probably requires some sort of argument. For many methods available through Open Peripheral, you can try supplying "unknown". At this point, if you are still stuck, you should consult the online or in-game documentation. OpenPeripheral documentation is in game, however I quite like this program.
As you are testing the methods, if they have return values, there are a few ways to handle it. This program will handle almost anything, but might not be so pretty:
Code (lua) Select
-- once again, note that I assume the peripheral is on top of the computer
local somePeripheral = peripheral.wrap( 'top' )
function getInfo( ... )
  local tValues = {...}
  for k, v in pairs( tValues ) do
    print( k .. type( v ) )
  end
end
getInfo( somePeripheral.someMethod() )
If you're still frustrated after trying all the things in this tutorial, Ask A Pro is the best place to go. Please do not ask peripheral-specific questions in this thread, although questions about the tutorial are acceptable.
When posting in AAP, please remember to post the peripheral's mod's version, the Minecraft version, and the CC version. It is also helpful to post if the mod provides the methods directly, or if a third party (such as OpenPeripheral) does. If you aren't sure, post your full mod list.
I'm a ComputerCraft veteran with over 3k posts on the old ComputerCraft Forum.  I'm mostly inactive in CC having moved on to bigger, more interesting projects but still contribute to the community.