I have a project which spawns lots of "weapon" objects, which take a variety of forms. To keep track of them, I have a table named "weapon" which I add to whenever a new weapon object is spawned. I want to delete weapons when they exceed some maximum distance from the player, which led me to something like this:
Code: Select all
for i = 1, table.maxn(weapon) do
if((weapon[i].x - player.x)^2 + (weapon[i].y - player.y)^2 > weapon[i].killDistance) then
table.remove(weapon, i)
end
end
Code: Select all
local killset = {}
for i = 1, table.maxn(weapon) do
if((weapon[i].x - player.x)^2 + (weapon[i].y - player.y)^2 > weapon[i].killDistance) then
table.insert(killset,i)
end
end
for i = 1, table.maxn(killset) do
local todie = killset[table.maxn(killset)]
table.remove(killset, todie)
table.remove(weapon, todie)
end
--Mr. Strange