@@ -335,7 +335,8 @@ will be more of the same. But each covers a different node type.
335
335
336
336
### Custom Actions
337
337
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.
339
340
340
341
``` C#
341
342
using UnityEngine ;
@@ -363,14 +364,16 @@ public class CustomAction : ActionBase {
363
364
}
364
365
```
365
366
366
- Code required to modify your tree builder.
367
+ Add your new node to a custom tree builder.
367
368
368
369
``` C#
369
370
using Adnc .FluidBT .Trees ;
370
371
371
372
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" ) {
374
377
_tree .AddNode (Pointer , new CustomAction {
375
378
Name = name
376
379
});
@@ -382,7 +385,8 @@ public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
382
385
383
386
### Custom Conditions
384
387
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.
386
390
387
391
``` C#
388
392
using UnityEngine ;
@@ -410,14 +414,16 @@ public class CustomCondition : ConditionBase {
410
414
}
411
415
```
412
416
413
- Code required to modify your tree builder.
417
+ Add the new condition to your behavior tree builder with the following snippet .
414
418
415
419
``` C#
416
420
using Adnc .FluidBT .Trees ;
417
421
418
422
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" ) {
421
427
_tree .AddNode (Pointer , new CustomCondition {
422
428
Name = name
423
429
});
@@ -429,6 +435,85 @@ public class TreeBuilderCustom : BehaviorTreeBuilderBase<TreeBuilderCustom> {
429
435
430
436
### Custom Composites
431
437
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
+
432
517
### Custom Decorators
433
518
434
519
## Submitting your own actions, conditions, ect
0 commit comments