Jump to content

Real, Impactful Destruction of Game Objects


frankdev
 Share

Recommended Posts

Object is garbage collected (memory released) when there is no reference to that object left. Removing object from array, or setting it to null, should work as long as you have no more references to that object elsewhere. Check if you are not polluting those objects to window scope. 

Link to comment
Share on other sites

Well, how would you use object that was deleted? If you can still use object, then it wasn't deleted. If you can use object, it means that you have reference to it. "delete" statement is deleting only a reference, not object itself.

Link to comment
Share on other sites

.. Wait, what? (to the two last posts, frankdev and Chris )

 

There is a few different ways of doing it, and I don't want to impose anyone on you, since it may not fit the way you are doing things. It may be good if you explain us how things work in your game, give us more information, maybe post some code. Recycling is a good Idea, but it is a bit complicated to implement, especially for a beginner. Now, the real danger of just deleting objects from the array, or setting them to null is that that leaves holes in the array and it always continues to grow. The simplest way to nicely delete objects is by splicing them, since it closes back the hole, but again I remind you that it depends on the way you do stuff. To be able to splice an object, you need to know its index in the array. Then, you can do array.splice(index, 1); to remove it from the array. If you do that in a for-loop, then don't forget to also do i--;

 

Here is the simplest way to do it.

 

 

// considering that bullets is an array of bulletsfunction updateBullets(){  for(var i=0; i<bullets.length; i++){    bullets[i].update();    if(bullets[i].colliding() || bullets[i].offBounds() ){      bullets.splice(i,1);    i--;    }  }} 

 

 

This works, but an issue with it would be that no two bullets would be able to collide with eachother (since only one would be destroyed before the other would collide)

A solution to this would be to make each bullet ( and spaceship) have a property named "alive" or something along those lines that is set to false when it has collided, and then have a function to remove the dead objects at the end of each game loop. (which makes a garbage collector inside a garbage collector: collectorception). Yet again, depending on how your game loop works, you may get weird situations where spaceships don't always collide with bullets. The solution would be to first move/update bullets and spaceships; then to test collisions and destroy the right objects. (and destroying objects by pairs (using splice of course))

 

I am not saying this is the best way to do things, nor that it is the way that would fit your needs best. Recycling is definitely better, but it takes a lot of nerve to integrate. (I'm impressed none made a library for it by now)

 

also a note: You probably know this, but if the object is in an array, and you want to set the object in the array to null, you have to set it according to the array:

 

myArray[5]= {x:4,y:6};//Wrong:var myObject=myArray[5];myObject=null;console.log(myArray[5]); // This will still give you {x:4,y:6}//Right:myArray[5]=null;console.log(myArray[5]) //  This will give you null//Of course I don't recommend this for deleting game entities. Use splice, or something more advanced like recycling.
Link to comment
Share on other sites

  • 3 weeks later...

The object pool concept is probably the best way to go forward if you want to maintain a consistent FPS, as you are not going to be dealing with uncontrollable GC triggering and memory churn when deleting/nulling objects. The downside is that it will take a slight hit on startup to initialise everything.. 

Link to comment
Share on other sites

One thing to keep in mind is that if you're using some sort library and creating these objects in the library, its hard to tell if there is some internal list being maintained that is keeping your references alive. In that case you'd need to be careful to use whatever hooks the library provides for destroying objects rather than just removing the reference from the array. For example, in Quintus everything is part of a stage. Even if you have none of your own references to objects, once you've inserted something into the stage then thats going to hold on to the reference. In Quintus it still gets rid of it correctly if you use destroy() of course.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...