我正在学习计算机技术(我的世界)中的编程,并且在读取一些存储单元时遇到了一些麻烦。
我正在处理的函数将遍历所有单元格并将存储容量添加到 for 循环中的变量中。
这是我到目前为止得到的
local cell1 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_2")
local cell2 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_3")
local cell3 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_4")
local cell4 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_5")
local cell5 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_6")
local cell6 = peripheral.wrap("tile_thermalexpansion_cell_reinforced_name_7")
cells = {"cell1", "cell2", "cell3", "cell4", "cell5", "cell6"}
local totStorage
function getTotStorage(table)
for key = 1,6 do
x = table[key]
totStorage = totStorage + x.getMaxEnergyStored()
end
print(totStorage)
end
我在这条线上得到一个错误
totStorage = totStorage + x.getMaxEnergyStored()
说“尝试调用 nil”。有什么建议么?