Pooling can be used to increase the performance of your game – learn more about it and when to use it.
Instantiating and destroying game objects can decrease the performance of your game, especially when happening often. This can be reduced by using pooling.
Pooling is a technique that will instantiate a pool of game objects when a level was loaded and only enables and disables them afterwards instead of destroying them and instantiating new objects. Usually, this will help increasing the performance, but it can depend on the use case if it actually helps or has negative effects, since having a lot of game objects alive in a pool (even disabled) also costs memory.
Pooling is set up in the Makinom editor (Base/Control > Game Object Pools).
When to use Pooling
As said, pooling costs memory for holding the game objects, but increases the performance by removing the heavy instantiation and destruction of game objects.
You should use pooling when game objects are often created and destroyed, e.g.:
- firing projectiles, missiles, etc.
- reappearing enemies
You shouldn’t use pooling for things like level architecture or procedurally generated levels, since those will most likely only be instantiated once and not destroyed until the level ends.
E.g. the missiles fired by the player’s ship in a vertical space shooter are pooled. The player’s fire rate and the time a missile will be alive result in a maximum of about 15 missiles to be alive at the same time.
The pool size is 20 – this is a good use of a pool, but can also be reduced a bit.
The pool size is 100 – this is a waste of memory.
Game Object Pools
A game object pool is based on a prefab. You can add a pool in the pooling settings and select the prefab that will be used for pooling.
The Start Size of the pool defines the quantity of game objects that will be created at the start of a level. Pools can optionally grow by enabling Can Grow in the pool’s settings – this will add more game objects to the pool when needed.
The pooled objects can be used by the prefabs defined in schematics. It’s only used when the prefab is defined as a resource in the schematic’s settings and has using pooling enabled. The prefab in the schematic and the pool must be the same prefab. If that’s the case, spawning and destroying a prefab in the schematic will get a game object from the pool and enable/disable it instead of instantiating and destroying it.
The prefab missile is pooled with a start size of 10 and can grow.
The player’s control is handled by a schematic using the missile prefab as a resource with Use Pooling enabled. The same prefab as the pooled prefab is used, i.e. spawning this prefab will use a pooled game object.
The level starts and 10 missile prefabs are instantiated and disabled immediately. The player fires missiles, they’re placed at the player’s position and enabled (Spawn Prefab node). When they hit a target or leave the screen, they’re disabled (Destroy Prefab node).
If the pool runs out of missiles, new missiles will be instantiated and added to the pool (i.e. they’ll also be enabled/disabled when used).
The game object pools will be created when a level was loaded. You can check the current scene before creating a pool to limit pooling to defined scenes:
Only the scenes that are defined will use pooling.
The defind scenes are excluded from pooling, all other scenes use pooling.
By default, pooling uses Exclude without defined scenes – i.e. all scenes will use pooling. The scene checks can be overridden by each individual pool.
The prefab missile is pooled with a start size of 10 and can grow. Pooling objects is excluded from the scene Start Menu.
The game starts in the Start Menu scene, where a menu is displayed (Show Dialogue node) to start the game, load or view highscores. No pooled game objects are instantiated here.
The player starts the game and the first level loads – 10 missile prefabs are instantiated and disabled immediately.