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

get rf capacity from cells in table nil

Started by VSXGsonic, 28 March 2020 - 03:15 AM
VSXGsonic #1
Posted 28 March 2020 - 04:15 AM
I'm trying to wrap TE energy cells using wired modems without typing all 50 or so cells
i thought it might work but it errors. but i know if i print them it shows the right cells and i have no clue whats wrong but its first time using the getNames like this.


temp:11: attempt to call field 'getRFCapacity'(a nil value)


local energyC = 0
local cells = {}

for i, name in pairs(peripheral.getNames()) do
  if peripheral.getType(name) == "thermalexpansion:storage_cell" then
    table.insert(cells, name)
  end
end 

for i,v in ipairs(cells) do
energyC = energyC + v.getRFCapacity()
end
Edited on 28 March 2020 - 03:21 AM
Luca_S #2
Posted 28 March 2020 - 08:55 AM
getNames() returns a table of strings containing the names of the peripherals. So your cells table will look something like this:


{
  "thermalexpansion:storage_cell_1",
  "thermalexpansion:storage_cell_2",
  "thermalexpansion:storage_cell_3",
  "thermalexpansion:storage_cell_4",
  "thermalexpansion:storage_cell_5",
}

So to call the getRFCapacity method, you'll need to wrap the peripherals first. Instead of doing
table.insert(cells, name)
you can just do
table.insert(cells, peripheral.wrap(name))
Lyqyd #3
Posted 28 March 2020 - 05:25 PM
If you have a table of names, you can use peripheral.call:


for i, v in ipairs(cells) do
    energyC = energyC + peripheral.call(v, "getRFCapacity")
end
VSXGsonic #4
Posted 28 March 2020 - 06:11 PM
If you have a table of names, you can use peripheral.call:


for i, v in ipairs(cells) do
	energyC = energyC + peripheral.call(v, "getRFCapacity")
end

is there any advantage to .call ? I haven't used it before as I have always used .wrap
Edited on 28 March 2020 - 05:11 PM