Example #1
0
        /// <summary>
        /// Sends a message to listeners.
        /// </summary>
        /// <param name="sender">Object/info about object that's sending the message.</param>
        /// <param name="target">Intended recipient, or null for any.</param>
        /// <param name="message">Message.</param>
        /// <param name="parameter">Message parameter.</param>
        /// <param name="values">Any number of additional values to send with message.</param>
        public static void SendMessageWithTarget(object sender, object target, string message, string parameter, params object[] values)
        {
            if (!(Application.isPlaying || sendInEditMode))
            {
                return;
            }
            if (debug)
            {
                Debug.Log("MessageSystem.SendMessage(sender=" + sender +
                          ((target == null) ? string.Empty : (" target=" + target)) +
                          ": " + message + "," + parameter + ")");
            }
            var messageArgs = new MessageArgs(sender, target, message, parameter, values); // struct passed on stack; no heap allocated.

            for (int i = 0; i < listenerInfo.Count; i++)
            {
                var x = listenerInfo[i];
                if (string.Equals(x.message, message) && (string.Equals(x.parameter, parameter) || string.IsNullOrEmpty(x.parameter)))
                {
                    try
                    {
                        x.listener.OnMessage(messageArgs);
                    }
                    catch (System.Exception e)
                    {
                        Debug.LogError("Message System exception sending '" + message + "'/'" + parameter + "' to " + x.listener + ": " + e.Message);
                    }
                }
            }
        }
        /// <summary>
        /// Sends a message to listeners.
        /// </summary>
        /// <param name="sender">Object/info about object that's sending the message.</param>
        /// <param name="target">Intended recipient, or null for any.</param>
        /// <param name="message">Message.</param>
        /// <param name="parameter">Message parameter.</param>
        /// <param name="values">Any number of additional values to send with message.</param>
        public static void SendMessageWithTarget(object sender, object target, string message, string parameter, params object[] values)
        {
            if (!(Application.isPlaying || sendInEditMode))
            {
                return;
            }
            if (debug || ShouldLogSender(sender))
            {
                Debug.Log("MessageSystem.SendMessage(sender=" + sender +
                          ((target == null) ? string.Empty : (" target=" + target)) +
                          ": " + message + "," + parameter + ")");
            }
            var messageArgs = new MessageArgs(sender, target, message, parameter, values); // struct passed on stack; no heap allocated.

            try
            {
                sendMessageDepth++;
                for (int i = 0; i < listenerInfo.Count; i++)
                {
                    var x = listenerInfo[i];
                    if (x == null || x.removed)
                    {
                        continue;
                    }
                    if (x.listener == null)
                    {
                        x.removed = true;
                        continue;
                    }
                    if (string.Equals(x.message, message) && (string.Equals(x.parameter, parameter) || string.IsNullOrEmpty(x.parameter)))
                    {
                        try
                        {
                            if (ShouldLogReceiver(x.listener))
                            {
                                Debug.Log("MessageSystem.SendMessage(sender=" + sender +
                                          ((target == null) ? string.Empty : (" target=" + target)) +
                                          ": " + message + "," + parameter + ")");
                            }
                            x.listener.OnMessage(messageArgs);
                        }
                        catch (System.Exception e)
                        {
                            Debug.LogError("Message System exception sending '" + message + "'/'" + parameter + "' to " + x.listener + ": " + e.Message);
                        }
                    }
                }
            }
            finally
            {
                sendMessageDepth--;
                if (sendMessageDepth == 0)
                {
                    RemoveMarkedListenerInfo();
                }
            }
        }
 public void OnMessage(PixelCrushers.MessageArgs messageArgs)
 {
     switch (messageArgs.message)
     {
     case "TeachRecipe":
         TeachRecipe(messageArgs.firstValue.ToString());
         break;
     }
 }
 public void OnMessage(MessageArgs messageArgs)
 {
     for (int i = 0; i < messagesToListenFor.Length; i++)
     {
         var messageEvent = messagesToListenFor[i];
         if (IsParticipantOk(messageEvent.requiredSender, messageArgs.sender) &&
             IsParticipantOk(messageEvent.requiredTarget, messageArgs.target) &&
             string.Equals(messageEvent.message, messageArgs.message) && string.Equals(messageEvent.parameter, messageArgs.parameter))
         {
             messageEvent.onMessage.Invoke(messageArgs);
         }
     }
 }
Example #5
0
 /// <summary>
 /// Handles messages requesting to change the value of the data source. On receipt of the
 /// RequestDataSourceChangeValueMessage, invokes the onRequestDataSourceChangeValue event,
 /// passing the first argument of the message as the new value.
 /// </summary>
 /// <param name="messageArgs">First argument is the new value.</param>
 public void OnMessage(MessageArgs messageArgs)
 {
     onRequestDataSourceChangeValue.Invoke(messageArgs.firstValue);
 }