Author Topic: Dozens of enemies rendering, FPS drops drastically when a few are killed (2.5)  (Read 1639 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
I can have dozens (I've even tried hundreds) of zombies on-screen and they render and perform just fine. When I kill a few, however, the FPS drops drastically. I had 100+ on screen once and the FPS was ~50. I killed 3, fps dropped to ~10. I have no clue what could be causing this, as I've tried commenting out a lot of code that I thought could be the culprit (namely the random number generations and z-index sorting while the zombie is dead). It did help the FPS, but only by like 5 or 6 FPS. I've also commented out some of the shooting code, basically trimming the code to bear-bones point, click, and shoot and the zombie simply dying via kill(). It didn't help :/.

Here is my repo:
https://github.com/NateTheGreatt/Coffee-Beans

I don't have my flixel framework in the repo, I plan on putting it in soon.

Anyways, could anyone provide any insight on what's making the FPS drop so drastically? I'm using Flixel 2.5 if that's any help
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

bpercevic

  • Member
  • **
  • Posts: 87
  • Karma: +2/-0
    • View Profile
can you post the swf?

legnox

  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
the swf is located in the BIN folder of the source :P

bpercevic

  • Member
  • **
  • Posts: 87
  • Karma: +2/-0
    • View Profile
wow, lol, i looked earlier and somehow missed it. 

I think its because where you override kill() in zombie, it never actually gets killed because you didn't call super.kill().  even though you marked it dead, its still not set to be inactive, and is therefore I think still in memory.  so the more zombies you have the slower it goes.  try adding super.kill() to the bottom of override kill. 
« Last Edit: Fri, Jul 29, 2011 by percevic »

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
wow, lol, i looked earlier and somehow missed it. 

I think its because where you override kill() in zombie, it never actually gets killed because you didn't call super.kill().  even though you marked it dead, its still not set to be inactive, and is therefore I think still in memory.  so the more zombies you have the slower it goes.  try adding super.kill() to the bottom of override kill.

That doesn't make too much sense. Yes it's still in memory when it's dead, but it was in memory when it was alive, so it shouldn't perform worse just because it's dead and not solid. Also, he does a super.kill after it fades out (but it doesn't appear like that ever happens, or something).

However, this was bothering me, so I looked deeper into it. It has nothing to do with your zombies. For example, aim at an area with no zombies and fire for 20 seconds straight. Every time you shoot you are creating a bullet, so over time the number of bullets builds up creating the slow down. The reason it seemed to happen when killing zombies is because you shoot to kill them.

You need to make sure you kill bullets that go off screen, and make sure all bullets that are killed are truly cleaned up (or reused). There's lots of optimization to be had, but that seems to be the one that is killing you. Collision and overlaps are expensive, so having lots of bullets makes those calls take a ton of time.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
That doesn't make too much sense. Yes it's still in memory when it's dead, but it was in memory when it was alive, so it shouldn't perform worse just because it's dead and not solid. Also, he does a super.kill after it fades out (but it doesn't appear like that ever happens, or something).

However, this was bothering me, so I looked deeper into it. It has nothing to do with your zombies. For example, aim at an area with no zombies and fire for 20 seconds straight. Every time you shoot you are creating a bullet, so over time the number of bullets builds up creating the slow down. The reason it seemed to happen when killing zombies is because you shoot to kill them.

You need to make sure you kill bullets that go off screen, and make sure all bullets that are killed are truly cleaned up (or reused). There's lots of optimization to be had, but that seems to be the one that is killing you. Collision and overlaps are expensive, so having lots of bullets makes those calls take a ton of time.

I did optimize my bullet code a little bit. I made it so it doesn't create a new sprite for every iteration and instead just moves one sprite down the line. That should have boosted performance the most but it made only a small fps difference of 4 or 5. Bullets don't go out of the camera's X/Y and are killed at the end of the iterations. I think >.>

I'm going to try to rewrite my bullet code, anyways. Any suggestions on how I could do it better? I really want to do a raycast but I'm not sure how to check collision of drawn line against an FlxSprite :S
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

legnox

  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Have you checked out PhotonStorms gun plugin? it's pretty nifty.

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
I did optimize my bullet code a little bit. I made it so it doesn't create a new sprite for every iteration and instead just moves one sprite down the line. That should have boosted performance the most but it made only a small fps difference of 4 or 5. Bullets don't go out of the camera's X/Y and are killed at the end of the iterations. I think >.>

I'm going to try to rewrite my bullet code, anyways. Any suggestions on how I could do it better? I really want to do a raycast but I'm not sure how to check collision of drawn line against an FlxSprite :S

When you press ~ and look at the console, the U is the number of updates being called per frame, and D is the number of draws, and as you shoot, you can see the number of updates just keeps going up, so they must not be getting killed.

There should be some good tutorials on google about colliding in different ways. I can't imagine colliding against a line wouldn't have any tutorials. I've never actually done it, but I would assume you could get the equation of the line, and for each enemy's x, you can find out the corresponding y on the line, and check if that y is within the bounding box. Though there's probably better ways.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
When you press ~ and look at the console, the U is the number of updates being called per frame, and D is the number of draws, and as you shoot, you can see the number of updates just keeps going up, so they must not be getting killed.

There should be some good tutorials on google about colliding in different ways. I can't imagine colliding against a line wouldn't have any tutorials. I've never actually done it, but I would assume you could get the equation of the line, and for each enemy's x, you can find out the corresponding y on the line, and check if that y is within the bounding box. Though there's probably better ways.

Ah, I had no idea what the U and D meant. Thank you! That is extremely helpful. I'm rewriting my engine of current, it was too spaghetti anyway.
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery