4

I have been thinking of trying out component based architecture for game development. I have read some blog posts and articles about it but I have a few things I have not sorted out yet. When I say component based I mean that you can add components to a ComponentManager that updates all components in its list. I want to try this to avoid getting classes that inherits variables and functions that they don´t need. I really like the idea of having an really simple entity class with a lot of components working side by side without getting bloated. Also, it is easy to remove and add functionality at runtime wich makes it really cool.

This is what I am aiming for.

// this is what the setup could be like
entity.componentManager.add(new RigidBody(3.0, 12.0));
entity.componentManager.add(new CrazyMagneticForce(3.0));
entity.componentManager.add(new DrunkAffection(42.0, 3.0));

// the game loop updates the component manager which updates all components
entity.componentManager.update(deltaTime);

Communication: Some of the components need to communicate with other components

I can´t rely on the components to be self-sustaining. Sometime they will need to communicate with the other components. How do I solve this? In Unity 3D, you can access the components using GetComponent().

I was thinking of doing it like this, but what happens if you have two components of the same type? Maybe you get a Vector back.

var someComponent : RigidBody = _componentManager.getComponent(RigidBody);

Priority: Some components need to update before others

Some components need to update before others to get the correct data for the current game loop. I am thinking of adding a PRIORITY to each component, but I am not sure that this is enough. Unity uses LateUpdate and Update but maybe there is a way to get an even better control of the order of execution.

Well, thats it. If you have any thoughts don´t hesitate to leave a comment. Or, if you have any good articles or blogs about this I will gladly take a look at them. Thanks.

Component based game engine design

http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/


EDIT: My question is really how to solve the issue with priorities and communication between the components.

Community
  • 1
  • 1
Mattias
  • 3,907
  • 4
  • 28
  • 50

1 Answers1

3

I settled for an RDMBS component entity system http://entity-systems.wikidot.com/rdbms-with-code-in-systems#objc

It is so far working really well. Each component only holds data and has no methods. Then I have subsystems that process the components and do the actual work. Sometimes a subsystem needs to talk to another and for that I use a service locator pattern http://gameprogrammingpatterns.com/service-locator.html

As for priorities. Each system is processed in my main game loop and so it is just a matter of which gets processed first. So for mine I process control, then physics, then camera and finally render.

Allan
  • 3,339
  • 2
  • 21
  • 24
  • Thanks for the answer. I still have a lot to figure out, but you put me in the right direction. – Mattias Oct 05 '11 at 11:40
  • @Mattias No worries. Yeah I found a lot of literature on component entity systems neglected a lot of the details. In the end I just read everything I could and figured something that made sense to me. I hope to write an article on it sometime. I really think component entity systems are the way to go for games development so hopefully you find it worthwhile too :) – Allan Oct 07 '11 at 03:59
  • @Allan I'm looking at the RDMBS method. Each "system" searches for entities that match the component criteria for the system. Isn't this super inefficient? Not including that per entity found, we have to search again for components that we want to use. Is there a more efficient solution, or we have to do series of dictionary look ups, and array creations for each update? I'm looking at this code base as example : [link]https://github.com/adamgit/Game--Escape-from-the-Pit/blob/master/eclipse%20workspace/Escape%20From%20the%20Pit/src/org/tmachine/games/escapefromthepit/SubsystemGhosts.java – terry franguiadakis Nov 16 '11 at 23:35
  • @terry franguiadakis - The RDMBS method isn't designed with optimal speed in mind, but the trade off is worth it. In practise there is a negligible performance hit. As it is, with games, at least 50% of the time is spent rendering. For my tests the entity system with look ups only cost around 1% of total time. To ensure the best performance I used two dictionaries, one was to ensure fast retrieval when calling getAllComponentsOfType() and the other for fast getComponent() – Allan Nov 18 '11 at 00:13