Jump to content

Effiency of reusing game objects


4gent0rang3
 Share

Recommended Posts

Hey so I'm creating a procedural platformer and I don't have much time to test this so I'll ask here :)

Say I have a PlatformManager to automatically create and destroy platforms in the game world. Would it be more efficient to, when a platform is destroyed, add it to a 'destroyedPlatforms' list and then when creating a new platform just take one of the destroyed platforms and modify its properties to become a new platform, or should I just create a new platform object every time I need a platform and destroy platforms when they are destroyed (let garbage collection clean them up)? In essence I'm asking if its would be more efficient to reuse and modify an old object or simply create a new one?

Also on a side note, would it be a good idea to make the PlatformManager object an extended Phaser.Group object? (I'm using typescript for this project)

PS: If this doesn't relate to Phaser specifically feel free to move this thread :)

Link to comment
Share on other sites

Impossible to say without testing it but the general consensus is that using object pooling and reusing old platforms is more efficient than relying on GC and creating new ones each time, the downside being this is slightly more difficult to manage. In some situations (many probably) the efficiency gain from object pooling could be extremely minimal but dealing with stale state (as you have to do to reuse old objects) is always a potential coding hazard waiting to happen so pros/cons to each approach.

Link to comment
Share on other sites

For games targeting mobile devices you generally want to try to keep the quantity of garbage generated for collection at an absolute minimum, otherwise you can end up with your render frame rate seizing up momentarily while the GC does its sweep. This is going to apply particularly heavily to action games like platformers where any glitches in scrolling will be particularly noticeable. So if you can't create your whole map up front and have to do so on the fly during playback of the level then using a pool definitely does sound like a good idea if you want to achieve optimum mobile performance.

Link to comment
Share on other sites

I agree with mattystyles. It depends on how many platforms you are using and how you would use them. I doubt you would see performance problems if you just use .destroy() to kill a platform, but I do remember watching a flash game tutorial from Lee brimelow about object pooling so yes, that is a thing.

Also, I wouldn't really have an array called "destroyedPlatforms" because your destroyed platforms would go back into a pool to be used on the screen again. Instead, I would have one array named "platformPool" and one named "platformsInGame" or something like that.

Link to comment
Share on other sites

I'm with @mattstyles and @Jimaginary in that pooling is the general practice that should be employed in development. It's better, in case you ever need to scale up the game too.

But that being said, I'm yet to see in my projects using Phaser a time just destroying objects has actually affected performance.. So I stick with the easier method of lots of create and destroy because it's efficient enough. Testament to the brilliant minds that are crafting the engine no doubt.

Link to comment
Share on other sites

Isn't as easy as creating a group and let Phaser keep track of the alive state of your group?

 

this.currentPlatform = this.platformPool.getFirstDead();
            
            if (!this.currentPlatform) {
                this.currentPlatform = new MrHop.Platform(this.game,  nextPlatformData.numTiles, this.game.world.width + nextPlatformData.separation, nextPlatformData.y, this.levelSpeed, this.floorPool, this.coinsPool);
            } else {
                this.currentPlatform.prepare(nextPlatformData.numTiles, this.game.world.width + nextPlatformData.separation, nextPlatformData.y, this.levelSpeed);
            }
            this.platformPool.add(this.currentPlatform);

http://jsbin.com/tufisagoho/edit?html,js,output

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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