0
\$\begingroup\$

I’m in the process of developing a game. My problem is this.. I’m going to implement the observer pattern to have my game objects “subscribe” to events that happen. For instance one character shoots which in turn creates a “sound event” and other characters will be able to subscribe to “sound events” so they can hear it too.

My block comes from if I have a character class and other class types which will subscribe to those events and I have multiple character objects how will my event dispatcher/message bus have communication to all of the individual objects?

  • Would there be a global event dispatcher?
  • Should the event dispatcher be a singleton?
  • Will the event dispatcher hold references to every individual object that it needs?
    • How can I keep this system decoupled and not have a unnecessarily large unmanageable event system?

I’m aware I can have a std::vector which can store objects of classtype character and dynamically call each ones OnEvent function, but should all my objects be communicating with one global event system? (A event system singleton maybe?)

\$\endgroup\$
  • \$\begingroup\$ Yeah, the standard way would be to have a singleton class with public events that the other classes could subscribe to. You could have multiple systems like that to keep everything tidy. \$\endgroup\$ – Milos Romanic Oct 9 at 8:25

Your Answer

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

Browse other questions tagged or ask your own question.