FlowCanvas Forums › Support › Signals from/to C# script › Reply To: Signals from/to C# script
Hello,
The two of them work differently. Here are example code with explanation comments for you.
SIGNAL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using NodeCanvas.Framework; using UnityEngine; public class ExampleSignal : MonoBehaviour { public SignalDefinition signal; //To catch, subscribe to the signal void OnEnable() { signal.onInvoke += OnSinglaInvoke; } void OnDisable() { signal.onInvoke -= OnSinglaInvoke; } //Called when signal is invoked void OnSinglaInvoke(Transform sender, Transform receiver, bool isGlobal, params object[] args) { //handling receiver and isGlobal is optional but this is what the nodes are doing if ( isGlobal || receiver == this.transform ) { Debug.Log("Sender = " + sender); Debug.Log("arg0 = " + args[0]); Debug.Log("arg1 = " + args[1]); } } //Invoke a signal in code void Update() { if ( Input.GetKeyDown(KeyCode.Space) ) { //invoke the signal only to be received by this transform as target receiver. signal.Invoke(this.transform, this.transform, false, "hello", 13f); //invoke the signal globaly without a specific target receiver. signal.Invoke(this.transform, null, true, "hello", 13f); } } } |
CUSTOM EVENT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using FlowCanvas; using ParadoxNotion; using ParadoxNotion.Services; using UnityEngine; public class ExampleCustomEvent : MonoBehaviour { public FlowScriptController graphOwner; //All custom events are handled through the EventRouter component. //To catch events in code we need to get or add an EventRouter if it does not exist already //on the gameobject of the graphowner that we want to catch events from. //Then subscribe to its onCustomEvent. void OnEnable() { var router = graphOwner.GetComponent<EventRouter>(); if ( router == null ) { router = graphOwner.gameObject.AddComponent<EventRouter>(); } router.onCustomEvent += OnCustomEventInvoke; } //To unsubscribe we only care if the EventRouter component was there in the first place. //If so, unsubscribe from its onCustomEvent. void OnDisable() { var router = graphOwner.GetComponent<EventRouter>(); if ( router != null ) { router.onCustomEvent -= OnCustomEventInvoke; } } //Called when the event is invoked/sent void OnCustomEventInvoke(string eventName, IEventData eventData) { Debug.Log("Sender = " + eventData.sender); Debug.Log("Event Value = " + eventData.valueBoxed); } //Send an event in code void Update() { if ( Input.GetKeyDown(KeyCode.Space) ) { //send an event to the graph owner with null value and this sender graphOwner.SendEvent("MyEventName", null, this); //send an event to the graph owner with a float value and this sender graphOwner.SendEvent<float>("MyEventName", 13f, this); } } } |
If you need any clarification please let me know. 🙂
Join us on Discord: https://discord.gg/97q2Rjh