table.remove wont "Remove" table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

table.remove wont "Remove" table

Post 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?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: table.remove wont "Remove" table

Post by ivan »

"table.remove" is used for numerically-indexed tables and works like so:

Code: Select all

table.remove(list, index)
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
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Re: table.remove wont "Remove" table

Post by BluBillz »

ivan wrote:"table.remove" is used for numerically-indexed tables and works like so:

Code: Select all

table.remove(list, index)
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..
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: table.remove wont "Remove" table

Post 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
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Re: table.remove wont "Remove" table

Post 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?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: table.remove wont "Remove" table

Post by ivan »

Keep in mind that when you compare numbers like so:

Code: Select all

pt.x == pt2.x and pt.y == pt2.y
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.
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Re: table.remove wont "Remove" table

Post by BluBillz »

ivan wrote:Keep in mind that when you compare numbers like so:

Code: Select all

pt.x == pt2.x and pt.y == pt2.y
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)

:?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: table.remove wont "Remove" table

Post by s-ol »

BluBillz wrote:
ivan wrote:Keep in mind that when you compare numbers like so:

Code: Select all

pt.x == pt2.x and pt.y == pt2.y
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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Re: table.remove wont "Remove" table

Post 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?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: table.remove wont "Remove" table

Post 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)

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 62 guests