Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add a Start method for every time a composite runs the first time. #64

ashblue started this conversation in Ideas
Discussion options

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 after Reset() 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; 
 }
 }
 }
}
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /