Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a component based system, how should change the rendering/visual state of an entity such that I know what animation to use, without breaking encapsulation?

My guess is that it would have to be message-based system, not a query-based one, because having a few ifs checked every frame to determine the right animation based on the values of the attributes of the other components doesn't sound very efficient.

share|improve this question
Component systems aren't designed to be particularly Efficient, they're designed to be Flexible. If you're worried about a few if() statements every frame then you're worried about the wrong thing =) – Patrick Hughes Dec 9 '11 at 17:34
How do your components normally talk to each other? You have a specific situation listed but in general you are just asking how does A talk to B. – James Dec 9 '11 at 21:12
@James Dependencies are set when I initialize them. The components can't later connect with other components (all dependencies must be set from the beginning). – Paul Manta Dec 9 '11 at 22:24
@PaulManta I would definitely suggest some sort of a well defined small API communication system then for just general blind communication between the components. – James Dec 9 '11 at 23:36
@James When it comes to 'regular' components I've managed to make communication between them very well defined. However, I get the feeling that the physics and rendering components require special treatment (the rendering comp. is the one that's giving me problems). – Paul Manta Dec 10 '11 at 16:13
up vote 2 down vote accepted

The thing is, your animated is coupled to the state of the other components in the system. So there really isn't any problem with having your animation component knowing about, or even querying directly, relevant components that contain what kind of internal state you need to represent in an animation.

For basic movement stuff, usually what I do is set up some kind of observer pattern. Every frame I check the velocity vector of the object (in whatever component it's stored in) and use that to determine which movement based animation to play. For other oneoffs, I just have the components that handle them see if there is an animation component, and plays an appropriate animation from there.

share|improve this answer
I disagree somewhat about that. The rendering component is not inherently tied to the other components' states, but to the entity's 'macro' state (dead, running, jumping, etc). Other components might also be interested in the macro state, for example the movement component might ignore button presses if the entity is dead. I guess my question is more about implementing such a macro-state system for the entity. – Paul Manta Dec 11 '11 at 15:03
I didn't say the rendering component was coupled, but the animation itself was. So in terms of coupling, it would be something like entity "game logic" component -> animation component -> skinned mesh component -> rendering component. – Tetrad Dec 11 '11 at 18:43
I've though about it and it seems letting the renderer know about other components is the way to go. I'm still not about checking the motion vector, though. What if the entity moves because it's being pushed, not because it's walking? What about jumping? How do you determine if the upward motion is caused by a jump, by climbing, or by some other factor? – Paul Manta Dec 12 '11 at 12:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.