frankdev Posted July 10, 2013 Share Posted July 10, 2013 How would one go about destroying a specific entity in an array? I have tried destroy, I have tried setting the object equal to null, but nothing has helped. After about 400 enemy ships spawn the game just lags. Any advice? Quote Link to comment Share on other sites More sharing options...
xerver Posted July 10, 2013 Share Posted July 10, 2013 I think we need a whole lot more context than what you are giving us. Quote Link to comment Share on other sites More sharing options...
Quetzacotl Posted July 10, 2013 Share Posted July 10, 2013 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. Quote Link to comment Share on other sites More sharing options...
frankdev Posted July 10, 2013 Author Share Posted July 10, 2013 Quetzacotl, I am looping through a for loop...it is an array of bullets. Could that be the problem, that I am still using those objects after they were deleted? Quote Link to comment Share on other sites More sharing options...
Quetzacotl Posted July 10, 2013 Share Posted July 10, 2013 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. Quote Link to comment Share on other sites More sharing options...
Chris Posted July 10, 2013 Share Posted July 10, 2013 If its in an array, have you already tried the arrays splice() method to remove the object from it? Quote Link to comment Share on other sites More sharing options...
rich Posted July 10, 2013 Share Posted July 10, 2013 Try recycling it? Create a bullet pool and kill the bullet when it's off-screen (or whatever destroys it in game) and then when you fire a new bullet revive the first 'dead' one in the pool. Then you only ever need as many bullet objects as you can fire at any one time in your game. P.Uri.Tanner 1 Quote Link to comment Share on other sites More sharing options...
frankdev Posted July 11, 2013 Author Share Posted July 11, 2013 Yeah. I think recycling is the only option; since every object is identical splicing it erases the whole array. Thanks! Quote Link to comment Share on other sites More sharing options...
Chris Posted July 11, 2013 Share Posted July 11, 2013 If every object in your array is _really_ identical, then it would be enough keeping only one of them Quote Link to comment Share on other sites More sharing options...
Paul-Andre Posted July 16, 2013 Share Posted July 16, 2013 .. 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. Quote Link to comment Share on other sites More sharing options...
dazld Posted August 5, 2013 Share Posted August 5, 2013 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.. Quote Link to comment Share on other sites More sharing options...
TheHermit Posted August 5, 2013 Share Posted August 5, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.