-
Notifications
You must be signed in to change notification settings - Fork 195
/
NPBehaveExampleHelloBlackboardsAI.cs
49 lines (39 loc) · 1.9 KB
/
NPBehaveExampleHelloBlackboardsAI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using UnityEngine;
using NPBehave;
public class NPBehaveExampleHelloBlackboardsAI : MonoBehaviour
{
private Root behaviorTree;
void Start()
{
behaviorTree = new Root(
// toggle the 'toggled' blackboard boolean flag around every 500 milliseconds
new Service(0.5f, () => { behaviorTree.Blackboard["foo"] = !behaviorTree.Blackboard.Get<bool>("foo"); },
new Selector(
// Check the 'toggled' flag. Stops.IMMEDIATE_RESTART means that the Blackboard will be observed for changes
// while this or any lower priority branches are executed. If the value changes, the corresponding branch will be
// stopped and it will be immediately jump to the branch that now matches the condition.
new BlackboardCondition("foo", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART,
// when 'toggled' is true, this branch will get executed.
new Sequence(
// print out a message ...
new Action(() => Debug.Log("foo")),
// ... and stay here until the `BlackboardValue`-node stops us because the toggled flag went false.
new WaitUntilStopped()
)
),
// when 'toggled' is false, we'll eventually land here
new Sequence(
new Action(() => Debug.Log("bar")),
new WaitUntilStopped()
)
)
)
);
behaviorTree.Start();
// attach the debugger component if executed in editor (helps to debug in the inspector)
#if UNITY_EDITOR
Debugger debugger = (Debugger)this.gameObject.AddComponent(typeof(Debugger));
debugger.BehaviorTree = behaviorTree;
#endif
}
}