Jump to content

  • Log In with Google      Sign In   
  • Create Account

Enemy is dead, now what?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 menyo   Members   -  Reputation: 494

Like
0Likes
Like

Posted 10 October 2012 - 12:03 PM

So i tried looking everywhere for a solid answer for this but there is a lot of contradiction and since i have my own possibly wrong thoughts makes this very confusing.

This pretty much goes for any object i dont use anymore but let's take the enemy object. What should i do exactly if health drops below 0?

Obviously i can put it out of my list, it does not get updated or drawn anymore, but is it still in memory?

Some say this is enough, i doubt this.

Some say i have to null it so the garbage collector takes it on its next run.

Some say i have to make it inherit IDisposable and dispose of it manually.

So what is a good way to handle objects that you wont be using anymore?

Current Project: TechnoFlux read all about it on my

DEV BLOG


Sponsor:

#2 caldiar   Members   -  Reputation: 702

Like
1Likes
Like

Posted 10 October 2012 - 12:39 PM

Allocating/Deallocating memory is a pretty expensive thing to do. However, with C# the garbage collector is going to decide what to end up doing with your memory once there are no longer any references to it. It still isn't a good idea to give the garbage collector any excuse to start up and begin sifting through your 'trash'.

Ideally, instead of 'deleting' the enemy once it's dead, have your enemies have a bool variable saying whether they're active or not. If the enemy is dead, it isn't active. If the enemy isn't active then you can skip physics, rendering, etc... on that particular enemy during your update loop.

Then, when you want to spawn new enemies, you can flag the deactivated enemy as active again and re-initialize the enemy to default stats (back to full health, etc...).

It's a bit of a balancing act though since it's not really practical to have everything in your game pre-allocated and always sitting in memory. If you're going to be creating/deleting something quite often, however, you should focus on keeping that in memory as long as possible otherwise you're going to be making your garbage collector work overtime.

#3 greenvertex   Members   -  Reputation: 510

Like
1Likes
Like

Posted 10 October 2012 - 12:44 PM

Basically, remove all reference to the object you wish to disappear. It will then do so... eventually.

See Garbage Collector Basics.

For something like enemies, however, you might want to consider a different solution. I don't mean to add to the pile of answers you've already gotten but I'm going to go ahead and add to the pile of answers you've already gotten:

If you find yourself creating and destroying enemies pretty rapidly (conceptually not hard to imagine on my part). Consider creating a pool of enemies of some large-enough size to hold all the enemies that can be on the screen at one time + some margin of error. Then, any time you need an enemy, grab something from that pool, initialize it as a new enemy, and use it until you no longer need it. Once it is killed, return it to the enemy pool to be used again. These don't depend on the garbage collector so you don't find yourself in this strange position of wondering if they're "truly" dead. They're not, but that ends up being a good thing.

#4 menyo   Members   -  Reputation: 494

Like
0Likes
Like

Posted 10 October 2012 - 01:04 PM

Ok great, so essentially once i create an enemy and only reference that from a list it does get cleaned up eventually once kicked out of the list.

Very good idea to keep a list as a pool, just flag a bool for the status of the enemy and stop interacting with those. Once a new enemy gets created i use one of the free spots or one with the dead flag.

Thanks for the link, i'll read it. I believe Shawn has created one in his blog too, guess i am going to read that too.

Current Project: TechnoFlux read all about it on my

DEV BLOG





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS