Jump to content




get rf capacity from cells in table nil

lua

3 replies to this topic

#1 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

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 by VSXGsonic, 28 March 2020 - 04:21 AM.


#2 Luca_S

  • Members
  • 407 posts
  • LocationGermany

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))


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#4 VSXGsonic

  • Members
  • 19 posts
  • LocationIn a hole :|

Posted 28 March 2020 - 06:11 PM

View PostLyqyd, on 28 March 2020 - 05:25 PM, said:

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 by VSXGsonic, 28 March 2020 - 06:11 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users