Whenever I shoot, I use bulletObject = mm.playerBullet.Get(); (mm is MainManager, which holds reference to everything important). And it works…
… The issue however, when multiple bullet types and multiple enemy types have to be spawned. My pool is already filled with bullet A and if I assign bullet B or C or D to my playerBulletPrefab, my pool will be now mixed with two bullet types.
I could clear my pool, every time I use a new type but that would destroy the purpose of using Object pooling?! and creating hundredths of pools for each single type seems very unreasonable and wrong to me.
I don’t really know how to approach this, especially because all my bullet and enemy types are very different from each other with different scripts attached to them, simply changing the sprite or speed of each object in the pool wont be enough.
And before someone’s wondering, yes I’m an idiot and yes my knowledge about programming isn’t very advanced… my degenerate brain can only do so much, I’m trying my best…
Create more pools, no one said you need to use exactly one pool instance.
Each pool can be its own type.
This example recycles instances of ParticleSystem, but you can make a management hub for multiple such pools, which you can access to lease and return all kinds of objects.
Those two sentences kind of contradict each other. You already recognised that the point of a pool is to keep the objects alive so they can be reused later and you don’t have to create and destroy the objects all the time which is a quite expensive operation. So we trade memory for speed. However now you state that creating many pools seems unreasonable?
Just think about it logically. I guess you can only have one weapon active at a time, right? So only one bullet type may be needed. However when you switch weapons you need bullets of another bullet type. If they don’t exist yet, they would need to be created on the fly which is expensive and the pool should avoid that. So the bullets have to exist already in order to have the benefit of pooling. So all those instances of all those different bullet types have to be stored somewhere. Of course it makes no sense to store completely different things in the same list / array / pool as you would not know what kind of object you’d be dealing with. So separate pools is the only logical approach. The pools themselfs are tiny compared to the actual content. The instances that are pooled usually occupy much more memory than the pool itself. Imagine you have 10 different bullet types and you want to pool 100 instances of each type, so 1000 instances in total. Having 10 lists each storing 100 object references or having a single list that stores 1000 makes almost no difference. Though having 10 individual lists of course makes it much easier to actually manage and group things that belong together. A “messy” pool where you throw in arbitrary objects makes no sense as you would somehow need to identify the type of an instance in the pool when you need to get one. This just makes no sense.
I have a Spawner behavior which is just that. It starts empty, and keeps a limited number of sleepers keyed by limited number of recently used prefab roots. My SelfDestructing behavior can figure out if it came from a Spawner and thus Despawn, or if it should just Destroy itself. It’s essentially a capacitor… you can defeat its benefits if you constantly spam a bunch of instances of different prefabs, but in normal usage it can take the shock out of instantiating straight out of the prefab pile again.
Okay, that’s an interesting point. I’m easily distracted and often forget things, it’s a mobile game with not that many bullets and enemies spawned. Maybe I do more harm than good with that much pooling and should only use it on a very few selected things. I test the performance of my game first and then decide what should be pooled…And I just realized I can use particle system for my enemies, instead of spawning them.