首页 > 解决方案 > Lua:返回表槽函数为零

问题描述

我收到错误:source_file.lua:5: 尝试调用 nil 值(全局“getCard”)

我尝试在 questCards 中找到正确的表,其中 Index=name 与 objName 中的给定字符串相同

questCards={{['name']='test1',['creatureName']='test3'},{['name']='test2',['creatureName']='test4'}}
obj='test1'
card=getCard(obj)
card['creatureName']=nil --Only for test purpose
if card['creatureName']==nil then
    --do Somthing
end
function getCard(objName)
  for k,v in pairs(questCards) do
    if v['name']==objName then
      return v
    end
  end
end

标签: lualua-table

解决方案


错误消息告诉您getCard在调用它时未定义。

您需要getCard在调用它之前进行定义。


推荐阅读