I’ll give you an example. All my enemies, players, and even debris and obstacles have the ability to harm. May that harm be they shot a projectile, they have a bump surface (spikes), they swung a sword, they have an AOE (fire, electric field), whatever. They all have scripts that take care of this action, BUT this script may or may not be the same class, or have a consistent interface, or even position in the entities hierarchy. The sword could be in the hand bone of a model, the bump surface a collider right on the root of the entity. We don’t know.
When this script operates that it’s striking something, it needs to inform both the entire entity that this happened, and the entity that it happened. An “OnStrike” notification.
This could be seen as similar to ‘OnCollision’, both the parties involved are sent this message.
So the entity being struck has a script somewhere in its hierarchy (for organizational purposes) that needs to react to this. It needs to receive this message. Also the entity that does the striking has some sound effect that plays on it, and this is done in a different script than the script performing the strike.
So, BOTH entities have a need to listen for this event to occur, but doesn’t know exactly who in each other’s hierarchy is signaling it (especially not the one being struck, since it didn’t do the signaling).
So the play sound effect script registers with its root that it wants to be told about any time it strikes something.
And the got hit reaction script registers with its root that it wants to be told if its been struck.
Then the striking script signals the roots of both its own entity, and the entity it struck, that this occurred. And as a result these observers perform the task desired.
//in sword script
void OnTriggerEnter(Collider other)
{
//determine if collision is a legite sword strike, calculate damage, strikingEntity, strikingWeapon, and who was struck
var n = new WeaponStruckNotification(damage, strikingEntity, strikingWeapon, struckObject);
Notification.PostNotification<WeaponStruckNotification>(this, n, true);
Notification.PostNotification<WeaponStruckNotification>(struckObject, n, true);
}
//in struckObject reaction script
void Start()
{
Notification.RegisterObserver<WeaponStruckNotification>(this.FindRoot(), this.OnWeaponStruck);
}
void OnWeaponStruck(WeaponStruckNotification n)
{
this.Health -= n.Damage;
if(n.StrikingWeapon.HasKnockback)
{
//do knockback
}
}
//in entity that did the striking
void Start()
{
Notification.RegisterObserver<WeaponStruckNotification>(this.FindRoot(), this.OnWeaponStruck);
}
void OnWeaponStruck(WeaponStruckNotification n)
{
//play sound effect
}
I wouldn’t say its reinventing the wheel. It’s taking the decouple nature of ‘messages’, and adding the type safe nature of ‘.net events’, and expanding on both by giving it an understanding of my ‘entity hierarchy design’. It’s its own deal that perform a task that I find very useful… static events can’t really do that without some mediator in between handling who receives what messages…
I also have another system in my framework (no source available for this one) that basically allows design time, no code, registering for various events that then trigger some other component I call ‘triggerables’ (it’s oddly a lot like the ‘event’ system unity released in their new UI system, but before I even saw their new UI system). And these Notifications integrate easily with them as well.
Anywho, if you don’t see the benefit of it, that’s fine. Either you don’t need this in your designs, or maybe I’m just not describing it well enough. At the end of the day… I have use for it, someone else might (I’ve seen this design done before, just not as robustly… like here: http://wiki.unity3d.com/index.php?title=NotificationCenter ). Don’t use it.
I merely shared it because OP was talking about basically doing what I had already done. Figured they’d find looking at what I created useful.