-
-
Couldn't load subscription status.
- Fork 121
-
Would be nice if composites had an OnStart method to simplify cached setup logic.
Requirements
Start()runs the first time a composite is ticked- When a composite is ticked again it does not run
Start() - Composite runs
Start()when ticked afterReset()has been called
Example Implementation on https://github.com/ashblue/fluid-behavior-tree/blob/develop/Assets/com.fluid.behavior-tree/Runtime/TaskParents/Composites/SelectorRandom.cs
namespace CleverCrow.Fluid.BTs.TaskParents.Composites { public class SelectorRandom : CompositeBase { private bool _init; public override string IconPath { get; } = $"{PACKAGE_ROOT}/LinearScale.png"; protected override TaskStatus OnUpdate () { if (!_init) { ShuffleChildren(); _init = true; } for (var i = ChildIndex; i < Children.Count; i++) { var child = Children[ChildIndex]; switch (child.Update()) { case TaskStatus.Success: return TaskStatus.Success; case TaskStatus.Continue: return TaskStatus.Continue; } ChildIndex++; } return TaskStatus.Failure; } public override void Reset () { base.Reset(); ShuffleChildren(); } private void ShuffleChildren () { var rng = new Random(); var n = Children.Count; while (n > 1) { n--; var k = rng.Next(n + 1); var value = Children[k]; Children[k] = Children[n]; Children[n] = value; } } } }
Would be simplified as the following:
namespace CleverCrow.Fluid.BTs.TaskParents.Composites { public class SelectorRandom : CompositeBase { public override string IconPath { get; } = $"{PACKAGE_ROOT}/LinearScale.png"; protected override void OnStart () { ShuffleChildren(); } protected override TaskStatus OnUpdate () { for (var i = ChildIndex; i < Children.Count; i++) { var child = Children[ChildIndex]; switch (child.Update()) { case TaskStatus.Success: return TaskStatus.Success; case TaskStatus.Continue: return TaskStatus.Continue; } ChildIndex++; } return TaskStatus.Failure; } private void ShuffleChildren () { var rng = new Random(); var n = Children.Count; while (n > 1) { n--; var k = rng.Next(n + 1); var value = Children[k]; Children[k] = Children[n]; Children[n] = value; } } } }
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment