- Home /
Procedural platformer: add/remove or pooling for platforms?
I'm trying to make a mobile 2d game when player endlessly jumps up onto procedurally generated platforms. I'm new to Unity and I'm worrying that I might be crating some memory leak situation here. Since this type of game it pretty common, I guess this was already solved multiple times. I create new platforms above the screen area each time player jumps higher up. When screen moves up, you can't go back, falling down is death. So what should I do with platforms that are below the screen area and won't be used?
Remove them?
Deactivate them?
Reuse them?
It's not a bullet hell game when there's hundreds of objects being created and destroyed every second. More like 20-30 objects every few seconds. Should I worry about efficiency? Can it clog the memory? Looking for answers from people who have some experience with this kind of games.
UPD: And actually, I'd also wanted to ask same thing about any temporary sprites I put in the scene. Like, when player jumps or lands, there's a small dust cloud animation being added just above the ground and is being removed when it finishes. Shout it be reused too?
Answer by tanoshimi · Apr 16, 2016 at 07:13 AM
Instantiating objects requires memory allocation. Destroying them releases the memory, but requires garbage collection. This happens automatically, but It's this process of garbage collection that can causes spikes/lags in your game's performance, which is generally undesirable.
Pooling instantiates objects as they are required (or, sometimes, pre-creates a set of objects as the game is initialised), but doesn't destroy them when they are no longer needed - it just deactivates them and returns them to the pool ready to be used again. So your average memory usage is higher using this method, because all objects that have been placed in the pool remain in memory even when they are not being used, but you avoid the spikes when objects are instantiated or destroyed, since deactivating/re-enabling an object from the pool is a trivial operation that requires no memory management.
Pooling requires a little bit more code infrastructure, but there's plenty of good examples on the net. Which is best for your situation? Try both and use the profiler.
Thanks for the explanation! I don't want to have my game freezing or skipping frames so I'll better implement pooling. But I'll also try just creating objects as an experiment. I need to find out just how bat it gets.
Answer by AnatolyDream · Apr 16, 2016 at 12:51 PM
"When screen moves up, you can't go back, falling down is death. So what should I do with platforms that are below the screen area and won't be used?"
Why not use them again? Since it's not a slow paced platformer, I would go with object pooling. Plus, if you don't have a lot of platforms that differ from each other too much then you can just reposition those that were already used before.
Something like this pseudo code:
If platform is down behind the screen - deactivate it.
Go through your object pool and check for deactivated platforms.
Use any of them to randomly position it behind the screen above your character.
Your answer
Follow this Question
Related Questions
Help with simple issue (hopefully) 1 Answer
Trying to destroy a projectile and "teleport" to another area of the screen when it hits a target. 1 Answer
snapping objects at runtime 2 Answers
Pooling object performs the same or worse than instantiate/destroy? 1 Answer
Can't Destroy Instantiated Prefab 2 Answers