检查是否已创建 table 无效

Checking if a table has been created not working

使用代码:

function createNewBody(name,mass)
if not world.body[name]==nil then
    print("This body has already been created. Maybe you meant to update it's values?\n")
else
    world.body[name]={mass=m,x=0,y=0,xAccel=0,yAccel=0,xR=0,yR=0,properties={gaseous=false,texture=""}}
    world.bodies=world.bodies+1
end
end

这段代码没有显示任何错误,但是当我将 createNewBody(moon,1.622) 绑定到一个密钥然后使用它时,它让我在不显示错误消息的情况下向密钥发送垃圾信息。

而且,是的,我已经定义了 world.bodiesworld.body

not world.body[name]==nil 被解析为 (not world.body[name])==nil。由于 not 的结果是一个布尔值,它永远不会是 nil.

尝试not(world.body[name]==nil)world.body[name]~=nil