Using Peripherals from Other Mods

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

Previous topic - Next topic

KingofGamesYami

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
  1. local methods = peripheral.getMethods( "top" ) -- note: for the purpose of this tutorial, I will always assume the peripheral
  2. -- is on the top. If your peripheral is placed elsewhere, you may have to change
  3. -- some of the arguments
  4. for k, v in pairs( methods ) do -- iterate through the methods available to the peripheral
  5.   print( v ) -- print them to the screen
  6. end
If this does not work, I would save them to a file which you can then peruse using the 'edit' program.
Code: Lua
  1. local methods = peripheral.getMethods( "top" )
  2. local file = fs.open( "output", "w" ) -- opens a file named 'output' in write mode
  3. for k, v in pairs( methods ) do
  4.   file.writeLine( v ) -- writes the method to a new line
  5. end
  6. file.close() -- closes the file (important!)
After running this program, you should use the built-in edit program on the file 'output',
>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
  1. -- once again, note that I assume the peripheral is on top of the computer
  2. local somePeripheral = peripheral.wrap( 'top' )
  3. function getInfo( ... )
  4.   local tValues = {...}
  5.   for k, v in pairs( tValues ) do
  6.     print( k .. type( v ) )
  7.   end
  8. end
  9. 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.