I have a ResourcePool class that uses generics.

public class ResourcePool<T> where T : class
        bool isWrappable;
        int wrapIndex;
        int numberOfInvalidObjects;
        ValidateObject objectCheck;
        T[] objects;

If I have multiple ResourcePool classes.

private ResourcePool<PlasmaAmmo> poolPlasmaAmmo;
        private ResourcePool<LazerAmmo> poolLazerAmmo;
        private ResourcePool<HappyAmmo> poolHappyAmmo;

How can I store these pools in a dictionary or list?

List<ResourcePool<T>> pools = new List<ResourcePool<T>>();

Doesn't work for me.

6 Years
Discussion Span
Last Post by DaveTran

List<ResourcePool<T>> pools = new List<ResourcePool<T>>(); T has to be a class.

I would use an interface for Ammo-classes:

public interface IAmmo

public class PlasmaAmmo : IAmmo

public class LazerAmmo : IAmmo

public class HappyAmmo : IAmmo

After that you declare ResourcePools for different ammos:

private ResourcePool<IAmmo> poolPlasmaAmmo;
private ResourcePool<IAmmo> poolLazerAmmo;
private ResourcePool<IAmmo> poolHappyAmmo;

and then declare a list of ResourcePools:

List<ResourcePool<IAmmo>> pools = new List<ResourcePool<IAmmo>>();



So if I wanted a list of all pooled objects then I would have to create an IPoolable interface?

This way lights, cameras and enities can all derive from that interface and all pools can then be put into one large dictionary/list.

List<ResourcePool<IPoolable>> pools = new List<ResourcePool<IPoolable>>();

Is this logical thinking and sensible? :)

Edited by DaveTran: n/a


Is this logical thinking and sensible?

Yes and no. You would be able to get all resource pools to a single list with "IPoolable" interface.

I would use separate lists of resource pools for similar resources (or resource pools), like ammos or cameras in their own resource pools. This is not to say to forget the "IPoolable" interface. The choice depends on the "bigger picture" i.e. how you're going to use the list of (all) resource pools.



I'm going to use a dictionary of resource pools that can be searched using a string.

"AmmoLazer" will bring up the AmmoLazer pool
"SpotLight" will return the SpotLight pool

This way I can update all pools easily using a foreach pool in dictionary method.

You would be able to get all resource pools to a single list with "IPoolable" interface.

Could you please explain how/why this could be the case?


Could you please explain how/why this could be the case?

Didn't test it but you would be able to do List<ResourcePool<IPoolable>> pools = new List<ResourcePool<IPoolable>>(); kind of structure to contain all resource pools, I think. That would hardly be practical.

The dictionary you're using makes more sense:

Dictionary<string, ResourcePool<IAmmo>> pools = new Dictionary<string, ResourcePool<IAmmo>>();

pools.Add("AmmoLazer", poolLazerAmmo);
pools.Add("AmmoPlasma", poolPlasmaAmmo);
// For each resource pool
foreach (ResourcePool<IAmmo> pool in pools.Values)
// or get a single pool
ResourcePool<IAmmo> lazerAmmo = pools["AmmoLazer"];

and I would still use IAmmo interface :)



How about:

Dictionary<string, ResourcePool<IPoolable>> pools = new Dictionary<string, ResourcePool<IPoolable>>();

Then all pools are in the same place. Please explain why you would advise against this as I can't see why any IPoolable object can just be added to the dictionary.

Sorry to be pedantic but I'm really curious why an all encompassing IPoolable dictionary is ill-advised.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.