Lua:通过函数返回表为 nil

我遇到了错误:source_file.lua:5: attempt to call a nil value (global 'getCard')

我尝试从 questCards 中捕获与给定的字符串 objName 相同的 Index=name 的正确表

questCards={{['name']='test1',['creatureName']='test3'},{['name']='test2',['creatureName']='test4'}}
obj='test1'
card=getCard(obj)
card['creatureName']=nil --仅用于测试
if card['creatureName']==nil then
    --做某事
end
function getCard(objName)
  for k,v in pairs(questCards) do
    if v['name']==objName then
      return v
    end
  end
end
点赞
用户107090
用户107090

错误消息告诉你,在调用 getCard 的地方,getCard 是未定义的。

在调用 getCard 之前,你需要先定义它。

2018-07-13 18:50:36