Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been reading this article: http://gameprogrammingpatterns.com/component.html which describes how a game object should be constructed out of components. It also encourages the decoupling of components, which brings up a problem with input, collision detection, and things that cause events to be triggered.

If I have a game object, which is a player and this player has 3 components; PlayerSoundManager, PlayerCollisionDetection, and PlayerStateManager, how would I go about making sure they have no references to each other? If PlayerCollisionDetection detects that a player should die, messages should be sent to the other two components telling the player to play a death sound, and change it's state.

Would it be wrong to put collision detection methods throughout all components and perform each reaction separately?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

No, but it will just be bad manners to write the same thing in every scripts and it can be a pain to debug if you have lots of function of collision detection in a lot of other scripts.

You can use the method SendMessage() to communicate between scripts (or inside a script). You can read its documentation here

e.g

if(player.health == 0){
  gameObject.SendMessage(playerStateManager.changeState, 'dead');
  //where playerStateManager is a reference to your class PlayerStateManager
  gameObject.SendMessage(playerSoundManager.play, 'deathSound');
  //where playerSoundManageris a reference to your class PlayerSoundManager
}
share|improve this answer

Your Answer

 
discard

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.