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.

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:

Score

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;
    }
}

EnemySpaceship

class EnemySpaceship
{
    public static event EventHandler<SpaceshipDeathEventArgs> Death;

    public void Kill()
    {
        OnDeath();
    }

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

SpaceshipDeathEventArgs

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

1 Answer 1

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: http://www.codeproject.com/Articles/29922/Weak-Events-in-C

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. –  Rubber Mallet 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
1  
@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: msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx –  John McDonald Feb 8 '12 at 3:57

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.