试图两次调用同一函数并传入不同的参数。

在我的主函数中,我进行以下调用:

function Main()
    FBIVVoit(1,9,1,0)
    FBIVVoit(1,9,1,1)
end

-- 这是函数代码:

function FBIVVoit(numTrain, numCar, numFBIVVoit, State)
  numTrainCount = 1
    while numTrainCount <= numTrain do
      numCarCount = 1
      while numCarCount <= numCar do
        FBIVVoit = dictionary.getvalue(Dict, 'SCVSStatutsV' .. numCarCount .. '.FBIVVoit')
        print('SCVSStatutsV' .. numCarCount .. '.FBIVVoit'
          .. ' before the change is: ' .. FBIVVoit)
        f:write(string.format('\n' .. os.date() .. ' -- ' .. 'SCVSStatutsV'
          .. numCarCount .. '.FBIVVoit' .. ' before the change is: ' .. FBIVVoit))
        dictionary.setvalue(Dict, 'SCVSStatutsV' .. numCarCount .. '.FBIVVoit', State);
        FBIVVoit = dictionary.getvalue(Dict, 'SCVSStatutsV' .. numCarCount .. '.FBIVVoit')
        print('SCVSStatutsV' .. numCarCount .. '.FBIVVoit' .. ' is now: ' .. FBIVVoit)
        f:write(string.format('\n' .. os.date() .. ' -- ' .. 'SCVSStatutsV'
          .. numCarCount .. '.FBIVVoit' .. ' is now: ' .. FBIVVoit))
        fSleep(1)
        numCarCount = numCarCount + 1
      end
      numTrainCount = numTrainCount + 1
    end
    print('Run stopped at ' .. os.date());
    f:write(string.format('\n' .. os.date() .. ' -- ' .. 'Run stopped'))
    f:close()
  end

-- 当我运行Main函数时,第一个对FBIVTrain函数的调用可以成功通过。在第二个调用时,我收到以下错误消息:

error in LUA script: c:\lua\pstm_test.lua:95: attempt to call global 'FBIVVoit' (a number value).

-- 真正困扰我的是,我还有很多其他函数,我多次调用它们并传递不同的参数给函数,但我没有得到这种行为。

-- 你们中是否有人经历过这种情况,或者在查看我的代码时有什么问题?我被困在那里,希望得到帮助。

点赞
用户2198692
用户2198692

你在这里覆盖了函数:

FBIVVoit = dictionary.getvalue(Dict, 'SCVSStatutsV' .. numCarCount .. '.FBIVVoit')

dictionary.getvalue()的结果覆盖它。因此下次你尝试调用函数时,它就不存在了。

如果你真的想在函数内使用相同的变量名,请使用local限定符。实际上,始终为临时变量使用local。它速度快且不会污染全局命名空间。

2014-04-14 19:11:47