Lua定位/表?

时间:2012-07-30 17:20:06

标签: lua lua-table

对一些人来说这应该是微不足道的,但我不明白:s

if Message == "!kanebot" then
    pos = {}
    pObj = Get_GameObj(pID)
    pos = Get_Position(pObj)
    pos2:AssignX(pos2:GetX()+ 4*math.cos(Get_Facing(Get_GameObj(pID))*(math.pi / 180)))
    pos2:AssignY(pos2:GetY()+ 4*math.cos(Get_Facing(Get_GameObj(pID))*(math.pi / 180)))
    pos2:AssignZ(pos2:GetZ()+ .3)
    reinf = Create_Object("Nod_Kane", pos)
    Attach_Script_Once(reinf, "M01_Hunt_The_Player")
    Attach_Script_Once(reinf, "M00_No_Falling_Damage_DME")
    InputConsole("%s has bought a kanebot.", Get_Player_Name_By_ID(pID))
end

给出的错误是:尝试索引全局'pos2'(零值)

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您获得变量pos的位置,然后将pos2编入索引。 pos2永远不会被初始化,因此当您尝试对其进行索引(pos2:blah)时,您会收到有关尝试索引nil的错误。

旁注:pos = {}行完全是多余的,因为您稍后会覆盖pos两行。此外,大多数这些变量应该是本地的,这既快又避免污染全局命名空间。

对您的代码和/或您正在使用的API一无所知的次要重构:

if Message == "!kanebot" then
    local gameobj = Get_GameObj(pID)
    local pos = Get_Position(gameobj)
    pos:AssignX(pos:GetX()+ 4*math.cos(Get_Facing(getobj)*(math.pi / 180)))
    pos:AssignY(pos:GetY()+ 4*math.cos(Get_Facing(getobj)*(math.pi / 180)))
    pos:AssignZ(pos:GetZ()+ .3)
    local reinf = Create_Object("Nod_Kane", pos)
    Attach_Script_Once(reinf, "M01_Hunt_The_Player")
    Attach_Script_Once(reinf, "M00_No_Falling_Damage_DME")
    InputConsole("%s has bought a kanebot.", Get_Player_Name_By_ID(pID))
end