简体   繁体   English

Lua函数多个返回值,Print(returnvalue)导致尝试连接字符串和Nil

[英]Lua Function Multiple Return Values, Print(returnvalue) Results in Attempt to Concatenate String and Nil

The following function getTank() works (if not used as a function to print fluidName for example). 以下函数getTank()起作用(例如,如果不用作打印fluidName的函数)。 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. 结果是'attempt to concatenate string and nil'mon2.write(returnedVariable)例如在函数之外。

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
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". 将这些变量的局部声明移到for循环之外(如果需要,可将其保留在for循环中,尽管我真的无法想象您会这样做,因为这样只会得到循环中的最后一个值)功能应该“起作用”。

The "local" qualifier limits the scope to block or chunk, so the locals in the loop in getTank() are scoped to the loop; “ local”限定词将范围限制为块或块,因此getTank()中循环中的局部变量作用域为循环; 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. 因此,当getTank返回时,它返回的变量尚未在函数范围内定义,因此它们均为nil。 See http://www.lua.org/manual/5.1/manual.html#2.6 for useful examples. 有关有用的示例,请参见http://www.lua.org/manual/5.1/manual.html#2.6

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 {} ). 但是由于这似乎无法解决您的问题,因此我认为您还有一个问题,即local tableInfo是空表,这意味着tankPeriph.getTankInfo("unknown")返回空表(不是nil ,而是{} )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM