Shooting enemies

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
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Shooting enemies

Post 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
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Shooting enemies

Post 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.
Kurosuke needs beta testers
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Shooting enemies

Post by Ellohir »

Yeah, insert them in a table so you can show some nice explosions before removing them :crazy:
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Shooting enemies

Post 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
waraiotoko
Prole
Posts: 33
Joined: Thu Oct 06, 2011 6:08 pm

Re: Shooting enemies

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Shooting enemies

Post 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.
Last edited by tentus on Tue Feb 07, 2012 7:18 pm, edited 2 times in total.
Kurosuke needs beta testers
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Shooting enemies

Post by coffee »

Ellohir wrote:Yeah, insert them in a table so you can show some nice explosions before removing them :crazy:
or don't remove them if you want them to be show them dead on the floor or can be risen again as zombies :D
waraiotoko
Prole
Posts: 33
Joined: Thu Oct 06, 2011 6:08 pm

Re: Shooting enemies

Post by waraiotoko »

coffee wrote:
Ellohir wrote:Yeah, insert them in a table so you can show some nice explosions before removing them :crazy:
or don't remove them if you want them to be show them dead on the floor or can be risen again as zombies :D
I never delete enemies, you could potentially resurrect them even after re-starting the game and re-loading your save. :D

Also, the ghosts from MGS3 come to mind.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests