RandomSelector

The RandomSelector  node is Similar to the selector node, the random selector will return success as soon one of its flow returns success. The difference is that the random selector will run its flow in a random order. If no flow return success then it will return failure.

More Information:

  • This node can only be created from “StateFlow” Graph.
  • This node is using Coroutine.

Examples

In this example, the RandomSelector node is called at start. It then call flow in random order because all the flow will return success the node will only call one of 3 action in random order.

Program:

  • Action 4 : log an message “first action is called”
  • Action 5 : log an message “second action is called”
  • Action 6 : log an message “last action is called”

Flow Graph:

Generated Script:

using UnityEngine;
using System.Collections.Generic;

public class Program : MonoBehaviour {
  public EventCoroutine coroutine1;

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

  bool ActivateEvent(string name) {
    switch(name) {
      case "Action_4": {
        Debug.Log("first action is called");
      }
      break;
      case "Action_5": {
        Debug.Log("second action is called");
      }
      break;
      case "Action_6": {
        Debug.Log("last action is called");
      }
      break;
    }
    return true;
  }

  System.Collections.IEnumerable ActivateCoroutineEvent(string name) {
    switch(name) {
      case "Selector_0": {
        List<int> eventIndex1 = new List<int>() { 0, 1, 2};
        List<int> randomOrder1 = new List<int>();
        for(int i1 = 2; i1 >= 0; --i1) {
          int indexs1 = Random.Range(0, i1);
          randomOrder1.Add(eventIndex1[indexs1]);
          eventIndex1.RemoveAt(indexs1);
        }
        for(int loop1 = 0; loop1 < 3; loop1++) {
          int index1 = randomOrder1[loop1];
          if(index1 == 0) {
            if(ActivateEvent("Action_4")) {
              yield return "Success";
            }
          } else if(index1 == 1) {
            if(ActivateEvent("Action_5")) {
              yield return "Success";
            }
          } else if(index1 == 2) {
            if(ActivateEvent("Action_6")) {
              yield return "Success";
            }
          }
          yield return null;
        }
        yield return "Failure";
      }
      break;
    }
    yield break;
  }

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

Output:

second action is called

The real output can be different when you play again because its call flow in random order.

Was this helpful to you?

Leave a Reply

Your email address will not be published. Required fields are marked *