0

I have declared an event and I can add delegates to it. However, I would like to have a dictionary of supported events, so that derived classes can state which events they implement and which they do not.

When I don't use the dictionary, my code works fine, I can add listeners in other classes and invoke the event in derived classes by calling OnPickup:

public delegate void EventDelegate();
protected static event EventDelegate pickupEvent;

public void AddListener(EventName name, EventDelegate listener)
{          
    pickupEvent += listener;
}

protected virtual void OnPickup()
{
    if (pickupEvent != null)
    {
        pickupEvent();
    }        
}

But when I use the dictionary when adding the delegate, it doesn't work. pickupEvent is null when I call it in OnPickup():

    public delegate void EventDelegate();
    protected static event EventDelegate pickupEvent;

    //dictionary of events supported by this class
    protected Dictionary<EventName, EventDelegate> events = new Dictionary<EventName, EventDelegate>();

    public void AddListener(EventName name, EventDelegate listener)
    {        
        events.Add(name, pickupEvent);

        if (events.ContainsKey(name))
        {
            //we support this event type, add the delegate
            print("adding a listener");
            events[name] += listener;
        }
    }

    protected virtual void OnPickup()
    {
        if (pickupEvent != null)
        {
            pickupEvent();
        }        
    }

It's not clear to me why this doesn't work - is this something about events and delegates that I'm missing?

Nathalia Soragge
  • 1,415
  • 6
  • 21
TheLastBert
  • 466
  • 4
  • 16
  • Why do you want to do this? – Flydog57 Jul 15 '18 at 04:03
  • 3
    The code makes no sense... 1) You have a static event so everything is going to be listening for everything, 2) You add an entry, and then you immediately check whether that entry exists, and it always will (or an exception would have already been thrown). The root of your problem is that you can't pass around a reference to an event, which is essentially what you're trying to do. You can however pass around delegates which subscribe and unsubscribe to the event. – MineR Jul 15 '18 at 04:12
  • Possible duplicate of [How to pass an event to a method?](https://stackoverflow.com/questions/2560258/how-to-pass-an-event-to-a-method) – MineR Jul 15 '18 at 04:28

1 Answers1

0

Not sure what's going on here, but maybe what you want is something like this:

public delegate void EventDelegate();

//dictionary of events supported by this class
protected Dictionary<EventName, EventDelegate> events = new Dictionary<EventName, EventDelegate>();

public void AddListener(EventName name, EventDelegate listener)
{
    if (!events.ContainsKey(name))
    {
        events[name] = listener;
    }
    else
    {
        events[name] += listener;
    }
}

protected virtual void OnPickup(EventName name)
{
    if (events.ContainsKey(name) && events[name] != null)
    {
        events[name]();
    }
}

I really couldn't understand what you're trying to achieve, but the problem is that when you add pickupEvent to the dictionary, it's null. Then you add listener to nothing. If you really need that pickupEvent, add listener to it right before registering it to the Dictionary.

Nathalia Soragge
  • 1,415
  • 6
  • 21