Page 1 of 1

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

Posted: Fri Apr 17, 2015 3:25 pm
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).

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

Posted: Mon Apr 20, 2015 4:40 am
by Locutus
Uhhm... table.remove(guardList, character)?
I mean, OnCharacterDeath already tells you the character index.

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

Posted: Mon Apr 20, 2015 12:35 pm
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.

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

Posted: Wed Apr 29, 2015 8:26 am
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.

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

Posted: Wed Apr 29, 2015 12:17 pm
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.