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.
--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.
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.
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.