Page 1 of 1
Shooting enemies
Posted: Tue Feb 07, 2012 11:10 am
by veethree
So i've been working on a game based on my rough and unfinished
Space game. The idea is almost the same, Except this one will have enemies, shooting and all that stuff. My problem is with the enemies. Right now i can spawn the enemies (press y ingame) but they don't do anything, I wanted to make sure i can de-spawn them before i start writing some sort of AI for them, And that's where the problem is. If i spawn 2 enemies, then shoot (space ingame) the first one i spawned, The game crashes. If i however shoot the 2nd one first, then the first one, It's all good..So yeah..that's where you guys come in..any help is appreciated.
Here's the code that deletes the enemies so you don't have to look for it:
Code: Select all
for i=1, #enemies do
for c=1, #bullets do
if checkcollision(bullets[c].x, bullets[c].y, 10, enemies[i].x, enemies[i].y, 50) then
table.remove(enemies, i)
end
end
end
That's in enemy.lua
The code is pretty messed up right now, Plus a bunch of bugs..Any suggestion regarding that are welcome too..
also, Don't worry about the ship being referred to as "car" in the code..the idea started as a car game. lol
Re: Shooting enemies
Posted: Tue Feb 07, 2012 1:50 pm
by tentus
Well, to start with, I could not recommend altering a table that you are looping through. If you insist on doing so, at the very least you need to loop backwards. A better solution would probably to loop through the enemies and then bullets, and when they collide, add that enemy to a "to be deleted" list. Then go through a new loop and delete the marked enemies.
Re: Shooting enemies
Posted: Tue Feb 07, 2012 2:53 pm
by Ellohir
Yeah, insert them in a table so you can show some nice explosions before removing them
Re: Shooting enemies
Posted: Tue Feb 07, 2012 3:14 pm
by veethree
tentus wrote:Well, to start with, I could not recommend altering a table that you are looping through. If you insist on doing so, at the very least you need to loop backwards. A better solution would probably to loop through the enemies and then bullets, and when they collide, add that enemy to a "to be deleted" list. Then go through a new loop and delete the marked enemies.
Wouldn't i have to alter the table which i'm looping through anyway? I mean with that i still have to remove the entry in the enemies table, i'd just do it with a different loop, Isn't that basically the same?
I did manage to get it to work though, Haven't fully tested it but it seems to work allright so far. I just added a boolean to the enemies called "isdead", and in the collision loop if a collision is detected it sets the isdead variable for the said enemy to true, And a different loop is running through the table checking for isdead's set to true and if it finds one it removes it.
Code: Select all
-- Enemy - bullet collisions
for i=1, #enemies do
for c=1, #bullets do
if checkcollision(bullets[c].x, bullets[c].y, 10, enemies[i].x, enemies[i].y, 50) then
enemies[i].isdead = true
end
end
end
-- enemy removal
for i=1, #enemies do
if enemies[i].isdead == true then
table.remove(enemies, i)
break
end
end
Re: Shooting enemies
Posted: Tue Feb 07, 2012 6:48 pm
by waraiotoko
If you spawn many enemies and kill them rapidly your game crashes (cannot index enemy table), since you edit the table you're looping through.
Re: Shooting enemies
Posted: Tue Feb 07, 2012 7:11 pm
by tentus
You still need to loop backwards through the removal process if you want to do it that way.
Code: Select all
-- enemy removal
for i=#enemies, 1, -1 do
if enemies[i].isdead == true then
table.remove(enemies, i)
break
end
end
I would recommend making a list (a local variable) like so:
Code: Select all
local isdead = {}
-- Enemy - bullet collisions
for i=1, #enemies do
for c=1, #bullets do
if checkcollision(bullets[c].x, bullets[c].y, 10, enemies[i].x, enemies[i].y, 50) then
isdead[#isdead + 1] = i
end
end
end
And then you can use the list to remove enemies.
Re: Shooting enemies
Posted: Tue Feb 07, 2012 7:13 pm
by coffee
Ellohir wrote:Yeah, insert them in a table so you can show some nice explosions before removing them
or don't remove them if you want them to be show them dead on the floor or can be risen again as zombies
Re: Shooting enemies
Posted: Tue Feb 07, 2012 7:15 pm
by waraiotoko
coffee wrote:Ellohir wrote:Yeah, insert them in a table so you can show some nice explosions before removing them
or don't remove them if you want them to be show them dead on the floor or can be risen again as zombies
I never delete enemies, you could potentially resurrect them even after re-starting the game and re-loading your save.
Also, the ghosts from MGS3 come to mind.