是什么导致"尝试使用NULL物理对象!"我的Garry的Mod Lua脚本中有错误?

时间:2016-05-29 22:38:29

标签: lua garrys-mod

我做了一个小小的剧本,让布娃娃向上飞。它有效,但它留下了错误信息,我无法弄清楚原因。

[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

错误在控制台中发送垃圾邮件,直到我删除所有现有的ragdolls

我的代码:

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)

提前致谢。

2 个答案:

答案 0 :(得分:1)

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

在不了解gmod的API的情况下,我猜测GetPhysicsObject可以返回描述NULL的特殊值,在这种情况下,您无法调用ApplyForceCenter在上面。在使用NULL执行任何操作之前,您应该只检查IsValid

    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)

答案 1 :(得分:1)

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

在GMod中,此功能为IsValid

if IsValid(phys) then

我已将此添加为对Henrik的答案的评论,但我还没有足够的代表。