for loop to delete single entity just deletes everything in the table instead

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.
Post Reply
HabitualVitality
Prole
Posts: 2
Joined: Sat Feb 29, 2020 3:43 am

for loop to delete single entity just deletes everything in the table instead

Post by HabitualVitality »

The purpose of this for loop is to check when one of the entities (they're just squares that spawn on the left/right and then move to the other side) has fulfilled it's purpose of making it to the opposite side they spawned, and if so; delete them.

Code: Select all

  --make loop go backwards so it doesn't error when one cube gets deleted
  for i = #evilCubes, 1, -1 do
    local cube = evilCubes[i]
    cube.xPos = cube.xPos + (cube.Speed*dt)--this is just how im moving my cubes

    --now I check if the cube has moved outside of the screen depending on which direction it started.
    if cube.Speed > 0 then--cube is moving to the right
      if cube.xPos >= cube.xDelete then--check if cube has left the screen
        table.remove(evilCubes,cube[i])--delete if it has
      end
    else--cube is moving to the left
      if cube.xPos <= cube.xDelete then--check if cube has left the screen
        table.remove(evilCubes,cube[i])--delete if it has
      end
    end

  end
Now whenever I delete the cube using table.remove what happens I find very strange. The cubes will move to the other side of the screen like they are supposed to, but once one gets there instead of just deleting it the loop seems to just delete every single cube on the screen. Now what I also find strange is sometimes it deletes all but like 2 or 3 cubes randomly.

I attached the .love file if you want to see what I mean or look at more of the code to check it.
HungryCubes.love
(1.05 KiB) Downloaded 141 times
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: for loop to delete single entity just deletes everything in the table instead

Post by MrFariator »

The problem has to do with how you're using table.remove. The said function takes a table, and an index, so the code should look more like:

Code: Select all

if cube.Speed > 0 then
  if cube.xPos >= cube.xDelete then
    table.remove(evilCubes, i) -- note the 'i' in place 'cube[i]'
  end
else
  if cube.xPos <= cube.xDelete then
    table.remove(evilCubes, i) 
  end
end
Without digging deeper into your code, presumably "cube[ i ]" doesn't point to any value at all, number or otherwise. When you call table.remove without a second argument (either passing nil or nothing at all), it will just remove the last entry from the table, and as such you get a situation where an evil cube is removing an entry from the table every frame until it manages to remove itself.
HabitualVitality
Prole
Posts: 2
Joined: Sat Feb 29, 2020 3:43 am

Re: for loop to delete single entity just deletes everything in the table instead

Post by HabitualVitality »

MrFariator wrote: Fri Oct 23, 2020 10:20 am Without digging deeper into your code, presumably "cube[ i ]" doesn't point to any value at all, number or otherwise. When you call table.remove without a second argument (either passing nil or nothing at all), it will just remove the last entry from the table, and as such you get a situation where an evil cube is removing an entry from the table every frame until it manages to remove itself.
For some reason it took hours for my post to actually show up and in that time I did actually manage to figure it out, but I still really appreciate the reply thank you.
Post Reply

Who is online

Users browsing this forum: No registered users and 231 guests