ISubscription subscription = notificationCenter.Subscribe<ShoutMessage>(this.HandleShoutMessage, this.CanHandleShoutMessage);
private void HandleShoutMessage(IMessage message, ISubscription subscription)
{
//…..
}
private void CanHandleShoutMessage(IMessage message)
{
return this.CurrentRoom == ((IPlayer)message.Sender).CurrentRoom;
}
notificationManager.Subscribe<PartyChatMessage>(this.OnPartyChatMessageReceived, this.CanReceivePartyChatMessage);
My objects would take a NotificationManager instance as a dependency in its constructor. My DI system is responsible for making sure that it provides a singleton instance of the NotificationManager. The other approach I have been playing with is just using eventing. This works fine, except that I have to make sure and provide all of my objects, a reference to the objects they need to subscribe to.
The nice part of the notification manager is that I can pattern out the way things communicate, by having all the objects that need to react to events, use the NotificationManager to subscribe for those events. Is there any downsides to this? It also feels safer, as I can unsubscribe from the NotificationManager singleton instead of trying to make sure I unregister my event handlers prior to an object being disposed. The NotificationManager also supports predicates, so that I can only handle messages that are applicable to what the object actually needs to see. An example is only handling chat messages that are sent from a player in the same room as the character receiving the message
What do you guys typically do for managing messages (chat, help, weather changes, npc movements, commanding etc) between objects in the engine?