Page 1 of 3
table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 9:26 am
by BluBillz
I am trying to have the enemy table remove itself when it equals the players x and y position. But how I have it isn't actually removing anything for some reason.
in love.update(dt)
Code: Select all
for k,enemy in pairs(enemies) do
if enemy.x == player.x and enemy.y == player.y then
table.remove(enemies,enemy)
end
end
Why would it not be actually removing that current enemy?
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 9:37 am
by ivan
"table.remove" is used for numerically-indexed tables and works like so:
Where "list" is a numerically indexed table and "index" is the
numeric index of the element you want to remove.
Removing more than 1 element from a numerically indexed table
during iteration is always done in reverse because it modifies the indices of the following elements:
Code: Select all
for i = #list, 1, -1 do
if list[i] == search_item then
table.remove(list, i)
-- break -- if you want to remove just 1 element
end
end
If your table is not-numerically indexed you can always write:
Code: Select all
for k, v in pairs(t) do
if v == search_item then
t[k] = nil
-- break -- if you want to remove just 1 element
end
end
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 9:49 am
by BluBillz
ivan wrote:"table.remove" is used for numerically-indexed tables and works like so:
Where "list" is a numerically indexed table and "index" is the
numeric index of the element you want to remove.
Removing more than 1 element from a numerically indexed table
during iteration is always done in reverse because it modifies the indices of the following elements:
Code: Select all
for i = #list, 1, -1 do
if list[i] == search_item then
table.remove(list, i)
-- break -- if you want to remove just 1 element
end
end
If your table is not-numerically indexed you can always write:
Code: Select all
for k, v in pairs(t) do
if v == search_item then
t[k] = nil
-- break -- if you want to remove just 1 element
end
end
I did the following
Code: Select all
for i=#enemies, 1,-1 do
if enemies[i] == player.x or player.y then
table.remove(enemies,i)
end
end
But now the enemies just spawn and instantly delete! What did I do wrong? I tried the other way and same problem..
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 10:09 am
by ivan
try:
Code: Select all
for i=#enemies, 1,-1 do
if enemies[i].x == player.x and enemies[i].y == player.y then
table.remove(enemies,i)
end
end
also, this code may not be very robust, you probably want to check
the distance between the player and the enemy
Code: Select all
for i=#enemies, 1,-1 do
if distance(player, enemies[i]) < threshold then
table.remove(enemies,i)
end
end
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 10:41 am
by BluBillz
ivan wrote:try:
Code: Select all
for i=#enemies, 1,-1 do
if enemies[i].x == player.x and enemies[i].y == player.y then
table.remove(enemies,i)
end
end
also, this code may not be very robust, you probably want to check
the distance between the player and the enemy
Code: Select all
for i=#enemies, 1,-1 do
if distance(player, enemies[i]) < threshold then
table.remove(enemies,i)
end
end
I litterally copy/paste your code inside love.update(dt) and it doesn't do anything...
keep in mind my enemies table looks like this
Code: Select all
function love.load()
enemies =
{
{
image = love.graphics.newImage("r_demon.gif"),
x = 200, y = 300, speed = 80, heading = 0,
hw = 16, hh = 16
},
{
image = love.graphics.newImage("b_demon.gif"),
x = 300, y = 300, speed = 60, heading = 0,
hw = 16, hh = 16
}
}
end
I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 10:56 am
by ivan
Keep in mind that when you compare numbers like so:
is not robust because enemies move at discrete steps (speed*dt).
Therefore, an enemy may be in the same position as the player between frames (so to speak).
Checking the "distance" instead is a starting point for an actual solution.
I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?
Yep, add the function "distance" in there and it should work.
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 11:15 am
by BluBillz
ivan wrote:Keep in mind that when you compare numbers like so:
is not robust because enemies move at discrete steps (speed*dt).
Therefore, an enemy may be in the same position as the player between frames (so to speak).
Checking the "distance" instead is a starting point for an actual solution.
I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?
Yep, add the function "distance" in there and it should work.
i did this...in love.update(dt)
Code: Select all
for k,enemy in pairs(enemies) do
distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
end
for i=#enemies, 1,-1 do
if distance then
table.remove(enemies.i)
end
end
But I get error...saying: bad argument #1 to 'remove' (table expected, got nil)
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 12:00 pm
by s-ol
BluBillz wrote:ivan wrote:Keep in mind that when you compare numbers like so:
is not robust because enemies move at discrete steps (speed*dt).
Therefore, an enemy may be in the same position as the player between frames (so to speak).
Checking the "distance" instead is a starting point for an actual solution.
I know a distance formula would be more appealing...but at the moment it still doesn't even remove itself from the game..why would this be?
Yep, add the function "distance" in there and it should work.
i did this...in love.update(dt)
Code: Select all
for k,enemy in pairs(enemies) do
distance = math.sqrt((player.x - enemy.x) ^ 2 + (enemy.y - player.y) ^ 2)
end
for i=#enemies, 1,-1 do
if distance then
table.remove(enemies.i)
end
end
But I get error...saying: bad argument #1 to 'remove' (table expected, got nil)
You put a dot where there should be a comma. Just look at the line, love tells you where the problem is (again, table.remove rakes two arguments, that's why you need to have a comma there.
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 12:04 pm
by BluBillz
You put a dot where there should be a comma. Just look at the line, love tells you where the problem is (again, table.remove rakes two arguments, that's why you need to have a comma there.
Well i did fix that...but all it does now is the enemies spawn instantly...then delete instantly...Not getting the players x and y positions..why would this be?
Re: table.remove wont "Remove" table
Posted: Wed Mar 11, 2015 1:20 pm
by s-ol
BluBillz wrote:You put a dot where there should be a comma. Just look at the line, love tells you where the problem is (again, table.remove rakes two arguments, that's why you need to have a comma there.
Well i did fix that...but all it does now is the enemies spawn instantly...then delete instantly...Not getting the players x and y positions..why would this be?
Because "or" doesn't work the way you intend to use it. "Or" is a logical operator, so your if basically means "if player and enemy are at the same x coordinate or player.y exists" which is always true. Someone else posted a solution above and hinted at instead using the distance (because enemies rarely exactly hit your exact spot)