2016-05-29 25 views
0

我制作了一个让布娃娃向上飞的小脚本。它的工作原理,但它留下了一个错误消息,我不知道为什么。什么原因导致“试图使用NULL物理对象!”在我的Garry的Mod Lua脚本中出错?

[ERROR] RunString:11: Tried to use a NULL physics object! 
    1. ApplyForceCenter - [C]:-1 
    2. fn - RunString:11 
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:179 

的误差得到垃圾邮件的控制台,直到我删除所有现有的布娃娃

我的代码:

hook.Add("Think", "Fly", function() 

ent = ents:GetAll() 

    for k, v in pairs(ent) do 
    local isRagdoll = v:IsRagdoll() 
     if isRagdoll == true then 
     phys = v:GetPhysicsObject() 
     phys:ApplyForceCenter(Vector(0, 0, 900)) 

     end 
    end 
end) 

在此先感谢。

回答

1

Henrik的回答是发现关于逻辑。在尝试使用它之前,您确实需要确保物理对象是有效的。

在GMod中,其功能是IsValid

if IsValid(phys) then

我已经加入此为Henrik的回答评论,但我不太有足够的代表呢。

+0

谢谢,马特,我相应地编辑了我的答案。顺便说一句,你现在应该有足够的代表评论无论你喜欢;) –

+0

@HenrikIlgen谢谢你! :) – MattJeanes

1

编辑:感谢MattJearnes澄清如何检查NULL的gmod对象。

不知道gmod的API什么的,我猜GetPhysicsObject可以返回一个描述NULL的特殊值,在这种情况下你不能调用ApplyForceCenter就可以了。您应该简单地使用IsValid做任何事情之前检查NULL

hook.Add("Think", "Fly", function() 
    ent = ents:GetAll() 

    for k, v in pairs(ent) do 
     local isRagdoll = v:IsRagdoll() 
     if isRagdoll == true then 
      local phys = v:GetPhysicsObject() 
      if IsValid(phys) then 
       phys:ApplyForceCenter(Vector(0, 0, 900)) 
      end 
     end 
    end 
end) 
相关问题