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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why IGraphElementWithData?

Discussion in 'Visual Scripting' started by ChiwTheNeko, Jul 1, 2022.

  1. ChiwTheNeko


    Mar 24, 2022

    I'm trying to write custom nodes for visual scripting and I noticed that existing nodes do not store their data in member variables. Instead they implement IGraphElementWithData and access their data using flow.stack.GetElementData(). Why is that?

    Is there a problem with storing data in member variables like this?
    Code (CSharp):
    1. public class MyNode : Unit
    2. {
    3.   private int mMyData;
    4. }
    I need my custom node to keep a reference to a C# script that is on the same GameObject than the script machine. I tried that:
    Code (CSharp):
    1. public class MyNode : Unit, IGraphEventListener
    2. {
    3.   private MyScript mMyScript;
    5.   public void StartListening(GraphStack stack)
    6.   {
    7.     mMyScript = stack.gameObject.GetComponent<MyScript>();
    8.   }
    9. }
    It seams to work. Is there a problem with this approach? Should I use IGraphElementWithData to keep the reference to my script instead?