Refactoring with components

Update: check this article for more in-depth details on Component Binding!

I’ve been refactoring my engine with the component-entity model, and things are turning out quite nicely. The one problem I always had with components was the way inter-component dependencies were handled. In the article I linked (one of the classics on the subject), the author says his components originally referenced each other through a component manager, but eventually they allowed components to store pointers to each other for performance reasons. In my mind, that’s way too many concrete dependencies.

So I’m rolling my own data binding system. Instead of directly accessing a component’s data from another component, I’m having both components store a copy of the properties they need. So if the physics component wants to overwrite the position and orientation of the model component, I don’t have to do this inside the physics component:

this.ModelComponentReference.WorldTransform = blah;

Instead, the physics component and model component have separate transforms. These act like input/output ports for each component. When constructing an entity, I bind the two “ports” together. I can also use multiple inputs for one output by combining them together with a transformation function. I can also bind actions (which is basically like event handling).

Here’s a good example of the power of this system. Let’s say I have a working player entity, and I want to have it play footstep sounds when he’s walking. I already have a PositionComponent and a PlayerStateComponent (with an enum of states like Idle, Running, Jumping…).

Let’s create a TimerComponent that has a few properties like Enabled, Interval, and Repeats, and an action (or event) that fires when the timer expires. First we would set a proper interval and enable repeating. Then we could create a binding for the Enabled state, which would set Enabled to true whenever the player’s state is “Running”. In my implementation, this is accomplished with a closure.

Then we can create a SoundComponent that’s set up with our footstep sounds, and whose 3D position is bound to our player’s position. We could then bind the TimerComponent’s firing event to the SoundComponent’s “Play” method. Mission accomplished.

Bear in mind that this system is still untested and I’m still in the middle of refactoring, but so far it’s looking good. Here’s a screenshot of my player factory code to give you an idea of how I’m doing this:

So there you go. It’s not perfect, but I think it’s going to work. The idea, at least, is sound. I’ll post later about the details of my binding system, which is still a matter of some contention. I’m trying to keep things clean while avoiding reflection at all costs.


Christ follower and indie game developer. Running, music, programming, games, art.

Posted in Coding, Lemma
6 comments on “Refactoring with components
  1. Sounds a lot like the component-entity system I ended up with after several iterations using Python. It was nice to work with, but ultimately I ended up switching development to Unity (and at the moment, not even that) because of lack of time to work on the tech side. I’m very interested in knowing how your system works out. :)

  2. et1337 says:

    Glad to hear I’m not going way out on a limb here. Another advantage to this system is that serialization becomes rather easy, what with the properties all set up already. I’ve finally got the code compiling again, and I hope to post another update soon with serialization and more example code.

  3. Wesley David says:

    Just wanted to post and say that you get bonus points for the August Burns Red desktop picture. =)

  4. Jonathon Bowyer says:

    I found this post while looking for implementations of communication between components, and its a very interesting approach. Binding matching properties together seems to be a great idea, and I’d like to find out more about it.

    Do you still plan on making a blog post detailing your data binding system?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Evan Todd. Christ follower and indie game developer. Running, music, programming, games, art.

Error: Twitter did not respond. Please wait a few minutes and refresh this page.