Jump to content




Storing multiple peripherals in a table

peripheral lua networking

14 replies to this topic

#1 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 07: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())


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 08: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")}


#3 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 08:08 AM

Argh, thanks. I intended to put it in the IF but it seems I mixed it up. I'll try the find.

#4 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 11:53 AM

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 by Avarion, 07 June 2015 - 11:59 AM.


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 12: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?

#6 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 12: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.

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 12: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().

#8 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 01:01 PM

View PostBomb Bloke, on 07 June 2015 - 12:57 PM, said:

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.

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 01: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.

#10 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 01: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))


#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 01: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


#12 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 01:52 PM

Does not change the result. Still attempt to call nil value

#13 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 01:58 PM

Did you mean getMaxEnergyStored, as in your earlier code snippet?

#14 Avarion

  • Members
  • 29 posts

Posted 07 June 2015 - 02:02 PM

Argh. Thanks. Sorry to waste your time with this :(

#15 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 June 2015 - 02:50 PM

Don't be.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users