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

Simple program, erroring at line 27. Peripheral wrapping?

Started by Robinlemon, 01 December 2015 - 08:17 PM
Robinlemon #1
Posted 01 December 2015 - 09:17 PM
EDIT: Now using peripheral.call rather than wrapping the variable.

THIS TOPIC CAN BE CLOSED.

Should get all connected energy cells, then get their current energy and add it to the variable to get the max energy. I'm wrapping the peripheral correctly although it still gives an error at line 27: attempt to index a nil value, aka it hasn't been wrapped.


periph = peripheral.getNames()
res = {}

for i=0, #periph do
  if string.sub(tostring(periph[i]), 1, 40) == "tile_thermalexpansion_cell_resonant_name" then
	table.insert(res, periph[i])
  end
end

function reDraw(curPower, maxPower)
  percent = math.floor( (curPower / maxPower) * 100 )
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.red)
  print("Power: "..curPower)
  term.setTextColor(colors.orange)
  print("Max power: "..maxPower)
  term.setTextColor(colors.lime)
  print(percent.."%")
end

while true do
  power = 0
  maxPower = 0
  for i=0, #res do
	curPeriph = peripheral.wrap(tostring(res[i]))
	cellMaxPower = curPeriph.getMaxEnergyStored()
	cellPower = curPeriph.getEnergyStored()
	power = power + cellPower
	maxPower = maxPower + cellMaxPower
  end

  reDraw(power, maxPower)
end
Edited on 01 December 2015 - 08:33 PM
Creator #2
Posted 01 December 2015 - 09:21 PM
Can you put it in pastebin because figuring out the number line is rather hard on the forums.
Dragon53535 #3
Posted 01 December 2015 - 11:47 PM
Little tip about the first loop you make

for i=0, #periph do
  if string.sub(tostring(periph[i]), 1, 40) == "tile_thermalexpansion_cell_resonant_name" then
		table.insert(res, periph[i])
  end
end
There's a specific function in the peripheral API for getting the type, or more specific "tile_thermalexpansion_cell_resonant_name"


for _,v in pairs(periph) do --#Better loop for tables, rather than having to reference periph[i] I now only have to say v
  if peripheral.getType(v) == "tile_thermalexpansion_cell_resonant_name" then
		table.insert(res, v)
  end
end


SECONDLY, your error you've been getting, is because of one simple thing: Tables in Lua start at index 1, not 0


for i=0, #res do

for i=0, #periph do
periph[0] and res[0] will BOTH be nil


curPeriph = peripheral.wrap(tostring(res[i]))
tostring on nil will return "nil" (I think, or just nil) and peripheral.wrap will not be able to wrap that peripheral and will return nil.


cellMaxPower = curPeriph.getMaxEnergyStored()
curPeriph is nil, since peripheral.wrap couldn't wrap a nil peripheral.

In the future, I would recommend figuring out why it didn't work for you rather than changing a call. Because you're gonna run into it a lot more in the future if you don't change.

Oh, and an easier way to do this program:

For what you were doing btw, calling was better in that case, since you didn't have to keep wrapping them.


local periph = peripheral.getNames() --#Make it local, runs better.
local res = {} --# ^
for _,v in pairs(periph) do --#Better loop for tables, rather than having to reference periph[i] I now only have to say v
  if peripheral.getType(v) == "tile_thermalexpansion_cell_resonant_name" then
		table.insert(res, peripheral.wrap(v)) --#Just insert the already wrapped peripheral, it's much easier since you are gonna start using it.
  end
end
local function reDraw(curPower, maxPower) --#Make the function local, runs slightly better, and other programs can't call this function when it's over.
  local percent = math.floor( (curPower / maxPower) * 100 ) --#Make percent local, so that it gets deleted on the end of this function.
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.red)
  print("Power: "..curPower)
  term.setTextColor(colors.orange)
  print("Max power: "..maxPower)
  term.setTextColor(colors.lime)
  print(percent.."%")
end
while true do
  local power = 0 --#Make power local
  local maxPower = 0 --#Make maxPower local
  for a,v in pairs(res) do --#Better for loop for tables
		local cellMaxPower = v.getMaxEnergyStored() --#Oh hey, local cellMaxPower, and easier to call getMaxEnergyStored
		local cellPower = v.getEnergyStored() --# ^
		power = power + cellPower
		maxPower = maxPower + cellMaxPower
  end
  reDraw(power, maxPower)
end
--#All in all, you've got indentation down (thankfully) and camelCase. Just need to work on locals, and learn about the pairs and ipairs for loops.
Edited on 02 December 2015 - 12:59 AM