The Selector node is similar to an “or” operation. It will return success as soon as one of its ‘flow’ return success. If a ‘flow’ returns failure then it will sequentially run the next ‘flow’. If no ‘flow’ returns 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 Selector node is called at start. It then call flow from up to bottom.
Program:
- variable : ‘value’ = “b”
- 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 Selector will never reach ‘Validation 3’ because the ‘Validation 2’ state is success and Selector will stop.
In this examples i use validation node because its more readable and more organized for high level logic.
Flow Graph:
Generated Script:
using UnityEngine; using System.Collections.Generic; public class Program : MonoBehaviour { public string variable0 = "b"; public EventCoroutine coroutine1; void Start() { coroutine1.Run(); } bool ActivateEvent(string name) { switch(name) { case "Validation_1": { if(variable0 == "a") { ActivateEvent("Action_4"); } } break; case "Action_4": { Debug.Log("value is a"); } break; case "Validation_2": { if(variable0 == "b") { ActivateEvent("Action_5"); } } break; case "Action_5": { Debug.Log("value is b"); } break; case "Validation_3": { if(variable0 == "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 "Selector_0": { if(ActivateEvent("Validation_1")) { yield return "Success"; } if(ActivateEvent("Validation_2")) { yield return "Success"; } if(ActivateEvent("Validation_3")) { yield return "Success"; } yield return "Failure"; } break; } yield break; } void Awake() { coroutine1 = new EventCoroutine(this, ActivateCoroutineEvent("Selector_0")); } }
Output:
value is b