

Doing a hardcoded number of them, means you might accidentally make a way to exceed the limit and cause an error, or you might have more than you ever really need at a time, and waste memory on them.
Random objects code#
The actual code to do that is usually very specific to the game, and you could hardcode a specific number in an array (like for instance, 100 bullets per player, assuming the player can't fire more than that ever fast enough to exceed that number) and just reuse them over and over, or you could use a generic list or dictionary as a dynamic growing pool, and create new ones on the fly to expand the pool to have however many get created at runtime, and both approaches have advantages and disadvantages. Really not that many things in games need pooling in a lot of cases, but just a few sets of things with specific rapidly used kinds things. you know, anything that only gets created or destroyed if you come within range of it or not very often, these types of things don't really need pooled, because they are kind of special to the area/use in the game. these are good cases where having a pool pre-loaded and ready to "move and activate" rather than instantiate, is the best way to approach it.ĭisabling/enabling gameobjects and components is faster than creating and destroying them, so thats the basic idea behind it all, is to avoid Instantiate and Destroy. these are the important things to pool because you don't want to instantiate and destroy a lot of them, or you clog up the processor and memory trying to create tons of new ones and collect the garbage of old unused/destroyed things. things that interact physically or move around (not static world stuff) and often get "destroyed" when they are distant or out of view from the player, or things that rapidly get instantiated like missiles from a helicopter.

A game like GTA has cars, pedestrians, street lamps, benches. I think the best way to really think about the concept of object pooling is to break down what there is going to be in your game that is dynamic or often recreated and removed, like a shooter game has lots of explosions, lots of "bad guys" and things that get instantiated/spawned and then later destroyed/despawned. Object pooling is really a simple concept, but can be tricky to wrap your mind around in actual use.
