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

So I've just been thinking about component and messaging systems recently for simple C# XNA games and came up with this. How extensible would this implementation be and what are the drawbacks? Example below:


class Score
    public int PlayerScore { get; set; }

    public Score()
        EnemySpaceship.Death += new EventHandler<SpaceshipDeathEventArgs>(Spaceship_Death);

    private void Spaceship_Death(object sender, SpaceshipDeathEventArgs e)
        PlayerScore += e.Points;


class EnemySpaceship
    public static event EventHandler<SpaceshipDeathEventArgs> Death;

    public void Kill()

    protected virtual void OnDeath()
        if (Death != null)
            Death(this, new SpaceshipDeathEventArgs(50));


class SpaceshipDeathEventArgs : EventArgs
    public int Points { get; private set; }

    internal SpaceshipDeathEventArgs(int points)
        Points = points;

Basically, the idea uses the built in events system in C#. When something interesting happens to an object, it raises a static event. Then, anything else in the game that wants to know when that happens subscribes to that static event and does something in response. In this case, the Score class subscribes to the static Death event of a Spaceship and when it dies, it uses the EventArgs to adjust the score accordingly.

Would this kind of architecture start to crumble and get quite confusing as the development of the game progressed? Thanks for reading.

share|improve this question
FYI, you can use lambdas fory our event handlers, i.e. EnemySpaceship.Death += (sender, e) => { PlayerScore += e.Points; }; – ashes999 Feb 8 '12 at 0:45
@ashes999: Ah, right. Thank you very much. That will certainly help with keeping lines of code short and tidy. – Harry Roberts Feb 8 '12 at 3:52
up vote 6 down vote accepted

That is a perfectly reasonable approach, and is quite common. I do it myself and find it makes for really clean and powerful code. Do watch out for strong references created by event listeners though, here's an article on that:

share|improve this answer
I agree with John, you may have to manage proper de-allocation of the events or they will build up can cause you to see confusing behavior. – Chuck D Feb 7 '12 at 23:50
Interesting. Can you provide a brief example of how you'd deregister an event in an XNA game? – Michael Feb 8 '12 at 1:32
@Michael: In this case, add EnemySpaceship enemy = sender as EnemySpaceship; if(enemy != null){ enemy.Death -= new EventHandler<SpaceshipDeathEventArgs>(Spaceship_Death); } in your Spaceship_Death method to detach from the event. Here's some documentation on Events that may describe this in more detail: – John McDonald Feb 8 '12 at 3:57

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.