I'm implementing a messaging system and I'm wondering about how I should handle events. Should I post them and send them to the appropriate listeners as soon as I receive them, or should I queue them and only dispatch when I am prompted to (most likely by the main game loop)?

Edit: More info

My game is a 2D platformer, so performance isn't going to be very hard to achieve. Considering what Roy T. said, I guess I might as well send out the event as soon as it is posted; there doesn't seem to be a need to queue it (unless I'm missing something, of course).


We really need more info, but if you're writing a really fancy system a dispatcher would be nice since then you can prioritize events (network events could have a higher priority than chat events for example).

However a real dispatcher is probably going to be overkill and might even make stuff slower because of the sorting on priority and if you don't think it over carefully enough you might starve low-priority events.

Anyway, with more info we can make a better guess what would be better.


As a response to you adding information: no it doesn't seem like you need a proper dispatcher :)

  • \$\begingroup\$ I added some more details to my question. \$\endgroup\$ – Paul Manta Sep 6 '11 at 8:55

You might want to have both available, and use the correct one for the current listener. For instance, your entity system might want to know immediately to remove an enemy, so it doesn't get updated and moved, but your inventory system might be able to wait as the player won't be able to use the item until the next frame anyway.

So on binding to the event, the listener should specify when it wants to be updated:

  • immediately
  • at the end of the frame
  • at the start of the next frame
  • just before the listener's next update
  • etc
  • \$\begingroup\$ In a larger system something like this would be very nice, but I don't think the scale of my current project justifies the time I'd need to spend on implementing this. \$\endgroup\$ – Paul Manta Sep 6 '11 at 9:48
  • 4
    \$\begingroup\$ Sure you know best what you need. But you asked which approach is best and the answer is for some minimum threshold of complexity you will find situations where you want both. \$\endgroup\$ – tenpn Sep 6 '11 at 11:16

C# event listeners are invoked in subscription order, immediately when the event occurs. I would do the same. I am using these in my 2D game, and it is working out great so far.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.