Lua定位/表格?

这对一些人来说应该很简单,但我不理解 :s

如果Message == "!kanebot",则
    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已购买了一个kanebot。", Get_Player_Name_By_ID(pID))
end

给出的错误是:试图索引全局变量“pos2”(一个空值)

有什么想法吗?

点赞
用户501459
用户501459

你将位置保存在变量 pos 中,然后通过索引 pos2 进行操作。pos2 从未被初始化,因此当你尝试对其进行索引 (pos2:blah) 时,会出现有关尝试对 nil 进行索引的错误。

顺便说一下:pos = {} 这一行完全是多余的,因为在两行之后你又覆盖了 pos。此外,大多数变量应该声明为局部变量,这既更快,也避免了污染全局命名空间。

不了解你的代码和/或使用的 API,略作修改如下:

if Message == "!kanebot" then
    local gameobj = Get_GameObj(pID)
    local pos = Get_Position(gameobj)
    local cosValue = math.cos(Get_Facing(getobj)*(math.pi / 180))
    pos:AssignX(pos:GetX() + 4 * cosValue)
    pos:AssignY(pos:GetY() + 4 * cosValue)
    pos:AssignZ(pos:GetZ() + 0.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-30 18:03:11