1

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
1
  • Twice you use the idiom for k,v in pairs(t) do ... end where you capture the value from the last iteration of the loop to use later. Since pairs() 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. Commented Nov 15, 2013 at 22:54

2 Answers 2

1
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".

Sign up to request clarification or add additional context in comments.

9 Comments

Assuming the last entry that pairs(tableInfo) returns has the values you expect it to. Note that pairs doesn't have a defined order for how it will return the table key/value pairs.
Here is the table itself: {[1]={["rawName"]="fluid.honey",["amount"]=12150,["capacity"]=576000,["name"]="honey",["id"]=52,},}
If that table is tableInfo then it should work (and a quick mock-up test works here). Wrap the variables in your monX.write calls in calls to tostring() (e.g. .. tostring(fluidName) ..) and see where you get nils and where you get values.
That got it. Thank you very much. The issue was tank0 was empty, so it was returning nil for amount.
The only issue now is fluidName is actually showing the amount inside the tank instead.
|
1

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 {}).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.