Game Development Reference
In-Depth Information
20 if(!Listeners.ContainsKey(NotificationName))
21 Listeners.Add (NotificationName, new List<Component>());
22
23 //Add object to listener list for this notification
24 Listeners[NotificationName].Add(Sender);
25 }
26 //------------------------------------------------
27 //Function to remove a listener for a notification
28 public void RemoveListener(Component Sender, string NotificationName)
29 {
30 //If no key in dictionary exists, then exit
31 if(!Listeners.ContainsKey(NotificationName))
32 return;
33
34 //Cycle through listeners and identify component, and then remove
35 for(int i = Listeners[NotificationName].Count-1; i>=0; i--)
36 {
37 //Check instance ID
38 if(Listeners[NotificationName][i].GetInstanceID() == Sender.
GetInstanceID())
39 Listeners[NotificationName].RemoveAt(i); //Matched. Remove from
list
40 }
41 }
42 //------------------------------------------------
43 //Function to post a notification to a listener
44 public void PostNotification(Component Sender, string NotificationName)
45 {
46 //If no key in dictionary exists, then exit
47 if(!Listeners.ContainsKey(NotificationName))
48 return;
49
50 //Else post notification to all matching listeners
51 foreach(Component Listener in Listeners[NotificationName])
52 Listener.SendMessage(NotificationName, Sender, SendMessageOptions.
DontRequireReceiver);
53 }
54 //------------------------------------------------
55 //Function to clear all listeners
56 public void ClearListeners()
57 {
58 //Removes all listeners
59 Listeners.Clear();
60 }
61 //------------------------------------------------
62 //Function to remove redundant listeners - deleted and removed listeners
63 public void RemoveRedundancies()
64 {
65 //Create new dictionary
66 Dictionary<string, List<Component>> TmpListeners = new Dictionary<string,
List<Component>>();
67
Search WWH ::




Custom Search