- Home /
General pooling question
I have a general question about pooling. I have a simple 2D game that spawns random stars (different types) that you collect, random enemies that you avoid, and random health points that you collect. Some levels use different types that fit the world (eg. ice-world, summer, etc.)
Currently, the prefabs spawn randomly using a switch and are destroyed once they reach a boundary outside of the scene. This seems like a great opportunity to use pooling but I don't know the best way to use it.
1. Should I create a unique pool and instance for each unique prefab and then switch between them randomly in the spawner?
or...
2. Should I look into creating pools of stars that has all of the stars in it and randomly selects from the list, enemies with all the enemy types in it, etc?
...or...
3. Is there another option?
I'm brand new to pooling and have been researching all morning but I'm confused about how to approach this, given the random nature of what I want to do.
Also, do I need new instances for each pool if I go with option #1?
Thank you,
Confused
Answer by Tsaras · Mar 03, 2019 at 06:25 PM
Pooling effectiveness depends on the case. If you are using lots of gameobjects then pooling them will be more efficient than instantiate/destroy. That being said, your option 1 is viable in which you have a pool for each object type and after you random it you have a way to pick the appropriate pool to give you an available object.
For your option 2, I am not sure how you can implement the randomness in a complete pool of gameobjects, in order to enforce the probabilities you want. For example, if you throw all kinds of stars in a single pool of say 100 and you have 10 of them in use, the random generator will have to pick something from the remaining 90? Therefore making the problem a bit more complex.
Thank you, there are not too many instantiated items at any given time.. maximum 20ish. Would pooling even be appropriate?
It's not so much a matter of how many are instantiated at a given time but more of an issue of how many of them will have to be destroyed and re-instantiated. Generally if you have lots of object creation/destruction while you need a generally small number of objects active at one time is the ideal circumstances to pool them.
Thank you, I will get working on setting up different pools for each item.