Short version:
How would i go about creating an object pool that can store classes of different types all derived from the same base class?
See below for an example of expected usage.
Long version:
I have a class BaseComponent
, with many derived classes e.g. Child1Component
, Child2Component
.
I also have another object that represents a collection of these components, with their properties set to specific values. I call this an EntityTemplate
, since an entity is defined by the set of components and their values.
I want to create entities based on entity components. To do this currently i get the appropriate EntityTemplate
, loop through it's different components and call a Clone
method I've defined on each child class. I also have a Copy
method defined there too, which might be useful.
When an entity expires i'd like to add its components to an object pool, then when i next need to create an entity i'd get the entity template and for each component i'd get one of the same type out of the pool and set it's properties equal to the one in the EntityTemplate
, something like below:
// What i want to do
var entityTemplate = GetTemplate("UniqueString");
var MyActualEntity = new Entity();
foreach(var componentTemplate in entityTemplate)
{
var actualComponent = MagicComponentPool
.GetComponentSameTypeAsParam(componentTemplate);
actualComponent.CopyFrom(componentTemplate);
MyActualEntity.Components.Add(actualComponent);
}