public void ProcessFromQueue() { if (Target == null || !Target.IsValid) { return; } Target.TryDispatch(ref this); }
/// <summary> /// Sends a message to the messenger on the given GameObject. /// </summary> public void Send(GameObject inTarget, Message inMessage) { if (m_Manager.DebugMode) { if (m_Destroyed) { throw new InvalidOperationException("Messenger has already been destroyed"); } if (!inMessage.Type) { throw new ArgumentException("Provided MsgType is null."); } if (inTarget == null) { throw new ArgumentNullException(); } } MessengerImpl messenger = m_Manager.Find(inTarget); if (messenger == null) { if (m_Manager.DebugMode) { if (m_Manager.Database.Find(inMessage.Type).HasFlags(MsgFlags.RequireHandler)) { throw new Exception("MsgType " + inMessage.Type.ToString() + " requires a handler"); } } return; } MessageWrapper wrapper = new MessageWrapper(messenger, inMessage); if (m_Manager.DebugMode) { if (wrapper.Metadata.CanLog(LogFlags.Dispatch)) { m_Manager.Log(Owner.name + " sending message " + inMessage.ToString() + " to " + inTarget.name); } } if ((wrapper.Metadata.Dispatch & MsgDispatch.Immediate) != 0) { messenger.TryDispatch(ref wrapper); } else { m_Manager.QueueMessage(ref wrapper); } }
public void Invoke(ref MessageWrapper inMessage) { if (m_Handlers != null) { m_Handlers(inMessage.Message); } if (m_Actions != null) { m_Actions(); } if (m_Typed != null) { m_Typed.Invoke(ref inMessage); } if (m_Children != null) { int prevDispatchChildren = ++m_DispatchChildrenDepthAndVersion; int childLength = m_Children.Count; for (int i = 0; i < childLength; ++i) { MessengerImpl impl = m_Children[i]; if (impl != null) { impl.TryDispatch(ref inMessage); } } --m_DispatchChildrenDepthAndVersion; // Call earliest in the stack will have prevDispatchChildren set to 1. // If this is the earliest call and it's not the expected value of 0 by this point, // that means the list has changed and we should clean up the null children if (prevDispatchChildren == 1 && m_DispatchChildrenDepthAndVersion != 0) { for (int i = m_Children.Count - 1; i >= 0; --i) { if (m_Children[i] == null) { m_Children.RemoveAt(i); } } m_DispatchChildrenDepthAndVersion = 0; } } }
/// <summary> /// Broadcasts a message from the root messenger. /// </summary> public void Broadcast(Message inMessage) { if (m_Parent == null) { Send(this, inMessage); return; } if (m_Manager.DebugMode) { if (m_Destroyed) { throw new InvalidOperationException("Messenger has already been destroyed"); } if (!inMessage.Type) { throw new ArgumentException("Provided MsgType is null."); } } MessengerImpl root = m_Parent; while (root.m_Parent != null) { root = root.m_Parent; } MessageWrapper wrapper = new MessageWrapper(root, inMessage); if (m_Manager.DebugMode) { if (wrapper.Metadata.CanLog(LogFlags.Dispatch)) { m_Manager.Log(Owner.name + " broadcasting message " + inMessage.ToString()); } } if ((wrapper.Metadata.Dispatch & MsgDispatch.Immediate) != 0) { root.TryDispatch(ref wrapper); } else { m_Manager.QueueMessage(ref wrapper); } }
/// <summary> /// Sends a message to the given messenger. /// </summary> public void Send(MessengerImpl inTarget, Message inMessage) { if (m_Manager.DebugMode) { if (m_Destroyed) { throw new InvalidOperationException("Messenger has already been destroyed"); } if (!inMessage.Type) { throw new ArgumentException("Provided MsgType is null."); } if (inTarget == null) { throw new ArgumentNullException(); } } MessageWrapper wrapper = new MessageWrapper(inTarget, inMessage); if (m_Manager.DebugMode) { if (wrapper.Metadata.CanLog(LogFlags.Dispatch)) { m_Manager.Log(Owner.name + " sending message " + inMessage.ToString() + " to " + inTarget.Owner.name); } } if ((wrapper.Metadata.Dispatch & MsgDispatch.Immediate) != 0) { inTarget.TryDispatch(ref wrapper); } else { m_Manager.QueueMessage(ref wrapper); } }