Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Help Wanted How do I start a coroutine in Bolt and wait for it to finish?

Discussion in 'Visual Scripting' started by Jonas-Neuston, Nov 26, 2020.

  1. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    25
    In a Bolt flow graph, how do I start a coroutine and wait for it to finish?

    I've looked through the Bolt documentation and forum posts but I can't find a good solution.

    Bolt 1.4.12
    Unity 2019.4.5f1
     
  2. Ex-Crow

    Ex-Crow

    Joined:
    Aug 14, 2020
    Posts:
    111
    All green flow starter event nodes like Start, Update, On Enable, etc have a "Coroutine" checkbox in the Graph Inspector.

    They start and end automatically with the checkbox ticked in.

    Nodes like Timer and Cooldown have built in coroutines so they don't need to be placed inside coroutines manually.
     
  3. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    25
    Thank you for replying @Ex-Crow, but that doesn't really seem to apply to my case. I'll try to describe my situation further:

    Imagine I have a component like this:
    Code (CSharp):
    1. public class CarAttackDirector : MonoBehaviour
    2. {
    3.     public IEnumerator RunOneAttack()
    4.     {
    5.         // Do stuff in here
    6.     }
    7. }

    I then want to be start the RunOneAttack method as a coroutine, from the Bolt flow graph. Later, when the coroutine finishes I want to execute some other logic in the Bolt flow graph.

    Thank you
     
  4. Ex-Crow

    Ex-Crow

    Joined:
    Aug 14, 2020
    Posts:
    111
  5. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    25
    Okay, thanks for clarifying that. I'll write a custom node.
     
  6. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    25
    I'm now using the following code to start coroutines from a Bolt flow graph in my upcoming game Arcano:
    Code (CSharp):
    1. using Bolt;
    2. using Ludiq;
    3. using System.Collections;
    4. using UnityEngine;
    5.  
    6. namespace Neuston.Bolt
    7. {
    8.     public class CoroutineRunnerUnit : Unit
    9.     {
    10.         [DoNotSerialize] public ControlInput start;
    11.         [DoNotSerialize] public ValueInput methodName;
    12.         [DoNotSerialize] public ValueInput subject;
    13.         [DoNotSerialize] public ControlOutput complete;
    14.  
    15.         protected override void Definition()
    16.         {
    17.             start = ControlInputCoroutine("start", RunCoroutine);
    18.             methodName = ValueInput<string>("methodName", string.Empty);
    19.             subject = ValueInput<MonoBehaviour>("subject", null);
    20.             complete = ControlOutput("complete");
    21.         }
    22.  
    23.         IEnumerator RunCoroutine(Flow flow)
    24.         {
    25.             var c = flow.GetValue<MonoBehaviour>(subject);
    26.             var meth = flow.GetValue<string>(methodName);
    27.             yield return c.StartCoroutine(meth);
    28.             yield return complete;
    29.         }
    30.     }
    31. }
     
    m3rt32 likes this.
unityunity