Talisman
Talisman

Reputation: 388

Lua positioning/tables?

this should be trivial to some but I don't get it :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

The error given is: Attempt to index global 'pos2' (a nil value)

Any ideas?

Upvotes: 1

Views: 334

Answers (1)

Mud
Mud

Reputation: 28991

You get the position into the variable pos, then are indexing pos2. pos2 is never initialized, so when you try to index it (pos2:blah) you get an error about trying to index nil.

Side note: the pos = {} line is completely superfluous, because you overwrite pos two lines later. Also, most of these variables should be made local, which is both faster and avoids polluting the global namespace.

Minor refactor knowing nothing about your code and/or the API you're using:

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

Upvotes: 3

Related Questions