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

peripheral.getType trouble

Started by gril002, 18 May 2016 - 04:01 PM
gril002 #1
Posted 18 May 2016 - 06: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
KingofGamesYami #2
Posted 18 May 2016 - 06: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 on 18 May 2016 - 06:34 PM
gril002 #3
Posted 18 May 2016 - 06: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
gril002 #4
Posted 18 May 2016 - 06:45 PM
oh and i had to ad pairs(rs.getSides) to get it to work
KingofGamesYami #5
Posted 18 May 2016 - 08:39 PM
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).
gril002 #6
Posted 18 May 2016 - 08:44 PM
I will look into it later since I am only using Wireless for now. But thanks for the tip