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

Getting the game object that owns the State Machine, from a Custom Unit in C#

Discussion in 'Visual Scripting' started by george_unity375, Oct 16, 2021.

  1. george_unity375

    george_unity375

    Joined:
    Oct 12, 2021
    Posts:
    1
    Hi there,

    I'm new to the Visual Scripting system. One of the first things I'm trying to do is creating some custom units to drive some high level behaviour. I'd like to access the game object that owns the State Machine component that is currently running inside my Unit.... is there an elegant way to do this?

    here's a code snippet...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.VisualScripting;
    5.  
    6. public class DebugLogNode : Unit
    7. {
    8.     [DoNotSerialize]
    9.     public ControlInput InputTrigger;
    10.  
    11.     [DoNotSerialize]
    12.     public ControlOutput OutputTrigger;
    13.  
    14.     [DoNotSerialize]
    15.     public ValueInput Message;
    16.  
    17.     protected override void Definition()
    18.     {
    19.         InputTrigger = ControlInput("InputTrigger", Execute);
    20.  
    21.         OutputTrigger = ControlOutput("OutputTrigger");
    22.  
    23.         Message = ValueInput<string>("Message", "Hello ");
    24.     }
    25.  
    26.     private ControlOutput Execute(Flow flow)
    27.     {
    28.         Debug.Log("[Log] " + flow.GetValue<string>(Message));
    29.  
    30.         // TODO get my game object that this state machine is running
    31.         //GameObject gameObject = ???
    32.         return OutputTrigger;
    33.     }
    34. }
    35.  
    Thanks in advance,
    George
     
  2. bearcoree

    bearcoree

    Joined:
    Mar 8, 2016
    Posts:
    72
    I've been stuck at the same point. Coming from PlayMaker, doing this was very easy. It seems to be impossible in Bolt as far as I can tell though.

    The workaround I found is to
    Code (CSharp):
    1. public ControlInput inputTrigger;
    2. public ControlOutput outputTrigger;
    3. public ValueInput ObjectInput;
    4. protected override void Definition()
    5. {
    6.    ObjectInput = ValueInput<YourType>("ObjectInput", null);
    7.    inputTrigger = ControlInput("inputTrigger", (flow) =>
    8.    {
    9.       // do something with obj
    10.       var obj = flow.GetValue<YourType>(ObjectInput);
    11.       return outputTrigger;
    12.    });
    13. }
    (Untested code)

    And then linking up the ObjectInput to the "This" Node in the graph. It'd be much easier for me to just have access to the GameObject hosting this graph.
     
  3. MasterSubby

    MasterSubby

    Joined:
    Sep 5, 2012
    Posts:
    252
    You can get the data, and it actually is very easy. Just need to know where it lies. You can get all sorts of data by accessing the current graph stack from your method or lambda func.
    Code (CSharp):
    1. flow.stack.gameObject