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

Storing multiple peripherals in a table

Started by Avarion, 07 June 2015 - 06:48 AM
Avarion #1
Posted 07 June 2015 - 08:48 AM
Hi,

I'm trying to read the energy status of multiple energy cells. I'm trying to make it adaptive so I just have to add more cells and add them to the network and the program recognizes them. Therefore I thought I use function which uses a loop to check for all the cells and add them to a table. Then returns the table. Unfortunately it seems the table is empty when I try to access it from the main program.


function prepare()
cell = {}
local peripheralList = peripheral.getNames()
counter = 0
for Index = 1, #peripheralList do
  counter = counter + 1
  if string.find(peripheralList[Index], "tile_thermalexpansion_cell") then
   cell[counter]=peripheral.wrap(peripheralList[Index])
  end
end
return(cell)
end
function getEnergy()
end

cell = prepare()
print(cell[1].getType())
Bomb Bloke #2
Posted 07 June 2015 - 09:05 AM
You're adding 1 to counter with every iteration of your "for" loop, regardless as to whether you're adding a peripheral to your cell table. That means that if the first peripheral in the list is empty, cell[1] will be nil, and if the second is a cell, cell[2] will point to that, and so on. That is to say, you end up with gaps in your final table.

It would be easier to use peripheral.find(), which does what you're trying to do:

cell = {peripheral.find("tile_thermalexpansion_cell")}
Avarion #3
Posted 07 June 2015 - 09:08 AM
Argh, thanks. I intended to put it in the IF but it seems I mixed it up. I'll try the find.
Avarion #4
Posted 07 June 2015 - 12:53 PM
I've checked find and I've tried to fix my old code. Both result in the same problem. I do not get the data.


function prepare()
cell = {}
local peripheralList = peripheral.getNames()
counter = 0
for Index = 1, #peripheralList do
  if string.find(peripheralList[Index], "tile_thermalexpansion_cell") then
	counter = counter + 1
	cell[counter]=peripheral.wrap(peripheralList[Index])
   end
end
return(cell)
end

cell = prepare()

print(cell[1][1])

cell = {peripheral.find("tile_thermalexpansion_cell")}
print(cell[1][1])

The table seems always to be empty.

If I wrap a single energy cell I get data.

Edit: Oh, if I use just cell[1] I just get the table ID, thus I've used [1][1]. There might be the problem if I haven't understood how table work.
Edited on 07 June 2015 - 10:59 AM
Bomb Bloke #5
Posted 07 June 2015 - 01:06 PM
Well, let's assume wrapped energy cells have a "getStoredRF" function (no idea if they do, but just for example). A "wrapped peripheral" is basically just a table filled with the functions available to that peripheral. So if you wrapped one, you'd call such a function by doing:

cell = peripheral.wrap("whateverSide")
print(cell.getStoredRF())

But if you wrap a bunch of the things, and stick them all into a table (as sub-tables), you might do:

cell = prepare()

for i = 1, #cell do
  print(cell[i].getStoredRF())
end

… or:

cell = {peripheral.find("tile_thermalexpansion_cell")}

for i = 1, #cell do
  print(cell[i].getStoredRF())
end

Making any sense?
Avarion #6
Posted 07 June 2015 - 01:52 PM
Still it refuses to comply.

x = peripheral.wrap("tile_thermalexpansion_cell_resonant_name_3")

print("Energy: "..x.getEnergyStored("unknown"))
print("Max Energy: "..x.getMaxEnergyStored("unknown"))

This prints:


Energy: 78236235
Max Energy: 80000000

And this prints nothing:

cell = {peripheral.find("tile_thermalexpansion_cell")}

for i = 1, #cell do
  print(cell[i].getEnergyStored())
end

Even when I add the "unknown" to the method.
Bomb Bloke #7
Posted 07 June 2015 - 01:57 PM
The type being passed to peripheral.find() may be incorrect. Probably too short, by the looks of things - try "tile_thermalexpansion_cell_resonant_name", and if that doesn't work, figure out the exact type with peripheral.getType(). Eg, if you have a cell behind your computer, you'd do:

print(peripheral.getType("back"))

Whatever that prints is what you've got to use with peripheral.find().
Avarion #8
Posted 07 June 2015 - 02:01 PM
The type being passed to peripheral.find() may be incorrect. Probably too short, by the looks of things - try "tile_thermalexpansion_cell_resonant_name"

Thanks. That was the solution. I thought find (and my variant) look up a part of the string and wrap when the part is found in the peripherals name.
Bomb Bloke #9
Posted 07 June 2015 - 02:10 PM
peripheral.find() looks for exact matches of the specified peripheral type. Your one does partial matches, but checks against peripheral names, which might be something different again!

But if you did:

if string.find(peripheral.getType(peripheralList[Index]), "tile_thermalexpansion_cell") then

… I reckon that'd do the job.
Avarion #10
Posted 07 June 2015 - 02:27 PM
Thanks. One last question. Though its not part of the original I'm not sure if I need to open a new thread. I now try to put the work in functions but it seems I do not know how to submit the table to the function. The content seems to get lost because I always get the message: "attempt to call nil"


function getMaxEnergy(cell)
local count = 0
local i = 0
for i in pairs(cell) do
  count = count + cell[i].getMaxEnergy()
end
print(i)
return(count)
end
cell = {peripheral.find("tile_thermalexpansion_cell_resonant_name")}

print(getMaxEnergy(cell))
Bomb Bloke #11
Posted 07 June 2015 - 02:44 PM
Either do this:

function getMaxEnergy(cell)
local count = 0
for i = 1, #cell do
  count = count + cell[i].getMaxEnergy()
end
return(count)
end

… or this:


function getMaxEnergy(cell)
local count = 0
for _, i in pairs(cell) do
  count = count + i.getMaxEnergy()
end
return(count)
end
Avarion #12
Posted 07 June 2015 - 02:52 PM
Does not change the result. Still attempt to call nil value
Bomb Bloke #13
Posted 07 June 2015 - 02:58 PM
Did you mean getMaxEnergyStored, as in your earlier code snippet?
Avarion #14
Posted 07 June 2015 - 03:02 PM
Argh. Thanks. Sorry to waste your time with this :(/>
Bomb Bloke #15
Posted 07 June 2015 - 03:50 PM
Don't be.