I'm trying to implement a component based system for the first time and I understand the core concepts but I'm having trouble working out a framework that allows flexibility but is also cache friendly.
For example here's my Transform component:
struct TransformComponent
{
glm::mat4x4* localTransform;
glm::mat4x4* worldTransform;
int* parent;
};
and Here's an abridged version of my transform system:
class TransformSystem
{
vector<glm::mat4x4> worldTransforms;
vector<glm::mat4x4> localTransforms;
vector<int> parents;
vector<size_t> denseToSpare;
vector<InternalHandle> sparseToDense;
size_t componentCount;
size_t firstFree
Handle createComponent();
void destroyComponent(Handle target);
TransformComponent getComponent(Handle target);
void setParent(Handle child, Handle parent);
void update()
{
glm::mat4x4* wT = &worldTransforms[0];
glm::mat4x4* lT = &localTransforms[0];
int* parent = &parents[0];
for(size_t i = 0; i < componentCount; i++, wT++, lT++, parent++)
{
if(parent == -1)
wT = lT;
else
wT = worldTransforms[parent] * lT;
}
}
}
Here's the entity structure
struct Entity
{
unsigned int ID;
hash_map<ComponentType, Handle> componentHandles;
};
-Components are stored in a dense array which is managed by a sparse array of handles.
-External "Handle"s contain an index into the sparse array and a version number
-Internal "InternalHandle"s contain an index into the dense array, a version number and an index for keeping a free list of internal handles in the sparse array
This all works fine. The trouble is inter-component and inter-system communication. For example the MeshRendererComponent, and associated system, are dependent upon the TransformComponent for position.
I've spent hours looking at this problem from every angle I can think of and I can't just seem to figure out a reasonable solution to retrieving the component from the other system without making the components aware of their entity.
If anybody has any insights they would like to share I would be very grateful. Thank you.