Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to use signals outside of Timeline

Discussion in 'Timeline' started by Ziplock9000, Jul 18, 2023.

  1. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    I want to use Signals outside of timelines to broadcast when events have been triggered. Yes I could make my own system, but why have multiple systems when I'm already using signals for timeline and it just so happens I need to talk to the same objects anyway and have a signalreviever on them already.

    My specific use case is when a player walks over a certain area I want to trigger a signal that will present the player with a quest.

    However, neither the SignalAsset or SignalEmitter seem to have a method I can call to trigger or emit the signal?
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    105
    The signal is sent over
    INotificationReceiver.OnNotify
    , so you can call it in your collider area trigger script.
     
  3. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    I'm not sure what you mean.
    INotificationReceiver.OnNotify
    is an callback triggered at the receiver end, but I want to know to emit a signal in the first place.

    In my collider script I have a reference to a SignalAsset, what method on that object can I call to emit the signal? or method on something else and pass the signal in.

    So per your suggestion, how do I call
    INotificationReceiver.OnNotify

    in my collider?
     
  4. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    105
    Triggering an event is simply just a "call the callback" process, so just call it in your OnTriggerEnter or other. Here's an example:
    Code (CSharp):
    1.  
    2. public class SignalArea : MonoBehaviour
    3. {
    4.     [SerializeField] private SignalAsset signalToSend;
    5.     private SignalEmitter runtimeEmitter;
    6.  
    7.     private void Awake()
    8.     {
    9.         runtimeEmitter = ScriptableObject.CreateInstance<SignalEmitter>();
    10.         runtimeEmitter.asset = signalToSend;
    11.     }
    12.  
    13.     private void OnDestroy()
    14.     {
    15.         Destroy(runtimeEmitter);
    16.     }
    17.  
    18.     private void OnTriggerEnter(Collider other)
    19.     {
    20.         // check if there is a receiver on the other object
    21.         // your player needs a SignalReceiver component to get event from this component
    22.         if (!other.TryGetComponent<SignalReceiver>(out var receiver)) return;
    23.  
    24.         // sends the signal
    25.         receiver.OnNotify(default, runtimeEmitter, default);
    26.     }
    27. }