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.

Im trying to implement an Event System for a game, where there are classes that can fire or handle an event wheter or not they implement these interfaces:

public interface IGameEvent<T> where T : EventArgs
{
     event EventHandler<T> OnEvent;
}

public interface IGameHandler<T> where T : EventArgs
{
    void OnEvent(object sender, T e);
}

everything looked great until i realized that no class can implement more than 1 IGameEvent because it would cause duplicate declaration,

Here is an example:

public class Event
{
    public KeyPressEvent OnKeyPress;
    public UpdateEvent OnUpdate;

    public void AddHadler<T>(IGameEvent<T> eEvent , IGameHandler<T> eHandler) where  T : EventArgs
    {
        eEvent.OnEvent += eHandler.OnEvent;
    }

    public void RemoveHandler<T>(IGameEvent<T> eEvent, IGameHandler<T> eHandler) where T : EventArgs
    {
        eEvent.OnEvent -= eHandler.OnEvent;
    }
}

KeyPressEvent:

public class KeyPressEvent : IGameEvent<KeyPressEvent.KeyPressedEventArgs>
{
    public class KeyPressedEventArgs : EventArgs
    {
        public KeyPressedEventArgs(Keys key)
        {
            Key = key;
        }

        public Keys Key { get; private set; }
    }

    public event EventHandler<KeyPressedEventArgs> OnEvent;

    private void OnCheckForKeyPressed()  //Example
    {
        if (OnEvent != null) 
            OnEvent(this, new KeyPressedEventArgs(Keys.Space));
    }
}

Would be better to manually store the suscribers in a list in the EventSystem ?

How slower or faster than this approach that would be?

Thanks!

share|improve this question
    
Events are not classes; they are a type of class member along with Properties and Methods. –  Pieter Geerkens Jul 29 '13 at 4:51
    
What are you trying to achieve here? What you've got here is not a good approach, but it's very difficult to talk about potential right approaches without the context of what problem you're trying to solve. (For example: You've tagged this with "performance" - but what's the performance issue here? This approach and those in my answer are all "fast enough" - depending on the context.) –  Andrew Russell Jul 29 '13 at 10:44
    
I think you're looking for this.. en.wikipedia.org/wiki/Observer_pattern –  wes Jul 29 '13 at 21:21

2 Answers 2

up vote 3 down vote accepted

Although you can use the existing event model in C#, it might not always give you the flexibility that you require.

You can for instance not sort events based on their priority, clear events of a specific type or add a queued/delayed event.

Example)

EventManager

To signup for an event the object needs a method with the same signature as the CallbackMethod delegate. The call might look like this:

class SomeObject
{
    private void OnDamageTaken(object sender, IGameEvent e)
    {
        ...
    }

    ... void Initialize()
    {
        ...
        EventManager.Subscribe("EVENT_PLAYERINJURED", this.OnDamageTaken);
        ...
    }
}

//Somewhere in the application
EventManager.Notify("EVENT_PLAYERINJURED", new PlayerInjuredEvent(...));

If you don't like using hardcoded eventIds everywhere you could introduce a property on your game event classes.

public PlayerInjuredEvent : IGameEvent
{
    public static string EventType
    {
            get
            {
                return "EVENT_PLAYERINJURED";
            }
    }
}

Now you can write:

EventManager.Notify(PlayerInjuredEvent.EventType, ...

I hope this gives you some ideas on how to solve your problem.

share|improve this answer

You're basically re-inventing event here. Your entire system is basically an ugly re-implementation of the existing event infrastructure in C#. You should try and use event directly instead.

Or you could even consider doing what most games do, which is to simply use methods instead of events. Have an Update method and (if you like) a KeyPress method. Then just call those methods. Very, very simple. This is preferable over events for frame-based simulations (anything with an Update/Draw loop).

(Personally I like to make key-press information available globally (not architecturally perfect, but extremely practical), or passed into an Update method as an argument, rather than as a separate method call.)

Along the lines of the of methods, if you're finding you have too many methods, you might want to consider a component-aggregation architecture.

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.