Jump to content




peripheral.getType trouble


5 replies to this topic

#1 gril002

  • Members
  • 38 posts

Posted 18 May 2016 - 05:01 PM

So for some reason when runing the below code, I get the error attempt to call nil at the peripheral.getType( side). I have CC 1.7 and the needed is 1.6 so that shouldnt be a problem, and I copy pasted the function from the wiki so it shouldnt be misspeleled either.


function getPeripherals()
--modem = peripheral.find("modem")
--drive = peripheral.find("drive")
sides = {"top", "bottom", "left", "right", "back"}
for i = 1,5 do
if i == 1 then
side = "top"
elseif i == 2 then
side = "bottom"
elseif i == 3 then
side = "left"
elseif i == 4 then
side = "right"
elseif i == 5 then
side = "back"
end
peripheral = peripheral.getType(side) --Problem lies here...
if peripheral == "drive" then
drive = side elseif
peripheral == "modem" then
modem = side
end
end
end

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 May 2016 - 05:15 PM

I doubt you got attempt to call nil on peripheral.getType. I would expect that to throw attempt to index ? (a string value) or attempt to index ? (a nil value) instead, since you are replacing the peripheral API with the peripheral on the left.
Also, you code can be shortened considerably:
for i, side in pairs( rs.getSides() ) do --#get a list of sides, iterate through it
local pType = peripheral.getType( side ) --#get the type
if pType == "drive" then --#if it's drive, then say drive is the side (eg drive = "right")
drive = side
elseif pType == "modem" then --#if it's a modem then say modem is the side (eg modem = "right")
modem = side
end
end

Edited by KingofGamesYami, 18 May 2016 - 07:34 PM.


#3 gril002

  • Members
  • 38 posts

Posted 18 May 2016 - 05:35 PM

So I ran the function as a stand alone program and it functioned. So I tried using local dor preipheral and then it worked. I am not sure why it helped since I don't use the name anywhere else but thanks for the help and telling me about the rs.getSides() thing

#4 gril002

  • Members
  • 38 posts

Posted 18 May 2016 - 05:45 PM

oh and i had to ad pairs(rs.getSides) to get it to work

#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 May 2016 - 07:39 PM

View Postgril002, on 18 May 2016 - 05:45 PM, said:

oh and i had to ad pairs(rs.getSides) to get it to work

My bad, edited. If you're interested, you might look at using peripheral.getNames (which includes the peripherals connected by wired modem).

#6 gril002

  • Members
  • 38 posts

Posted 18 May 2016 - 07:44 PM

I will look into it later since I am only using Wireless for now. But thanks for the tip





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users