Lua:返回表槽函数为nil

时间:2018-07-13 18:45:23

标签: lua lua-table

我收到错误: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

1 个答案:

答案 0 :(得分:1)

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

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