This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sora Firestorm's profile picture

Aap Renewal Project Tutorial Series - Modem Intermediate : Remote Peripherals

Started by Sora Firestorm, 26 August 2013 - 12:06 PM
Sora Firestorm #1
Posted 26 August 2013 - 02:06 PM
Prerequisites :
Peripheral Basics - isPresent, getType, getMethods, wrap, call (Will be linked when finished)
Modem Basics : Wired Modems (Will be linked when finished)

Hello, welcome to the AaP Renewal Project 'Modem Intermediate : Remote Peripherals' tutorial.

In order to begin using peripherals remotely, you're going to have to place a wired modem on both your computer and peripheral, and wire them up with networking cable, like in this picture :
Spoiler

Now you need to turn the modem attached to the peripheral on. Right click on it and a message will appear in the chat, and the grey ring will become red :
Spoiler

Once you've gotten the network setup, you can work on establishing the connection in software. Go into your computer and fire up the lua prompt.
First, you need to interface with your wired modem. I'd suggest using peripheral.wrap(), but peripheral.call() works too.

local modem = peripheral.wrap("right")
-- If you modem is on the right, that is

Now we can use the Modem API to interact with our device. In order to figure out what devices are connected, we'll use modem.getNamesRemote().
This ensures that only peripherals that are connected to our modem on the right side will show up as candidates for peripheral operations.
If you want any peripheral attached to the computer in some way (for example, a peripheral connected directly to the computer, or through a different modem), use peripheral.getNames().
Both peripheral.getNames() and modem.getNamesRemote() return a table, which is numerically indexed with the names as values to those keys.
If we iterate through this table, we'll get the names of the peripherals. The table would look something like the commented table, if it were declared manually :

local peripherals = modem.getNamesRemote()
--[[
This table will be different sizes depending on both the size of the network
(how many peripherals) and how you are narrowing your peripheral options

local peripherals = {
  [1] = "drive_0",
}
]]

Ensure that your peripheral is the type you want, by using peripheral.getType().

for k, v in pairs(peripherals) do
  -- We want a floppy drive, which is type 'drive'
  if peripheral.getType(v) == "drive" then
	toWrap = v
  end
end

If it's what you want, you can use the name of the peripheral in call() or wrap() calls, in place of the directions like "front" or "top"

local floppy = peripheral.wrap(toWrap)

if not floppy then
  error("No floppy drives attached")
end

-- Now we interface with the drive, as normal
if floppy.isDiskPresent() then
  -- Floppy manipulation here
else
  write("No disk in drive :(/> ")
end

-- Rest of code

That's it for the tutorial. Now you're able to use peripherals across the network. Think of the possibilities!

Thanks to Lyqyd, TheOriginalBIT, and others who has commented and made suggestions on how to make this tutorial better.
theoriginalbit #2
Posted 26 August 2013 - 02:49 PM
I'm surprised that you didn't at least test your code before publishing this tutorial, you have bugs… Just a bug report and some notes I have for you…

BUGS:
Spoiler

for k, v in pairs(peripherals) do
You haven't defined peripherals, I assume you were meaning peripheral.getNames()

if peripheral.getType(v) == "drive" then
  floppy = peripheral.wrap(v)
  return
end
You're returning after wrapping the peripheral, the code will end here

NOTES:
Spoiler
In order to begin using peripherals remotely, you're going to have to place a wired modem on both your computer and peripheral,
wire them up with networking cable, and turn the modems on.
It should be noted that only the peripheral's modem needs to be turned on, the modem on the computer is not required to be on.
In order to figure out what devices are connected, we'll use peripheral.getNames().
There is much more than this function, I suggest that you cover the others as well so this is more of an extensive tutorial.
Lyqyd #3
Posted 26 August 2013 - 02:58 PM
This is a good start, but it does require some expansion. As this is a modem series tutorial, where are the modem functions? You use peripheral.getNames, but don't talk about modem.getNamesRemote? This tutorial would also benefit from a screenshot showing the required setup in-game. The screenshot should clearly show the modem on a remote peripheral in the active state, and the modem on the computer in the inactive state, which is best practice unless the computer's peripheral methods have need to be exposed to the network. The code example is also rather monolithic, and should be broken up into chunks and explained more thoroughly, even if only in brief explanations referencing other tutorials.
Engineer #4
Posted 26 August 2013 - 02:59 PM
theoriginalbit said:

for k, v in pairs(peripherals) do
You haven't defined peripherals, I assume you were meaning peripheral.getNames()
Actually, he defined that table. Because he explained peripheral.getNames and wrote down the full table you would possibly get and named it peripherals. But I do agree with you that it is better to put the full code as you can copy and paste it yourself.
theoriginalbit #5
Posted 26 August 2013 - 03:03 PM
Actually, he defined that table. Because he explained peripheral.getNames and wrote down the full table you would possibly get and named it peripherals. But I do agree with you that it is better to put the full code as you can copy and paste it yourself.
I know that there was a defined table, but it was defined to show how the getNames function would return a table, it is not present in that large clearly separate code example… Also this was stated:
In order to figure out what devices are connected, we'll use peripheral.getNames().
Notice the "we'll use"…. well it's not used!
Engineer #6
Posted 26 August 2013 - 03:09 PM
Snip
Touché. But didn't I state I do agree with you?:P/>
Sora Firestorm #7
Posted 26 August 2013 - 09:21 PM
I'm sorry if I sound really stupid when I say this, but peripheral.getNamesRemote()? I iterated through the peripheral table and did not see that. Plus, peripheral.getNames() also listed remote peripherals when I tried it. The behavior hasn't changed for 1.6.2, has it? (I'm using FTB Unleashed, CC 1.55)

Any rate, thanks for the bug fixes, BIT. I stripped out that return, and modified the block with the peripheral table to be slightly clearer, I hope. I'm working on the pictures right now. Honestly, I did have pictures in the original draft, but I figured 'the modem basics tutorial will have all of the pretty pictures for network setup, I don't need them'. Proved me wrong. :P/>

EDIT : Modified tutorial, broke up the code, added pictures, other things suggested…

I need to watch what I write and ensure it's complete, otherwise you guys won't let me write tutorials anymore :P/>
theoriginalbit #8
Posted 26 August 2013 - 10:16 PM
I'm sorry if I sound really stupid when I say this, but peripheral.getNamesRemote()?
It should be more accurately typed as <modem>.getNamesRemote() as it is on the modem peripheral itself.

Plus, peripheral.getNames() also listed remote peripherals when I tried it. The behavior hasn't changed for 1.6.2, has it? (I'm using FTB Unleashed, CC 1.55)
Ok firstly using getNamesRemote allows for some extra network separation, as you could have say the network on the left one that the computer uses for it's own purpose, and the network on the right that it is a part of a separate network with peripherals… using getNamesRemote allows you to get the names of the peripherals on that specific modem network, thus there can be no cross-over as there would be with getNames…
And secondly, a tutorial should be designed to provide all information, unbiased, on a topic, not just information that the writer thinks is appropriate. It should show how something can be done and optionally provide pro's and con's to the options.
Sora Firestorm #9
Posted 26 August 2013 - 10:29 PM
Well, no wonder. That was stupid of me. Thanks for the information. I'd really ought not to write tutorials on things I don't have 100% grasp on. Going to change it…

EDIT : And done.