Jump to content




Getting Sides On Which Peripherals Are


  • You cannot reply to this topic
11 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

Posted 10 August 2013 - 09: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

#2 OmegaVest

  • Members
  • 436 posts

Posted 10 August 2013 - 09:30 PM

http://www.computerc...eripheral_(API)

sPeriph = peripheral.getType(side).
print(sPeriph)


#3 grand_mind1

  • Members
  • 207 posts

Posted 10 August 2013 - 09: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!

#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 10 August 2013 - 09: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.

#5 grand_mind1

  • Members
  • 207 posts

Posted 10 August 2013 - 10: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.

#6 CometWolf

  • Members
  • 1,283 posts

Posted 11 August 2013 - 05: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


#7 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 11 August 2013 - 07:15 AM

Use peripheral.getNames() if you want it to work with wired modems.

#8 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 11 August 2013 - 08:03 AM

View Postimmibis, on 11 August 2013 - 07:15 AM, said:

Use peripheral.getNames() if you want it to work with wired modems.

I believe wired modems use peripheral.getNamesRemote(), though I could be wrong :)

#9 CometWolf

  • Members
  • 1,283 posts

Posted 11 August 2013 - 08: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.

#10 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 11 August 2013 - 08:58 AM

View PostBubba, on 11 August 2013 - 08:03 AM, said:

View Postimmibis, on 11 August 2013 - 07:15 AM, said:

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.

View PostCometWolf, on 11 August 2013 - 08:23 AM, said:

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


#11 ElvishJerricco

  • Members
  • 803 posts

Posted 11 August 2013 - 09:08 AM

View PostCometWolf, on 11 August 2013 - 08:23 AM, said:

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.

#12 CometWolf

  • Members
  • 1,283 posts

Posted 11 August 2013 - 09: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users