The following function getTank() works (if not used as a function to print fluidName for example). The issue comes when I return the values and then try to access them outside of the function. The result is 'attempt to concatenate string and nil' at the lines: mon2.write(returnedVariable) for example which is outside of the function.
If I simply do the following:
for k,v in pairs(tableInfo) do amount=v.amount end
print(amount)
outside of the function, it gives the correct value.
function getTank(tankPeriph)
-- This has been tested and works
local tableInfo = tankPeriph.getTankInfo("unknown")
local fluidRaw, fluidName, fluidAmount, fluidCapacity
for k,v in pairs(tableInfo) do
fluidRaw = v.rawName
fluidName = v.name
fluidAmount = v.amount
fluidCapacity = v.capacity
end
return fluidRaw, fluidName, fluidAmount, fluidCapacity
end
function dispTanks()
-- working on it
-- TANK 0
mon2.setCursorPos(rowPos, ironTank0Col)
mon2.clearLine()
local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(irontank0)
mon2.write("Iron Tank 0 (" .. fluidName .. ") : " .. fluidAmount)
-- TANK 1
mon2.setCursorPos(rowPos, ironTank1Col)
mon2.clearLine()
local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(irontank1)
mon2.write("Iron Tank 1 (" .. fluidName .. ") : " .. fluidAmount)
-- TANK 2
mon2.setCursorPos(rowPos, ironTank2Col)
mon2.clearLine()
local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(irontank2)
mon2.write("Iron Tank 2 (" .. fluidName .. ") : " .. fluidAmount)
-- TANK 3
mon2.setCursorPos(rowPos, ironTank3Col)
mon2.clearLine()
local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(irontank3)
mon2.write("Iron Tank 3 (" .. fluidName .. ") : " .. fluidAmount)
-- TANK 4
mon2.setCursorPos(rowPos, ironTank4Col)
mon2.clearLine()
local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(irontank4)
mon2.write("Iron Tank 4 (" .. fluidName .. ") : " .. fluidAmount)
end
for k,v in pairs(t) do ... endwhere you capture the value from the last iteration of the loop to use later. Sincepairs()does not guarantee any particular order of traversal of the table (and can, in fact, go in different orders for the same table content), this is probably not doing what you think it is doing.