Object Pooling: 1 Simple Technique To Recycle Game Objects
Optimization is an important part game developers are constantly dealing with in order to make games run faster and more efficiently. Spawning tons of game objects, needless to say, can cause an impact on the performance and you will need some ways to optimize this aspect. This is where object pooling comes in.
Object pooling is an optimization technique that reuses destroyed game objects that appear multiple times in a gameplay session. It helps improve the performance in scenarios where a significant amount of game objects are constantly created and destroyed multiple times such as rapidly shooting multiple projectiles from a weapon.
Object pooling is one of the most common techniques in game development so it doesn’t hurt to learn about it as you will definitely find uses for it as you continue on your game development journey. In this article, I’ll explain what object pooling is in detail and show you a little bit of an example of object pooling in Unity.
What is Object Pooling Optimization Technique in Game Development?
Object pooling’s focus is to reduce the number of times your game needs to create and destroy a large number of game objects that need to come and go from a scene multiple times frequently by disabling or hiding the objects that are supposedly destroyed and then re-enabling when you need those game objects in the future.
Take a bullet hell top-down shooting game where you as a player can fire tons of bullets at hordes of enemies for example.
What you might do in this scenario is:
- Spawn bullet game objects.
- Send them flying towards enemies.
- Destroy the bullets when they hit an enemy or when they fly off the screen.
- Rinse and repeat for every bullet game object.
Following these steps, your game will have to create a lot of bullet game objects, destroy them, and then spawn new bullets repeatedly every time the player shoots something.
This way of implementing this particular game mechanic causes quite a bit of performance drop as creating and destroying game objects can be expensive operations if done extensively over a short amount of time.
This is where object pooling comes in.
Instead of getting rid of a game object that has served its purpose, you rather hide the object or set it to inactive and re-enable it when you need to use it again.
For example, instead of destroying the bullet game objects once they hit an enemy or go off screen, you set their active status to disabled to hide them from the game scene. Once the player tries to shoot something again, all you have to do is reset the bullets’ position and then enable them.
Right, with object pooling, this is what you do instead:
- Create an empty list to store the bullet game objects.
- Spawn/Instantiate the bullets for the first time.
- Store them in the list object you just created.
- When the bullets hit an enemy or fly off the screen, disable them so they’re gone from the scene. Don’t destroy them.
- When you need to shoot something again, get the disabled bullet game objects from the list.
- Reset their positions and enable them.
Some of you might have already figured it out. We’re simply storing the “used” game objects in a list, a.k.a. an object pool, and reuse them whenever we need to. Hence, the name “Object Pooling”.
This will significantly reduce the number of times the game needs to instantiate new game objects, thus, improving the game’s performance because the game doesn’t have to constantly create new objects all the time.
What is The Use of Object Pooling? Should I Use Object Pooling?
Object pooling is commonly used in game development when a game needs to spawn a lot of the same game objects shortly one after another continuously over a period of time.
The answer to whether you should use the object pooling technique or not is: It depends.
This largely depends on what kind of game you’re making and what kind of gameplay mechanics are present in your game. Obviously, you’re most likely not going to use this technique in something like a visual novel or a no-combat horror game since, characteristically, they don’t rapidly spawn objects multiple times.
Consider using the object pooling technique if:
- Your game needs to reuse the same kinds of game objects repeatedly in a short period of time.
- Periodically create the same game objects at a set interval with the same or similar conditions and parameters.
- The player shoots projectiles that are visible in the game scene and are game objects.
There are also many other cases where object pooling can come in handy. This article won’t end if we keep listing them, but you get the idea.
When To Use Object Pooling To Optimize Your Game
Object pooling can be used in many situations. One of the most common users of this technique is the bullet hell top-down shooter where the player continuously shoots out tons of bullets and there are tons of enemies constantly popping out to greet you.
Here is another example. In an Adventure Hack n’ Slash one-room-per-stage-style game, you might spawn a set of enemies at the beginning of a stage and later in that stage, you might encounter those enemies again.
Object pooling will help decrease the number of times you need to create and spawn those enemies, especially if you’re going for some kind of endurance mode with enemies spawning and rushing at you continuously.
Another good example is the enemies that live in the open world in MMORPGs as they tend to respawn after a while after getting defeated by players. Object pooling perfectly works in this scenario.
Conclusion
Object pooling is a great way to make your game run smoother and more performant due to how it handles multiple copies of game objects.
The technique involves creating a list and storing objects you will be using in the scene multiple times in there, disabling those objects once they’re done with their jobs, and resetting and re-enabling them when you need them again. This way, your game won’t have to constantly create and destroy many game objects, thus, increasing the performance.
There are many ways to utilize this optimization technique. Go out and explore the possibilities!
Are you thinking of making a game right now? If you have no idea where to start, how about you check out my other article about the type of games that are recommended for beginner developers and see if any of them pique your interest for a start?
Have a great day, and I will see you again in the next article!
Attribution