Object pooling and effects

In my game i honestly see very little use for object pooling. the only space i could see it being used would be particle effects like healing, burning, maybe some spell prefabs, etc. No more than 8 of any effect could ever possibly be needed at a time.

  1. is it worth it to create an object pool for relatively low poly effects like this?

  2. if so can/should one object pool handle all effects and the Get() pass through the specific prefab it needs from a common object pool?

especially with number 2
I’m not sure if this is is violating single responsibility as it would be handling many prefabs in one pool. i guess i could use a generic object pools and use a bunch of them but it seems like overkill to handle no more than 8 of any one effect.

It’s worth it any time you’re creating and destroying objects on a regular basis. Whenever you Destroy a GameObject, it’s queued for Garbage Collection. While Unity has gotten better using Incremental GC, at a certain point if the uncollected garbage (GameObjects destroyed, but memory not reclaimed to the memory pool and the memory unfragmented) exceeds a certain amount, Unity will take care of it all at once, which leads to a game freeze.

Nope. You need a different pool for each type of prefab.

That’s the best use case. It’s not about the number you need at one time, it’s about the number you need over time…

1 Like

thanks brian. building object pools now lol

1 Like

They really are worth it once you get them going.

ohohohohoh

i have another question…

like i have said before combat is rpg-turn based, enemies spawned in an area are determined BEFORE you load into the zone they are in. also you cant change active party members while in a zone. instead of having a floating object pool for each effect… what about something that queries the abilities of the party and enemies in the zone. then i could create an object pool array or list via generic singleton. this would recycle the object pool script, only generate object pools for the effects needed, and stop me from creating new object pools every time the combat scene loads from the area scene.

thoughts on any part of this idea that is bad or if list/array would be an issue??
I already have a loadout manager for the party. i could easily re-use some of that code to pull all ability effects from enemies.

if it matters in the current architecture enemy groups and player groups are in thier own scriptable objects, which hold their abilities(currently also SOs but trying to research strategy pattern as a possible upgrade for abilities)

@Brian_Trotter … ohm actually i just watched the strategy pattern video… turns out i was using the strategy pattern and didn’t realize it. not sure how that happened, i just drew it out one night and didnt realize it was already a thing.

There are a lot of patterns I was using before they had names, simply because they made the most sense at the time.

You said the Singleton word. Don’t say that word where Sam might here it! :stuck_out_tongue:

When it comes to object pools, I prefer a generic static class, though that takes a little finagling…

Something like this:

using UnityEngine;
using UnityEngine.Pool;

namespace Pooling
{
    public class StaticPool<T> where T : MonoBehaviour, IPoolable
    {
        private static ObjectPool<T> pool;
        

        public static void InitializePool(T prefab, int defaultCapacity = 10, int maxSize = 10000)
        {
            if (pool != null) return;
            if (!prefab) return;
            
            pool = new ObjectPool<T>(() =>
            {
                T t =  GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity);
                t.SetDisposeCallback(() => pool.Release(t));
                return t;
            }, poolable => poolable.gameObject.SetActive(true),
                poolable => poolable.gameObject.SetActive(false),
                poolable => GameObject.Destroy(poolable),
                false,
                defaultCapacity,
                maxSize);
        }
    }

    public interface IPoolable
    {
        void SetDisposeCallback(System.Action callback);
    }
}

Each different prefab would need it’s own dedicated type… (you’d run into problems with this, for example, if T was a ParticleSystem, but you could create a placeholder type of FlameParticle (which would have to be an IPoolable, then you would have to initialize the system with

StaticPool<FlameParticle>().InitializePool(prefab); //assuming you've linked the prefab to the GO that has this script

The FlameParticle would have to have the SetDisposeCallback() method properly filled in, and the Callback would have to be stored…

Then you’d get a FlameParticle with

FlameParticle fp = StaticPool<FlameParticle>().Get();

When the FlameParticle is no longer needed, it simply invokes the callback to release itself back to the pool.

I just typed that out from memory, so it might need some test runs to be sure I didn’t forget something.

1 Like

Wow Brian thats a very detailed response. honestly it’s going to require more attention than I can give e this while traveling. You may hear back from me on this in a couple days. Thanks again, I wasn’t expecting a script to magically generate itself based on vague concept questions.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms