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

Getting Sides On Which Peripherals Are

Started by grand_mind1, 10 August 2013 - 08:26 PM
grand_mind1 #1
Posted 10 August 2013 - 10:26 PM
I want to make a simple program that checks each side of the computer and ,if there is a peripheral on it, say what that peripheral is. I know that I would probably have to use something like this:

sides = {"left","right","back","front","top","bottom"}
for i,o in pairs(sides) do
end
If this is the correct way to do this then I still need help.
I don't know a lot about this kind of loop and how it works. If someone could try to help me understand how to use this loop in the way that I want that would be awesome.
Help is appreciated!
Thanks! :D/>
OmegaVest #2
Posted 10 August 2013 - 10:30 PM
http://www.computerc...eripheral_(API)


sPeriph = peripheral.getType(side).
print(sPeriph)
grand_mind1 #3
Posted 10 August 2013 - 10:38 PM
Um, I don't really understand. How would I get the variable "side" to change in a loop or something?
Never mind. Thanks for the help!
Bubba #4
Posted 10 August 2013 - 10:42 PM
You can do it quite simply by using peripheral.getNames, IIRC. This will return a table of sides that have peripherals attached to them.
grand_mind1 #5
Posted 10 August 2013 - 11:07 PM
Ok, so I tried to use this code in a program to decide which monitor, if any, to use:

sides = {"left","right","back","front","top","bottom"}
for i,side in pairs(sides) do
  sPeriph = peripheral.getType(side)
  if sPeriph == "monitor" then
    use = "monitor"
    break  
  end
end
But I get the error:

peripheral: 20: Expected string
I guess this means that I've done something wrong with using the peripheral API. The only place where I think this could've happened is when I give it the variable "side" but that really doesn't make any sense because side should be a string.
CometWolf #6
Posted 11 August 2013 - 06:37 AM
idk about using pairs for this, but the way i would do it with a regular for loop would be more akin to this

tSides = rs.getSides() -- redstone function returns a table containing sides
for i=1,#tSides do
  if peripheral.getType(tSides[i]) == "monitor" then
	use = "monitor"  --maybe wrap the monitor here right away?
	break
  end
end
immibis #7
Posted 11 August 2013 - 08:15 AM
Use peripheral.getNames() if you want it to work with wired modems.
Bubba #8
Posted 11 August 2013 - 09:03 AM
Use peripheral.getNames() if you want it to work with wired modems.

I believe wired modems use peripheral.getNamesRemote(), though I could be wrong :)/>
CometWolf #9
Posted 11 August 2013 - 09:23 AM
When you're using wired modems you would also have to change up the if statment a little bit

if string.match(peripheral.getType(tSides[i]),"monitor") then
This is because when using a wired modem each connected peripheral gets assigned an id(id_peripheralType). Usually this id is 0, unless you have multiple of the same peirpheral conneted.
MysticT #10
Posted 11 August 2013 - 09:58 AM
Use peripheral.getNames() if you want it to work with wired modems.

I believe wired modems use peripheral.getNamesRemote(), though I could be wrong :)/>
That's the modem method called internally. The peripheral api has the getNames function which checks all sides for peripherals, and if one is a wired modem it calls getNamesRemote to get attached peripherals.

When you're using wired modems you would also have to change up the if statment a little bit

if string.match(peripheral.getType(tSides[i]),"monitor") then
This is because when using a wired modem each connected peripheral gets assigned an id(id_peripheralType). Usually this id is 0, unless you have multiple of the same peirpheral conneted.
That's for the name/side of the peripheral, not for the type. So no need for any extra checks.

OP: This should work for you:

for _,name in ipairs(peripheral.getNames()) do
  --# tell the user there's a peripheral
  print("There's a ", peripheral.getType(name), " on ", name)
  --# if you need to use a certain peripheral, like a monitor:
  if peripheral.getType(name) == "monitor" then
	--# here you can store the monitor side
	monitorSide = name
	--# or directly wrap the monitor
	monitor = peripheral.wrap(name)
	--# or whatever you need to do
	break
  end
end
ElvishJerricco #11
Posted 11 August 2013 - 10:08 AM
When you're using wired modems you would also have to change up the if statment a little bit

if string.match(peripheral.getType(tSides[i]),"monitor") then
This is because when using a wired modem each connected peripheral gets assigned an id(id_peripheralType). Usually this id is 0, unless you have multiple of the same peirpheral conneted.

It should be peripheralType_id, with id at the end. So if you're going to be doing patters, it's probably better to use this:


if string.match(peripheral.getType(tSides[i]),"^" .. sPeripheralType .. "_%d+") then

The ^ makes sure sPeripheralType is found at the beginning of the string, and _%d+ makes sure there's an underscore, followed by one or more digits.
CometWolf #12
Posted 11 August 2013 - 10:25 AM
You are indeed correct on the order of the string, but it will match the peripheralType regardless of where it is in the string, if you just use string.match(string,peripheralType). No need for the extra mumbo jumbo, unless you're interested in the id aswell. It's not like there'a peripheral with a name of just digits anyway.