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 invoke a control output without an controlInput?

Discussion in 'Visual Scripting' started by Songh0318, Sep 6, 2021.

  1. Songh0318

    Songh0318

    Joined:
    Nov 29, 2016
    Posts:
    6
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,763
  3. Songh0318

    Songh0318

    Joined:
    Nov 29, 2016
    Posts:
    6
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,763
  5. MasterSubby

    MasterSubby

    Joined:
    Sep 5, 2012
    Posts:
    252
    You'll want to inherit from ManualEventUnit, and Mark it with the attribute UnityCategory. Make sure the root of that path starts with "Events/".

    From there you can override Start and Stop Listening methods with your event/delegate. Here you can bind to them and Unbind. Those methods have a GraphStack or Reference as a parameter. Use this to access your game object, components, ect.

    I'll look into making a tutorial for creating event units.
     
  6. Songh0318

    Songh0318

    Joined:
    Nov 29, 2016
    Posts:
    6
    Thanks for the replies. The following code worked for me.

    Code (CSharp):
    1. public class SelectTarget : Unity.VisualScripting.ManualEventUnit<Unit>
    2.     {
    3.         protected override string hookName => "Target Selected";
    4.  
    5.         [DoNotSerialize]
    6.         public ValueOutput Target;
    7.         Unit _unit;
    8.  
    9.         CellGrid _cellGrid;
    10.         GraphReference _graph;
    11.  
    12.         protected override void Definition()
    13.         {
    14.             base.Definition();
    15.             Target = ValueOutput<Unit>("Target", (flow) => _unit);
    16.         }
    17.  
    18.         public override void StartListening(GraphStack stack)
    19.         {
    20.             base.StartListening(stack);
    21.             _graph = stack.AsReference();
    22.             _cellGrid = stack.gameObject.GetComponent<CellGrid>();
    23.             _cellGrid.UnitClicked -= OnUnitClicked;
    24.             _cellGrid.UnitClicked += OnUnitClicked;
    25.         }
    26.  
    27.         private void OnUnitClicked(object sender, EventArgs e)
    28.         {
    29.             _unit = sender as Unit;
    30.             Trigger(_graph, sender as Unit);
    31.         }
    32.  
    33.         public override void StopListening(GraphStack stack)
    34.         {
    35.             base.StopListening(stack);
    36.             _cellGrid.UnitClicked -= OnUnitClicked;
    37.         }
    38.     }
    upload_2021-9-8_22-37-20.png
     
  7. Songh0318

    Songh0318

    Joined:
    Nov 29, 2016
    Posts:
    6
    Another problem emerged from this.

    A ManualEventUnit has one built-in ControlOutput that can be triggered using EventUnit.Trigger(GraphReference, args).

    How do I trigger additional control outputs? Say I want to trigger a cancel controloutput in addition to the completed controloutput.
     
  8. inferno222

    inferno222

    Joined:
    Apr 29, 2014
    Posts:
    6
    In order to have custom ControlOutputs, you can:
    Code (CSharp):
    1. Flow flow = Flow.New(graph_);
    2. // IF method
    3. flow.Run(output);
    4. // IF coroutine
    5. flow.StartCoroutine(output);
    For some reason flow.isCoroutine is not working on my end so there might be something wrong with my approach.