Astra Child

How to receive a Signal

Estimated reading: 2 minutes 0 views

There are several ways of receiving a signal, either from code or from a Node. Below are some examples on how to do that.

Receive a signal in code

using Doozy.Runtime.Signals;
using UnityEngine;Code language: CSS (css)
Simple Signal (a ping)
using Doozy.Runtime.Signals;
using UnityEngine;
namespace Sandbox
{
    public class TestClass : MonoBehaviour
    {
        public string StreamCategory;            // Target stream category
        public string StreamName;                // Target stream name
        private SignalReceiver m_SignalReceiver; // Signal receiver
        private SignalStream m_SignalStream;     // Target stream

        private void Awake()
        {
            //get a reference to the target stream
            m_SignalStream = SignalStream.Get(StreamCategory, StreamName);

            //initialize the receiver and set its callback
            m_SignalReceiver = new SignalReceiver().SetOnSignalCallback(OnSignal);
        }

        private void OnEnable()
        {
            //add the receiver to react to signals sent through the stream
            m_SignalStream.ConnectReceiver(m_SignalReceiver);
        }

        private void OnDisable()
        {
            //remove the receiver from reacting to signals sent through the stream
            m_SignalStream.DisconnectReceiver(m_SignalReceiver);
        }

        private void OnSignal(Signal signal)
        {
            //do stuff
            //here you can also extract info from the signal
        }
    }
}Code language: PHP (php)
MetaSignal (a signal with a value payload)

Receive a Signal on the stream with the given stream category and name and retrieve its value.

{
    public class TestClass : MonoBehaviour
    {
        public string StreamCategory;            // Target stream category
        public string StreamName;                // Target stream name
        private SignalReceiver m_SignalReceiver; // Signal receiver
        private SignalStream m_SignalStream;     // Target stream

        private void Awake()
        {
            //get a reference to the target stream
            m_SignalStream = SignalStream.Get(StreamCategory, StreamName);

            //initialize the receiver and set its callback
            m_SignalReceiver = new SignalReceiver().SetOnSignalCallback(OnSignal);
        }

        private void OnEnable()
        {
            //add the receiver to react to signals sent through the stream
            m_SignalStream.ConnectReceiver(m_SignalReceiver);
        }

        private void OnDisable()
        {
            //remove the receiver from reacting to signals sent through the stream
            m_SignalStream.DisconnectReceiver(m_SignalReceiver);
        }

        private void OnSignal(Signal signal)
        {
           //check if signal is MetaSignal
           if (signal.hasValue)
           {
               Type valueType = signal.valueType; //get the payload value type

               //depending on what the value type is (it can be anything) and your use-case below are some examples

               //Example - your value is an int (it can be anything)
               {
                   int signalValue;

                   //Option 1 - get it via a direct cast (unsafe method - fastest)
                   signalValue = signal.GetValueUnsafe<int>();

                   //Option 2 - get it via a cast inside a try catch (safe method - slower)
                   if (signal.TryGetValue(out signalValue))
                   {
                       //do stuff
                   }

                   //Option 3 - cast the valueAsObject
                   signalValue = (int)signal.valueAsObject;
               }
           }
        }
    }
}Code language: HTML, XML (xml)
Share this Doc
CONTENTS
Scroll to Top