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

Peripheral issue

Started by makeme, 05 March 2014 - 06:01 PM
makeme #1
Posted 05 March 2014 - 07:01 PM
Will this work to wrap peripherals?
I'm getting "peripheral :20: expected string"


local PeriList = peripheral.getNames()
local TankList = {}
local OtherPeriList = {}
local MonList = {}
local o = 1
local u = 1
local m = 1
for i = 1, #PeriList do
if PeriList == "rcirontankvalvetile" then
TankList = PeriList
u = u + 1
elseif PeriList == "monitor" then
MonList[m] = PeriList
m = m + 1
else
OtherPeriList[o] = PeriList
o = o + 1
end
end
local Tank1 = peripheral.wrap(TankList[1])
local Tank2 = peripheral.wrap(TankList[2])
local Tank3 = peripheral.wrap(TankList[3])
local Tank4 = peripheral.wrap(TankList[4])
local Mon = peripheral.wrap(MonList[1])

Edited on 05 March 2014 - 06:05 PM
CometWolf #2
Posted 05 March 2014 - 07:10 PM
Quick tip, when using "#" on an empty table, it returns 0, so you can use this and a plus 1 to add to the table automatically as it get's bigger, instead of having a specific variable for that.

for i = 1, #PeriList do
  if PeriList[i] == "rcirontankvalvetile" then
	TankList[#TankList+1] = PeriList[i]
  elseif PeriList[i] == "monitor" then
	MonList[#MonList+1] = PeriList[i]
  else
	OtherPeriList[#OtherPeriList+1] = PeriList[i]
  end
end
Also, you could just wrap the peripheral to an index in your table directly, instead of storing the name in a table, then wrapping that name to a variable.

Now as for the actual issue, since you haven't specified how this is all connected im going to assume it's through a wired modem. This means the names won't be exactly rcirontankvalvetile, which it would need to be for your if condition to be fulfilled. Chances are it's something like 0_rcirontankvalvetile, depending on how many you have connected. To deal with this, we can use string.match() instead of the double equal sign.

if string.match(PeriList[i],"rcirontankvalvetile") then
Basically what this does is, if the first string passed contains the second string passed, it will return the second string. There is a LOT more you can do with this funciton, but it's not nessacary to deal with your issue.
Edited on 05 March 2014 - 06:18 PM
makeme #3
Posted 05 March 2014 - 07:17 PM
Quick tip, when using "#" on an empty table, it returns 0, so you can use this and a plus 1 to add to the table automatically as it get's bigger, instead of having a specific variable for that.

for i = 1, #PeriList do
  if PeriList[i] == "rcirontankvalvetile" then
	TankList[#TankList+1] = PeriList[i]
  elseif PeriList[i] == "monitor" then
	MonList[#MonList+1] = PeriList[i]
  else
	OtherPeriList[#OtherPeriList+1] = PeriList[i]
  end
end

Now as for the actual issue, since you haven't specified how this is all connected im going to assume it's through a wired modem. This means the names won't be exactly rcirontankvalvetile, which it would need to be for your if condition to be fulfilled. Chances are it's something like 0_rcirontankvalvetile, depending on how many you have connected. To deal with this, we can use string.match() instead of the double equal sign.

if string.match(PeriList[i],"rcirontankvalvetile") then
Basically what this does is, if the first string passed contains the second string passed, it will return the second string. There is a LOT more you can do with this funciton, but it's not nessacary to deal with your issue.


Thanks a bunch I swear you have written more of this program than me :P/>
makeme #4
Posted 05 March 2014 - 07:29 PM
Another Quick issue

This code skips to the else how would I test if a tank is empty?

if not tank1["name"] == nul then
steAmPI.FluPrinter(tank1)
else
Mon.setCursorPos(X1Pos, Y1Pos)
Mon.setTextColor(128)
Mon.write(" Empty ")
Mon.setCursorPos(X1Pos, Y1BarPos)
Mon.setBackgroundColor(128)
Mon.write(" ")
Mon.setBackgroundColor(32768)
end
CometWolf #5
Posted 05 March 2014 - 07:41 PM

if not tank1["name"] == nul then
Im going to assume that "nul" here is supposed to be "nil", which means you could just write this instead

if tank1["name"] then
This will equate to true aslong as the variable tank1.name is not nil or false.

Anyways, isn't there a function you have to call on the tank to actually get it's liquid contents? Assuming you didn't already do this in a piece of the code that isn't posted ofcourse.
Edited on 05 March 2014 - 06:45 PM
makeme #6
Posted 05 March 2014 - 08:08 PM

if not tank1["name"] == nul then
Im going to assume that "nul" here is supposed to be "nil", which means you could just write this instead

if tank1["name"] then
This will equate to true aslong as the variable tank1.name is not nil or false.

Anyways, isn't there a function you have to call on the tank to actually get it's liquid contents? Assuming you didn't already do this in a piece of the code that isn't posted ofcourse.


yeah tank1 is a table
CometWolf #7
Posted 05 March 2014 - 08:14 PM
So… you're wrapping the tank functions to the variable tank1

local Tank1 = peripheral.wrap(TankList[1])
Then overwriting the tank functions with the tank info? Sounds like a bad idea… I mean it works, but you'll have to get the tank functions again to refresh it.

tank1 = tank1.getTankInfo()

would be better to just skip the wrap altogether.

tank1 = peripheral.call(TankList[1],"getTankInfo","unkown")
Edited on 05 March 2014 - 07:17 PM
makeme #8
Posted 05 March 2014 - 08:35 PM
So… you're wrapping the tank functions to the variable tank1

local Tank1 = peripheral.wrap(TankList[1])
Then overwriting the tank functions with the tank info? Sounds like a bad idea… I mean it works, but you'll have to get the tank functions again to refresh it.

tank1 = tank1.getTankInfo()

would be better to just skip the wrap altogether.

tank1 = peripheral.call(TankList[1],"getTankInfo","unkown")

I am planing to rewrite the whole getting tank info bit soonish once I get everything else (nearly) working