0
votes

So I'm playing around with a tutorial script, and I have a turret added that follows the player's movement. This turret should be shooting at the player every few seconds. Before I added collisions or timed shots or anything like that, I found out that the bullet shots actually aren't being shown. So I set it to a key press just to test, and nope, not working.

Full Code

Anything you see wrong? NOTE: I don't get an error when I run it, the bullets just don't show up!

1
I don't know if this is related but your shot removal code is not working the way you expect. You are removing entries from enemyShots based on their original indices but every time you call table.remove the indices are recalculated. See eval.in/282084 for an example of the problem. - Etan Reisner
@EtanReisner I added a few things to combat that I think, link Maybe this will help but I got tips for the bullets here: link - TabloidA
That link is wrong. At least one user comments to the same effect on that link as well. You cannot remove from a table like that. It does not work. You generally traverse the table backwards (to avoid the reindexing problems) or find some other way to do the same sort of work. - Etan Reisner
Hah, actually the author of the code discusses that but still gets it wrong despite that. - Etan Reisner
How would you recommend going about doing it then? @EtanReisner - TabloidA

1 Answers

0
votes

love.keyreleased(key) treats the shift key as a separate and independent key so "X" will never happen. Instead use "x" to trigger a shot.

function love.keyreleased(key)
    if key == 'x' then
        shoot()
    end
end

Once that is fixed you will have issues in your collision detection:

  • player is not being used as an array as line 52 for ii, vv in ipairs(player) do suggests.

  • There is nothing stopping a bullet from having a negative velocity and hitting a player or a player being hit by more than one bullet and so they may be added to the remEnemy and remShot table multiple times which will mess up deleting later.

  • The index shifts that happen when you delete an entry (mentioned by Etan Reisner) also exist.

For your use case, marking an object to be removed by adding it to a set seems like the best approach to me:

local remShots = {}

then if there is a collision or the bullet has negative velocity then

remShots[i] = true

and to remove, iterate in reverse to avoid shifted indices.

for i=#enemyShots,1,-1 do
  if remShots[i] then
    table.remove(enemyShots, i)
  end
end

Performance

A table.remove shifts all the elements above the index down to fill the gap. In the code here the loop removes multiple elements so they are shifted more than once, but as long as your tables aren't huge, this removal process is fine.

I'm note sure what you were going for with the first bullet point, but you can also use the remPlayer to avoid checking for collisions with players that have already been marked for removal when you get that worked out:

if not remPlayer[ii] and CheckCollision...