Game Development Reference
In-Depth Information
121 }
122 //-----------------------------------------------------------
123 //Called on scene change. Clean up dictionary
124 void OnLevelWasLoaded()
125 {
126 RemoveRedundancies();
127 }
128 //-----------------------------------------------------------
129 #endregion
130 }
More information on the
OnLevelWasLoaded
event can be
The following are the comments on the code sample 4-5:
•
Line 003
: Notice the addition of the
System.Collections.Generic
namespace giving us access to additional mono classes, including the
Dictionary
class. This class will be used throughout the
EventManager
class. More information on mono and its classes are explained later in
Chapter 6
,
Working with Mono
. In short, the
Dictionary
class is a special kind
of 2D array that allows us to store a database of values based on key-value
•
Line 007
: The
EventManager
class is derived from
MonoBehaviour
and
should be attached to an empty
GameObject
in the scene where it will
exist as a persistent singleton.
•
Line 024
: A private member variable
Listeners
is declared using a
Dictionary
class. This structure maintains a hash-table array of key-value
pairs, which can be looked up and searched like a database. The key-value
pairing for the
EventManager
class takes the form of
EVENT_TYPE
and
List<Component>
. In short, this means that a list of event types can be stored
(such as
HEALTH_CHANGE
), and for each type there could be none, one, or
more components that are listening and which should be notified when the
event occurs. In effect, the
Listeners
member is the primary data structure
on which the
EventManager
relies to maintain who is listening for what. For
more detailed information on the Mono Framework and common classes
within it, refer to
Chapter 6
,
Working with Mono
.