Reputation: 51
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
Upvotes: 1
Views: 1275
Reputation: 29473
The "local" qualifier limits the scope to block or chunk, so the locals in the loop in getTank() are scoped to the loop; outside the loop their values are lost. So when getTank returns, the variables it returns have not been defined in the scope of the function, so they are all nil. See http://www.lua.org/manual/5.1/manual.html#2.6 for useful examples.
But since that doesn't seem to fix your problem, I wager you have an additional issue, namely that local tableInfo
is empty table, which implies tankPeriph.getTankInfo("unknown")
returns empty table (not nil
, but {}
).
Upvotes: 1
Reputation: 80921
function getTank(tankPeriph)
-- This has been tested and works
local tableInfo = tankPeriph.getTankInfo("unknown") -- Local to the getTank function.
for k,v in pairs(tableInfo) do
local fluidRaw = v.rawName -- local to this for loop
local fluidName = v.name -- local to this for loop
local fluidAmount = v.amount -- local to this for loop
local fluidCapacity = v.capacity -- local to this for loop
end
return fluidRaw, fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
end
As indicated in my edited snippet above your locals are not local to where you think they are and you aren't returning their values from your function correctly. Move the local declaration for those variables to outside the for loop (keep the assignment in the for loop if you need it, though I can't really imagine that you do since you only get the last values in the loop that way) and your function should "work".
Upvotes: 1