Question, if an event gets sent via messenger.send('event name',[param list])
, can this single event instance be received by many different objects that have registered themselves via base.accept('event name',[param list])
? Or can an event instance only be received once?
I tried to do this (having many objects receive one event instance), but my objects don’t seem to be responding to the events, and when I did print messenger
I didn’t see any of my objects or events in the output, just a lot of DirectGUI objects and events. Is this a bad idea?
I was trying to adopt a pattern where objects would send lots of different events, and many other objects might respond to each event, so a general many-to-many communication system. For example, to implement drag and drop, when the user presses down the mouse button you would do send('drag',[draggee])
. Each draggable object will have registered itself upon initialisation with base.accept('drag')
. So each draggable should receive the drag event and have a chance to respond, each will check if draggee == self
and the one that is the draggee will begin dragging itself. Lots of unnecessary checking, but it’s simple and flexible, because other interested objects could subscribe to the drag event also and respond in other ways.
Then when the user releases the mouse button a ‘drop’ event will be sent and things would proceed similarly. Container objects may want to subscribe to the drop event and check to see if the thing was dragged onto them, then they may want to take control of it.
I was thinking of using this pattern lots and lots, to allow many objects to respond to events generated by many objects, without the objects all needing to have direct or indirect references to each other.
I guess if Panda’s messenger does not work like this I can just implement my own, you just need a singleton messenger object that everybody has a reference to, and that maintains a dictionary mapping event names to lists of callable objects, and provides the send and accept methods.