1

这对某些人来说应该是微不足道的,但我不明白: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”(一个零值)

有任何想法吗?

4

1 回答 1

3

你把位置变成变量pos,然后是索引pos2pos2永远不会被初始化,所以当你尝试索引它时(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
于 2012-07-30T18:03:11.897 回答