for loop to delete single entity just deletes everything in the table instead
Posted: Fri Oct 23, 2020 7:00 am
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.
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.
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
I attached the .love file if you want to see what I mean or look at more of the code to check it.