table size, empty tables and adding elements to a table
Posted: Wed Sep 23, 2009 3:39 am
Alright, me again. I've been looking for a reason why in LUA help, but can't find one, so maybe you guys can tell me. I have this code (Yes, this damn code again!):
Now, this code WORKS as long as myEnemies is not empty. If the table myEnemies is empty, nothing happens. I do en Test, and I get no feedback. No echo, no add to the table, and no error in debug. If I manually add Test to the table, and then try to en TestAgain, it works! I get TestAgain is now an enemy!, I can add other names, remove the original entry, blah, blah, UNTIL I remove all entries, and then we're back to broke.
So I thought that maybe I need to check for it being empty, as maybe for .. do breaks under an empty table, so I tried:The code compiles, but still when I try it nothing happens, no response, no error. Any clues?
Code: Select all
if myEnemies == nil then
myEnemies = {};
end
local newenemy = matches[2]
for k,v in pairs(myEnemies) do
if v == newenemy then
echo("" .. newenemy .. " is already an enemy!\n")
else
echo("" .. newenemy .. " is now an enemy!\n")
listAdd( myEnemies, newenemy )
break
end
end
So I thought that maybe I need to check for it being empty, as maybe for .. do breaks under an empty table, so I tried:
Code: Select all
if myEnemies == nil then
myEnemies = {};
end
local newenemy = matches[2]
for k,v in pairs(myEnemies) do
if IsEmpty(myEnemies) then
echo("No enemies!")
break
elseif v == newenemy then
echo("" .. newenemy .. " is already an enemy!\n")
else
echo("" .. newenemy .. " is now an enemy!\n")
listAdd( myEnemies, newenemy )
break
end
end