## table.remove wont "Remove" table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

### Re: table.remove wont "Remove" table

There's multiple problems with your code. First of all:

Code: Select all

if distance then
table.remove(enemies.i)
end
The condition for an enemy colliding with the player is if its distance from the player is less than a certain threshold. The line "if distance then" is just checking if the variable distance exists, which it does. What you want is something like "if distance < 10 then". There's another problem, however:

Code: Select all

for k,enemy in pairs(enemies) do
distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
end
You're not actually keeping track of the distance of each enemy from the player, you're just creating one distance variable and overwriting it for each enemy. I'd consolidate your two loops and do something like this (pseudocode):

Code: Select all

  for each enemy
distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
if distance < threshold
remove enemy
end
end

BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

### Re: table.remove wont "Remove" table

@Tesselode Okay it works!

thanks everyone for the help
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

### Re: table.remove wont "Remove" table

Also take a note that

distance = ((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2) ^ 0.5

is faster than

distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: table.remove wont "Remove" table

arampl wrote:Also take a note that

distance = ((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2) ^ 0.5

is faster than

distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
That is a lie, it doesnt apply to LuaJIT which compiles both cases to the same byte code, it applies to Lua though, since the later one needs to look for the function math.sqrt then call it, generate it's environment and process the call, pass the arguments to the C function and return the value.

Since LÖVE uses LuaJIT the later could even be faster...
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)
RBXcpmoderator12345
Citizen
Posts: 59
Joined: Sat Mar 07, 2015 11:53 pm

### Re: table.remove wont "Remove" table

table.remove removes a value from a table and puts it somewhere else
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

### Re: table.remove wont "Remove" table

May be not "lie", just "you are wrong"?
Positive07 wrote: ... compiles both cases to the same byte code ... the later could even be faster...

Without using math.sqrt program doesn't depend on any optimizations and becomes more readable.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

### Re: table.remove wont "Remove" table

If you are only comparing distances, it might be good to know that calculating square root is kind of expensive operation, since up to my knowledge it always involves a lot of calculation. Might be using newton method or something, but regardless it is a lot more expensive to calculate square root than sum or multiply. Same goes for all other numbers that are represented by taylor series like sin, cos, etc.

Code: Select all

local tresh = 3
local dist = math.sqrt(a*a + b*b)
if dist < tresh
print("within")
end


Code: Select all

local tresh = 3
local dist = a*a + b*b
if dist < tresh*tresh
print("within")
end

After saying that I have heard that.: "Premature optimization is the root of all evil." And if you calculate one square root per update loop it probably doesn't matter, but if you do a lot of them it might actually have a minor impact on performance.

Edit: Oh and if you do not care in which order the items are in a table, and you do have a lot of them. Swapping the object that you want to remove with the last object, then decreasing the loop counter by one and removing the last object is a lot faster than removing from a middle of a table. This is since the table tries to maintain the order, and moving every single object in the table by one might cause some slow downs in case you have A HUGE table.
Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: table.remove wont "Remove" table

arampl wrote:May be not "lie", just "you are wrong"?
Positive07 wrote: ... compiles both cases to the same byte code ... the later could even be faster...

Without using math.sqrt program doesn't depend on any optimizations and becomes more readable.
Nono, it's not like you are wrong, it's more like a lie in the sense that it doesnt apply to LÖVE (doesnt really matter it was an expression hahahaha)

I contradicted myself because I really dont know the internals of LuaJIT, I think both compile to the same code, but If I was wrong and they gave two different outputs then math.sqrt would be even more optimized than ^0.5, because the creator of LuaJIT would have put more attention on a function than in a normal operation. And that is because if the number wasnt 0.5 then you would probably need more math than a standard root so you would apply other code and not the standard math.sqrt code... I dont know how clever LuaJIT is but I know it is ultra fast so as Muris says, optimizing at this point is pointless
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

### Re: table.remove wont "Remove" table

Guys, I agree that optimization is the root of evil. This is just a small useful trick.

Positive, if you're not sure about internals just make tests:

Code: Select all

function love.load()

sqx1 = {}
sqx2 = {}

sqy1 = {}
sqy2 = {}

for i = 1, 1000000 do
sqx1[i] = love.math.random()
sqx2[i] = love.math.random()
sqy1[i] = love.math.random()
sqy2[i] = love.math.random()
end

t1 = love.timer.getTime()
for j = 1, 10000 do
for i = 1, #sqx1 do
b = ((sqx1[i] - sqx2[i]) ^ 2 + (sqy1[i] - sqy2[i]) ^ 2) ^ 0.5
end
end
t2 = love.timer.getTime()
for j = 1, 10000 do
for i = 1, #sqx1 do
b = math.sqrt((sqx1[i] - sqx2[i]) ^ 2 + (sqy1[i] - sqy2[i]) ^ 2)
end
end
t3 = love.timer.getTime()

print("without math.sqrt: " .. t2 - t1)
print("using   math.sqrt: " .. t3 - t2)
end

gives output:

without math.sqrt: 86.575054844
using math.sqrt: 86.694315749

So this is apply to LÖVE as you can see. Soooo.... OBEY!!!

But Muris are right - the catch is small.
Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: table.remove wont "Remove" table

Hahaha that is super small, it is possibly a hicup in your computer hahaha which proves that both of them are optimized and return the same byte code in JIT

I'm OBEYING since the beginning while you dont identify your self! Stop hiding your face, put an avatar with our sign! let's make the world OBEY!

PS: My avatar may be a little too obfuscated but it said OBEY somewhere...
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

### Who is online

Users browsing this forum: Google [Bot] and 14 guests