So, my ECS right now is pretty simple. I have a GameObject class, which represents my entity, that contains a few helper methods. It also contains a hash set with all the components that specific game objects has. I have a few methods, like GetComponent, GetComponents, etc. I also have a Scene, which has all GameObject instances (stored in a hash set as well, with the gameobject name and instance) and other scene data (transition in, out, etc.). My AbstractScene contains a method for getting all components of a specific name, from all game objects in that scene. Something like this:
scene.getComponents('position'); // returns a list of all components with the name "position", from all GameObject in the scene.
This method is called from the Behaviors. PositionBehavior is responsible for processing all the "position" components. It's the system.
My question is: how do i optimize this? Having to iterate on all GameObjects and find all components, creating a new list and returning seems pretty cumbersome and inefficient. And i'm doing this on every update. My architecture is something like this:
- Start first scene
- Game loop
- Call the render() method on the Scene object
- Call all Behaviors that are Renderable
- Call the update() method on the Scene object
- Call all Behaviors that are Updateable
- Call PhysicsBehavior