Search Unity

Resolved How do Bolt nodes reference components?

Discussion in 'Visual Scripting' started by SamTheLearned, Jan 15, 2022.

  1. SamTheLearned

    SamTheLearned

    Joined:
    Jun 23, 2021
    Posts:
    85
    Visual scripting intrigues me so I am dipping my toes into it. Something right away that confuses me is how nodes in Visual Scripting reference components. It seems automatic compared to me writing out a custom c# script.

    Code (CSharp):
    1. Rigidbody2D rb;
    2.  
    3.     void Start()
    4.     {
    5.         rb = GetComponent<Rigidbody2D>();
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         float horizontal = Input.GetAxisRaw("Horizontal");
    11.  
    12.         rb.velocity = new Vector2(horizontal, rb.velocity.y);
    13.     }
    Here is extremely simple code I have to move a sprite. I obviously have to reference the Rigidbody component to access it's functions.

    Here is the same setup in Bolt Visual Scripting.



    I can click on the node and see Target: Rigidbody2D - The Target Object which I assume is the reference to the Rigidbody on the GameObject that the FlowMachine is attached to. So its automatic reference?
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    Yes, it's automatic via the process called reflection. You just need to feed in a GameObject or one of its components as an input and the node will grab whatever component it requires from the same GameObject.

    By default nodes reference "Self" ie "this". So it references the object the graph is on, you can also point it to any other object.
     
    SamTheLearned likes this.