Creating new objects and game freeze issues

Topics: User Forum
Mar 8, 2010 at 3:22 AM

My game has objects that will spawn four other objects.  Then, when it is time to spawn again, all of the objects spawn new objects.  There is a player controlled avatar that can shoot these objects. 

My problem occurs when there are too many objects on the screen.  The game goes from running fine one moment to freezing up and needing to be killed.  This is my first time using Farseer and I'd appreciate any thoughts you might have on what I might do to fix this.  I don't know what other elements are relevant, so if you need to know something about my setup or settings, just ask.

Mar 8, 2010 at 5:41 AM
Edited Mar 8, 2010 at 5:43 AM

Are you running on the windows or xbox?

It might be a garbage collection issue. Try making a pool of enemies and create them just once. If you need one, just fetch one from the pool, once the enemy is dead, call a method that resets it and put it back in to the pool. If the enemies are different, have different pools. Farseer has a pool class if you need it.

Edit: Btw, how many object are exactly on the screen and did you narrow the problem down to Farseer?

Good luck.

Mar 9, 2010 at 3:12 AM

I am running this on Windows.

I like your idea about creating a poll and getting the objects from there.  I'll give that a shot.

There are quite a few items on the screen when it happens.  Guessing at maybe a hundred.  And, I am not sure that the problem is Farseer.  The only info I have is that I was creating these objects in a stationary way before and decided it would be a better game if they could move.  Once I ported them into Farseer is when I saw the problem.

Mar 9, 2010 at 3:30 AM

I tried the pool, but so no results, so I printed the count to the screen to get an exact value at when I started noticing the issue.

The problem is, the enemies spawn at an exponential rate.  Each one creates four more when it is time to spawn.  I had overlooked how quickly this can become unwieldy.  The problem occurs not at 100, but right around 3000!  Once I get the game working the way I'd like, I don't think I'll ever really need to be at that high a count.