(Lua)Attempt to Index Field '?' (a nil value) [Solved]

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

(Lua)Attempt to Index Field '?' (a nil value) [Solved]

Post by jedimoose32 »

Hi everyone.

I'm trying to have certain units added to a table when they spawn (along with a list of useful information such as their GetCharacterUnit data, GetCharacterTeam, and some other items). That part works fine, but I'm running into this error when I try and remove them from the table upon their death (maybe I shouldn't be trying to do that...?).
Error:

Code: Select all

Message Severity: 3
.\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to index field `?' (a nil value)
stack traceback:
	(none): in function <(none):1021>
And here's my code for adding them to the table, and after that the removal code (called by OnCharacterDeath):
Hidden/Spoiler:
[code]
local onenemySpawn = OnCharacterSpawn(
function(character)
if GetCharacterTeam(character) == self.teamDEF or GetCharacterTeam(character) == self.teamDF2 then
local guardInfo = {
ID = character,
Class = GetCharacterClass(character),
Unit = GetCharacterUnit(character),
Team = GetCharacterTeam(character),
Effect = "none"
}
self:PrintMe("GUARD INFODUMP - Class: " .. guardInfo.Class .. "\tTeam: " .. guardInfo.Team .. "\tEffect: " .. guardInfo.Effect)
self.EnemiesAliveCounter = self.EnemiesAliveCounter + 1
table.insert(guardList, guardInfo)
if guardInfo.Team == self.teamDEF then
attachAnonEffects(table.getn(guardList))
dumpInfo(table.getn(guardList))
end
if guardInfo.Team == self.teamDF2 then
table.insert(aggroFX, CreateEffect("assn_sfx_aggro"))
AttachEffectToObject(aggroFX[table.getn(aggroFX)], guardInfo.Unit)
guardList[table.getn(guardList)].Effect = "aggro"
dumpInfo(table.getn(guardList))
end
end
end
)

OnCharacterDeath(
function(character, killer)
if character ~= nil and killer ~= nil then
...
if GetCharacterTeam(character) == self.teamDEF or GetCharacterTeam(character) == self.teamDF2 or GetCharacterTeam(character) == self.teamDXX then
self.EnemiesAliveCounter = self.EnemiesAliveCounter - 1
for i=1, table.getn(guardList) do
self:PrintMe("Guard died, searching table")
if guardList.ID == character then
self:PrintMe("Found him, removing from table")
table.remove(guardList, i)
end
end
end
...
end
end
)
[/code]


EDIT: It occurred to me just now that if the character is dead, then 'character' would be nil, right? So then I don't really have a way of looking the guy up in the table. Does anyone have any suggestions? guardList.Unit won't work because GetCharacterUnit(character) will also return a nil since the unit doesn't exist (y'know, cause he's dead).
Last edited by jedimoose32 on Wed Apr 29, 2015 12:17 pm, edited 1 time in total.
User avatar
Locutus
1st Lieutenant
1st Lieutenant
Posts: 420
Joined: Fri Jun 04, 2010 10:08 am
Projects :: Stargate Battlefront Pegasus
Location: Germany
Contact:

Re: (Lua)Attempt to Index Field '?' (a nil value)

Post by Locutus »

Uhhm... table.remove(guardList, character)?
I mean, OnCharacterDeath already tells you the character index.
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: (Lua)Attempt to Index Field '?' (a nil value)

Post by jedimoose32 »

Okay, that makes sense but I'm not totally sure about the correct syntax. I'm pretty sure table.remove(guardList, character) won't work, because the keys in guardList are just numbers. The value of each key in guardList is the contents of the guardInfo table that gets populated inside OnCharacterSpawn(). "character" is only going to match guardList.ID. Which is why I currently have the for loop to go through the numerical keys in guardList, and then the if-then checking whether guardList.ID == character. Hopefully that makes sense.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: (Lua)Attempt to Index Field '?' (a nil value)

Post by [RDH]Zerted »

Instead of table.insert(guardList, guardInfo) do guardList[character] = guardInfo and that way you can use character as a key to look things up instead of looping and matching IDs.

Though I don't know if that's related to your ? error because I don't know which line is line 1021.
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: (Lua)Attempt to Index Field '?' (a nil value)

Post by jedimoose32 »

Heh, sorry to do this to you again Zerted, but I actually tried that the other day and it worked. :oops: Forgot to mark the thread as solved. Don't hate me too much.
Post Reply