Best way to destroy a object

Hi

I look at some code on labs.phaser.io , especially on bullet or physics section.

They use

object.setActive(false).setVisible(false);

Instead of

object.destroy();

To destroy a bullet object. but, it is that actually destroy an object ?

Imagine there are thousand of bullet object that are not actually destroyed, is this can impact game performance ?

I’m not a javascript or phaser expert, so, i want to know about this.

Thanks

2 Likes

There is, as you have guessed, a rather large difference between destroying a Game Object and making it inactive and invisible. Destroying it removes all of its references to the game’s systems, which allows the garbage collector to reclaim the memory it occupied when that becomes necessary. An object which is hidden still exists and can be activated at any time.

If you constantly create Game Objects and you remove them by making them invisible, they will stay on the display list, which could have slight performance implications if there are a lot of them. They will still take up memory, which can quickly become a problem. If you are sure you don’t need a Game Object for any purpose, you should absolutely destroy it completely.

Hiding and deactivating Game Objects could also have advantages, however. If you often have to create and destroy an object (as is the case with a bullet, for instance), it is much better to make a pool of Game Objects - that is, you make them invisible to “destroy” them, then simply reposition and reuse them when you next need them. This is not more complicated than destroying the object and later recreating it anew (depending on how complex the object is, creating it might actually be harder than reusing it) and avoids creating garbage. A destroyed Game Object will still take up memory until the garbage collector removes it - this is an unpredictable process which will often freeze the game until it completes. The less garbage you create, the less likely it is for this to happen, which is why pooling is a rather important concept.

The distinction between destroying and hiding a Game Object is an important one, but do not prematurely optimize your code. Generally, the best way to handle a case like this is to use the most obvious solution, then optimize it later if you determine that it’s necessary. For the purposes of a beginner, destroying a Game Object is easy and predictable (as long as you’re careful not to use destroyed Game Objects, of course), so I recommend against trying to use patterns you don’t yet understand.

12 Likes

Wow, thank you for your explanations !

Hello!
(It has been quite a time since the last reply, I hope I’m not violating the forum rules when I reactivate the topic…)
Telinc1, would you recommend to destroy all objects before a scene restart? As I understand the game, a restart will call create again and so all groups of pickups, monsters, a.s.o. will be re-created and the old ones are not needed anymore.
Christof

Game objects on the scene display list or update list are destroyed automatically when a scene is stopped.

1 Like

Thanks for the quick answer!