It loops one time if you call the function, right? So why the same element inserted again? Does it loop again somewhere else? But what about
restart the
self.targets? I mean you remove all values within
self.targets then refill it with nearest target. It may help.
Code: Select all
function Tower.getAllNear(self)
self.targets = {}
for index, value in ipairs(Troop) do
if (self:getSquaredDistance(value) <= self.range) then
table.insert(self.targets, value)
end
end
end
You shouldn't make a new variable if it only used one time like
distance and
shortestDistance. Also
nearest made for nothing.
EDIT:
Maybe you should make a new local table then insert nearest targets which may help better.
Code: Select all
function Tower.getAllNear(self)
local nearest = {}
for index, troop in ipairs(Troop) do
if (self:getSquaredDistance(troop) <= self.range) then
table.insert(nearest, troop)
end
end
return nearest
end
Azhukar, values for
Troop that he loops maybe return a table so you can't index a table with a table.