Skip to content

Commit 52c25a3

Browse files
committed
Added section on custom composites
1 parent cc113a9 commit 52c25a3

File tree

1 file changed

+93
-8
lines changed

1 file changed

+93
-8
lines changed

README.md

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ will be more of the same. But each covers a different node type.
335335

336336
### Custom Actions
337337

338-
Custom action example template.
338+
You can create your own custom actions with the following template. This is useful for bundling up code
339+
that you're using constantly.
339340

340341
```C#
341342
using UnityEngine;
@@ -363,14 +364,16 @@ public class CustomAction : ActionBase {
363364
}
364365
```
365366

366-
Code required to modify your tree builder.
367+
Add your new node to a custom tree builder.
367368

368369
```C#
369370
using Adnc.FluidBT.Trees;
370371

371372
public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
372-
...
373-
public TreeBuilderCustom CustomAction (string name) {
373+
public TreeBuilderCustom (GameObject owner) : base(owner) {
374+
}
375+
376+
public TreeBuilderCustom CustomAction (string name = "Custom Action") {
374377
_tree.AddNode(Pointer, new CustomAction {
375378
Name = name
376379
});
@@ -382,7 +385,8 @@ public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
382385

383386
### Custom Conditions
384387

385-
Custom condition example template.
388+
Custom conditions can be added with the following example template. You'll want to use these for checks such as sight,
389+
if the AI can move to a location, and other tasks that require a complex check.
386390

387391
```C#
388392
using UnityEngine;
@@ -410,14 +414,16 @@ public class CustomCondition : ConditionBase {
410414
}
411415
```
412416

413-
Code required to modify your tree builder.
417+
Add the new condition to your behavior tree builder with the following snippet.
414418

415419
```C#
416420
using Adnc.FluidBT.Trees;
417421

418422
public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
419-
...
420-
public TreeBuilderCustom CustomCondition (string name) {
423+
public TreeBuilderCustom (GameObject owner) : base(owner) {
424+
}
425+
426+
public TreeBuilderCustom CustomCondition (string name = "My Condition") {
421427
_tree.AddNode(Pointer, new CustomCondition {
422428
Name = name
423429
});
@@ -429,6 +435,85 @@ public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
429435

430436
### Custom Composites
431437

438+
Fluid Behavior Tree isn't limited to just custom actions and conditions. You can create new composite types with a fairly
439+
simple API. Here is an example of a new selector variation that randomly chooses an execution order.
440+
441+
```C#
442+
using Adnc.FluidBT.TaskParents.Composites;
443+
using Adnc.FluidBT.Tasks;
444+
using Random = System.Random;
445+
446+
public class SelectorRandom : CompositeBase {
447+
private bool _init;
448+
449+
// Triggers each time this node is ticked
450+
protected override TaskStatus OnUpdate () {
451+
if (!_init) {
452+
ShuffleChildren();
453+
_init = true;
454+
}
455+
456+
for (var i = ChildIndex; i < Children.Count; i++) {
457+
var child = Children[ChildIndex];
458+
459+
switch (child.Update()) {
460+
case TaskStatus.Success:
461+
return TaskStatus.Success;
462+
case TaskStatus.Continue:
463+
return TaskStatus.Continue;
464+
}
465+
466+
ChildIndex++;
467+
}
468+
469+
return TaskStatus.Failure;
470+
}
471+
472+
// Reset is triggered when the Behavior Tree ends, then runs again ticking this node
473+
public override void Reset (bool hardReset = false) {
474+
base.Reset(hardReset);
475+
476+
ShuffleChildren();
477+
}
478+
479+
private void ShuffleChildren () {
480+
var rng = new Random();
481+
var n = Children.Count;
482+
while (n > 1) {
483+
n--;
484+
var k = rng.Next(n + 1);
485+
var value = Children[k];
486+
Children[k] = Children[n];
487+
Children[n] = value;
488+
}
489+
}
490+
}
491+
```
492+
493+
Adding custom composites to your behavior tree only takes a line of code. Below is a commented out chunk
494+
of code if you need more control over the parameters of your composite.
495+
496+
```C#
497+
using Adnc.FluidBT.Trees;
498+
499+
public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
500+
public TreeBuilderCustom (GameObject owner) : base(owner) {
501+
}
502+
503+
public TreeBuilderCustom CustomComposite (string name = "My Custom Composite") {
504+
return ParentTask<CustomComposite>(name);
505+
506+
// Or you can code this manually if you need more specifics
507+
//
508+
// var parent = new CustomComposite { Name = name };
509+
// _tree.AddNode(Pointer, parent);
510+
// _pointer.Add(parent);
511+
//
512+
// return this;
513+
}
514+
}
515+
```
516+
432517
### Custom Decorators
433518

434519
## Submitting your own actions, conditions, ect

0 commit comments

Comments
 (0)