I'm developing my own game engine with ECS framework. This is the ECS framework I use:
- Entity: Just an ID which connects its components.
- Component: A struct which stores pure data, no methods at all (So I can write .xsd to describe components and auto-generate C++ struct code).
- System: Handle game logic.
- EventDispacher: Dispatch events to subscribers (Systems)
but I'm confused about how should systems update the members of components and inform other systems? For example, I have a TransformComponent like this:
struct TransformComponent
{
Vec3 m_Position;
Float m_fScale;
Quaternion m_Quaternion;
};
Obviously, if any member of the TransformComponent of a renderable entity is changed, the RenderSystem should also update the shader uniform "worldMatrix" before render the next frame. So if I do "comp->m_Position = ..." in a system, how should the RenderSystem "notice" the change of TransformComponent? I have come up with 3 solutions:
Send an UpdateEvent after updating the members, and handle the event in related System. This is ugly because once a system modify the component data, it must send an event like this:
{ ...; TransformComponent* comp = componentManager.GetComponent<TransformComponent>(entityId); comp->m_Position = ...; comp->m_Quaternion = ...; eventDispatcher.Send<TransformUpdateEvent>(...); ...; }Make members private, and for each component class, write a relevant system with set/get methods (wrapping event sending in set methods). This will bring a lot of cumbersome codes.
Do not change anything, but add "Movable" component. RenderSystem will iteratively do update for renderable entities with "Movable" component in Update() method. This may not solve other similar problems, and I'm not sure about the performance.
I can't come up with an elegant way to solve this problem. Should I change my design?