Game Development Reference
In-Depth Information
Lines 05 and 06. Here, PostNotification searches the Listener Dictionary
keys for a matching event to see whether there are any listeners registered
for this event. If there's no matching key (event) in the dictionary, then this
event has no registered listeners. Therefore, PostNotification can exit
immediately, as there's nothing further to do.
2.
Lines 08-10. PostNotification uses a C# ForEach loop to iterate
through all registered listeners for this event. Notice that the event
element in the Dictionary is accessed using the standard C# array syntax:
Listeners[NotificationName] . For each registered listener, the SendMessage
function is called to notify the object of the event occurrence. This is the
crucial line, notifying an object of an event. The SendMessage function is
inherited from the Component object and is a part of the Unity API. It's an
important method because it allows you to call any function on an active
object through its Component interface, simply by referring to the function
name as a string argument. You don't need to know about the function's
return type or even its argument list. You just need to know the function's
name, and Unity does the rest to make the function call possible. The next
section considers SendMessage in more detail.
3.
SendMessage and BroadcastMessage
When working in Unity, and with the component-based paradigm , (or with entity-based
programming , as it's sometimes called), the chances are high that'll you be creating a lot of C# script
files and adding them to your game objects as components. The components you create, which
are classes, will be your own custom types, typically derived from MonoBehaviour , or from other
descendent classes. As such, your classes will support a range of different variables and functions
to define their own behavior, making each class specific and unique. This is to be expected.
However, this raises a problem in C#, which is a strictly typed language. Specifically, there are often
times when you'll want to work with many game objects together, invoking functions and behavior
on their components without having to know or worry about their specific data type and interface.
For example, due to a destructive explosion event in your game, you may want to destroy a batch
of different but nearby objects clustered together in the scene, such as enemies, power-ups, props,
and maybe even scenery parts.
Before destroying the objects, however, it'd be useful to call a function or event on them all, notifying
them about their impending doom, so each has the opportunity to respond appropriately. Maybe
some objects play a destruction sound, while others flash red. In these cases, you'll want to call
a function on all the objects, but without having to know anything specific about their component
classes or data types. You simply want to invoke a function in all components across multiple
objects, whatever their data type and interface may be. The SendMessage and BroadcastMessage
functions both allow you to do this in different ways.
 
Search WWH ::




Custom Search