ForLoopNode.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GraphProcessor;
  5. using System.Linq;
  6. using NodeGraphProcessor.Examples;
  7. [System.Serializable, NodeMenuItem("Conditional/ForLoop")]
  8. public class ForLoopNode : ConditionalNode
  9. {
  10. [Output(name = "Loop Body")]
  11. public ConditionalLink loopBody;
  12. [Output(name = "Loop Completed")]
  13. public ConditionalLink loopCompleted;
  14. public int start = 0;
  15. public int end = 10;
  16. [Output]
  17. public int index;
  18. public override string name => "ForLoop";
  19. protected override void Process() => index++; // Implement all logic that affects the loop inner fields
  20. public override IEnumerable< ConditionalNode > GetExecutedNodes() => throw new System.Exception("Do not use GetExecutedNoes in for loop to get it's dependencies");
  21. public IEnumerable< ConditionalNode > GetExecutedNodesLoopBody()
  22. {
  23. // Return all the nodes connected to the executes port
  24. return outputPorts.FirstOrDefault(n => n.fieldName == nameof(loopBody))
  25. .GetEdges().Select(e => e.inputNode as ConditionalNode);
  26. }
  27. public IEnumerable< ConditionalNode > GetExecutedNodesLoopCompleted()
  28. {
  29. // Return all the nodes connected to the executes port
  30. return outputPorts.FirstOrDefault(n => n.fieldName == nameof(loopCompleted))
  31. .GetEdges().Select(e => e.inputNode as ConditionalNode);
  32. }
  33. }