Sequence

The Sequence node is similar to an “and” operation. It will return failure as soon as one of its ‘flow’ return failure. If a ‘flow’ returns success then it will sequentially run the next ‘flow’. If all ‘flow’ return success then it will return success.

More Information:

  • This node can only be created from “State Graph” or function with return type: ‘IEnumerator‘.
  • This node is using Coroutine.

Examples

In this example, the Sequence node is called at start. It then call flow from up to bottom.

Program:

  • variable : ‘value’ = “a”
  • Validation 1 : compare variable ‘value’ if value is “a”
  • Validation 2 : compare variable ‘value’ if value is “b”
  • Validation 3 : compare variable ‘value’ if value is “c”
  • Action 4 : log an message “value is a”
  • Action 5 : log an message “value is b”
  • Action 6 : log an message “value is c”

The Sequence will never reach ‘Validation 3’ because the ‘Validation 2’ state is failure and Sequence will stop of execution and its state will failure.

Flow Graph:

Generated Script:

using UnityEngine;
using System.Collections.Generic;

public class Program : MonoBehaviour {
  public string value = "a";
  public EventCoroutine coroutine1;

  void Start() {
    coroutine1.Run();
  }

  bool ActivateEvent(string name) {
    switch(name) {
      case "Validation_1": {
        if(value == "a") {
          ActivateEvent("Action_4");
        }
      }
      break;
      case "Action_4": {
        Debug.Log("value is a");
      }
      break;
      case "Validation_2": {
        if(value == "b") {
          ActivateEvent("Action_5");
        }
      }
      break;
      case "Action_5": {
        Debug.Log("value is b");
      }
      break;
      case "Validation_3": {
        if(value == "c") {
          ActivateEvent("Action_6");
        }
      }
      break;
      case "Action_6": {
        Debug.Log("value is c");
      }
      break;
    }
    return true;
  }

  System.Collections.IEnumerable ActivateCoroutineEvent(string name) {
    switch(name) {
      case "Sequence_6": {
        if(!ActivateEvent("Validation_1")){
          yield return "Failure";
        }
        if(!ActivateEvent("Validation_2")){
          yield return "Failure";
        }
        if(!ActivateEvent("Validation_3")){
          yield return "Failure";
        }
      }
      break;
    }
    yield break;
  }

  void Awake() {
    coroutine1 = new EventCoroutine(this, ActivateCoroutineEvent("Sequence_6"));
  }
}

Output:

value is a
Was this helpful to you?