Skip to content

Commit 022f0c0

Browse files
authored
Merge pull request #57 from ThePat02/2.0.0
Upgrade main branch to 2.0.0
2 parents 63da18b + b2e66f0 commit 022f0c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1825
-890
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
@icon("res://addons/behaviour_toolkit/icons/Gear.svg")
22
class_name BehaviourToolkit extends Node
3-
## The main node of the BehaviourToolkit plugin.
3+
## Base class for Behaviour Toolkit plugin nodes.
4+
##
5+
## Currently only used to group this plugins nodes.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
@icon("res://addons/behaviour_toolkit/icons/BTBehaviour.svg")
22
class_name BTBehaviour extends BehaviourToolkit
3+
## Base class for building behaviour nodes in BehaviourToolkit.
4+
##
5+
## Behaviours can return [enum BTBehaviour.BTStatus.SUCCESS],
6+
## [enum BTBehaviour.BTStatus.FAILURE] or [enum BTBehaviour.BTStatus.RUNNING]
7+
## which control the flow of the behaviours in Behaviour Tree system.
38

49

5-
enum Status {
10+
enum BTStatus {
611
SUCCESS,
712
FAILURE,
8-
RUNNING
13+
RUNNING,
914
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTComposite.svg")
23
class_name BTComposite extends BTBehaviour
4+
## Base class to build composite behaviour nodes.
5+
##
6+
## Composites can hold multiple behaviour nodes and evalute/execute them
7+
## based on custom logic based on their return values.
38

49

510
## The leaves under the composite node.
611
@onready var leaves: Array = get_children()
12+
13+
14+
func _get_configuration_warnings() -> PackedStringArray:
15+
var warnings: Array = []
16+
17+
var parent = get_parent()
18+
var children = get_children()
19+
20+
if not parent is BTComposite and not parent is BTRoot and not parent is BTDecorator:
21+
warnings.append("BTComposite node must be a child of BTComposite or BTRoot node.")
22+
23+
if children.size() == 0:
24+
warnings.append("BTComposite node must have at least one child.")
25+
26+
if children.size() == 1:
27+
warnings.append("BTComposite node should have more than one child.")
28+
29+
return warnings
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTDecorator.svg")
23
class_name BTDecorator extends BTBehaviour
4+
## Base class for decorators.
5+
##
6+
## Decorators are used to augment the behaviour of a leaf.[br]
7+
## Think of it as another layer of logic that is executed before the leaf.
38

49

510
## The leaf the decorator is decorating.
611
@onready var leaf: BTBehaviour = _get_leaf()
712

813

914
func _get_leaf() -> BTBehaviour:
10-
if get_child_count() == 0:
11-
return null
12-
13-
return get_child(0)
15+
if get_child_count() == 0:
16+
return null
17+
18+
return get_child(0)
19+
20+
21+
func _get_configuration_warnings() -> PackedStringArray:
22+
var warnings: Array = []
23+
24+
var parent = get_parent()
25+
var children = get_children()
26+
27+
if not parent is BTComposite and not parent is BTRoot:
28+
warnings.append("Decorator node should be a child of a composite node or the root node.")
29+
30+
if children.size() == 0:
31+
warnings.append("Decorator node should have a child.")
32+
elif children.size() > 1:
33+
warnings.append("Decorator node should have only one child.")
34+
elif not children[0] is BTBehaviour:
35+
warnings.append("Decorator node should have a BTBehaviour node as a child.")
36+
37+
return warnings
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTLeaf.svg")
23
class_name BTLeaf extends BTBehaviour
4+
## A leaf is where the actual logic that controlls the actor or other nodes
5+
## is implemented.
6+
##
7+
## It is the base class for all leaves and can be extended to implement
8+
## custom behaviours.[br]
9+
## The [code]tick(actor: Node, blackboard: Blackboard)[/code] method is called
10+
## every frame and should return the status.
311

412

5-
func tick(_actor: Node, _blackboard: Blackboard) -> Status:
6-
return Status.SUCCESS
13+
func tick(_delta: float, _actor: Node, _blackboard: Blackboard) -> BTStatus:
14+
return BTStatus.SUCCESS
15+
16+
17+
func _get_configuration_warnings() -> PackedStringArray:
18+
var warnings: Array = []
19+
20+
var parent = get_parent()
21+
var children = get_children()
22+
23+
if not parent is BTBehaviour and not parent is BTRoot:
24+
warnings.append("BTLeaf node must be a child of BTBehaviour or BTRoot node.")
25+
26+
if children.size() > 0:
27+
warnings.append("BTLeaf node must not have any children.")
28+
29+
return warnings
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTLeafIntegration.svg")
23
class_name BTLeafIntegration extends BTLeaf
4+
## Base class to build [BTLeaf]s that act on [FiniteStateMachine].
35

46

5-
@export var state_machine: FiniteStateMachine
7+
@export var state_machine: FiniteStateMachine:
8+
set(value):
9+
state_machine = value
10+
update_configuration_warnings()
11+
12+
13+
func _get_configuration_warnings():
14+
var warnings: Array = []
15+
16+
warnings.append_array(super._get_configuration_warnings())
17+
18+
if state_machine == null:
19+
warnings.append("No state machine set.")
20+
21+
return warnings

addons/behaviour_toolkit/behaviour_tree/bt_root.gd

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTRoot.svg")
23
class_name BTRoot extends BehaviourToolkit
34
## Node used as a base parent (root) of a Behaviour Tree
5+
##
6+
## This is the root of your behaviour tree.[br]
7+
## It is designed to expect first child node to be a BTComposite node to start
8+
## the execution of the behaviour tree.[br]
9+
## The root node is responsible for updating the tree.
410

511

612
enum ProcessType {
713
IDLE, ## Updates on every rendered frame (at current FPS).
8-
PHYSICS ## Updates on a fixed rate (60 FPS by default) synchornized with physics thread.
14+
PHYSICS, ## Updates on a fixed rate (60 FPS by default) synchornized with physics thread.
915
}
1016

1117

@@ -25,13 +31,19 @@ enum ProcessType {
2531

2632

2733
var active: bool = false
28-
var current_status: BTBehaviour.Status
34+
var current_status: BTBehaviour.BTStatus
2935

3036

3137
@onready var entry_point = get_child(0)
3238

3339

3440
func _ready() -> void:
41+
# Don't run in editor
42+
if Engine.is_editor_hint():
43+
set_physics_process(false)
44+
set_process(false)
45+
return
46+
3547
if blackboard == null:
3648
blackboard = _create_local_blackboard()
3749

@@ -56,8 +68,7 @@ func _process_code(delta: float) -> void:
5668
if not active:
5769
return
5870

59-
blackboard.set_value("delta", delta)
60-
current_status = entry_point.tick(actor, blackboard)
71+
current_status = entry_point.tick(delta, actor, blackboard)
6172

6273

6374
func _create_local_blackboard() -> Blackboard:
@@ -69,3 +80,19 @@ func _create_local_blackboard() -> Blackboard:
6980
func _setup_processing() -> void:
7081
set_physics_process(process_type == ProcessType.PHYSICS)
7182
set_process(process_type == ProcessType.IDLE)
83+
84+
85+
func _get_configuration_warnings() -> PackedStringArray:
86+
var warnings: Array = []
87+
88+
var children = get_children()
89+
90+
if children.size() == 0:
91+
warnings.append("Behaviour Tree needs to have one Behaviour child.")
92+
elif children.size() == 1:
93+
if not children[0] is BTBehaviour:
94+
warnings.append("The child of Behaviour Tree needs to be a Behaviour.")
95+
elif children.size() > 1:
96+
warnings.append("Behaviour Tree can have only one Behaviour child.")
97+
98+
return warnings
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTCompositeIntegration.svg")
23
class_name BTIntegratedFSM extends BTComposite
4+
## A composite behaviour node that allows to integrate [FiniteStateMachine]
5+
## in a behaviour tree by handling FSM node.
6+
##
7+
## To nest a FSM you need to add a [FiniteStateMachine] as a first child of
8+
## [BTIntegratedFSM]. When this behaviour is evaluated the child FSM
9+
## is set to active.
10+
## [br][br]
11+
## After the FSM is started it returns status
12+
## [enum BTBehaviour.BTStatus.RUNNING]. If FSM return
13+
## [enum BTBehaviour.BTStatus.SUCCESS] or [enum BTBehaviour.BTStatus.FAILURE]
14+
## the child FSM is stopped.
315

416

5-
@onready var state_machine: FiniteStateMachine = _get_machine()
17+
var state_machine: FiniteStateMachine = null
618

719

8-
func tick(_actor: Node, _blackboard: Blackboard) -> Status:
20+
func _ready():
21+
if not Engine.is_editor_hint():
22+
state_machine = _get_machine()
23+
24+
25+
26+
func tick(_delta: float, _actor: Node, _blackboard: Blackboard) -> BTStatus:
927
if state_machine.active == false:
1028
state_machine.start()
1129

12-
if not state_machine.current_bt_status == Status.RUNNING:
30+
if not state_machine.current_bt_status == BTStatus.RUNNING:
1331
state_machine.active = false
1432

1533
return state_machine.current_bt_status
@@ -20,3 +38,20 @@ func _get_machine() -> FiniteStateMachine:
2038
return null
2139
else:
2240
return get_child(0)
41+
42+
43+
func _get_configuration_warnings():
44+
var warnings: Array = []
45+
var children = get_children()
46+
47+
if children.size() == 0:
48+
warnings.append("BTIntegratedFSM must have a child node. The first child will be used as the state machine.")
49+
50+
if children.size() > 1:
51+
warnings.append("BTIntegratedFSM can only have one child node. The first child will be used as the state machine.")
52+
53+
if children.size() == 1:
54+
if not children[0] is FiniteStateMachine:
55+
warnings.append("BTIntegratedFSM's child node must be a FiniteStateMachine. The first child will be used as the state machine.")
56+
57+
return warnings

addons/behaviour_toolkit/behaviour_tree/composites/bt_random.gd

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ var active_leave: BTBehaviour
1212

1313

1414
func _ready():
15-
if use_seed:
16-
rng.seed = hash(seed)
17-
18-
19-
func tick(actor: Node, blackboard: Blackboard):
20-
if active_leave == null:
21-
active_leave = leaves[rng.randi() % leaves.size()]
22-
23-
var response = active_leave.tick(actor, blackboard)
24-
25-
if response == Status.RUNNING:
26-
return response
27-
28-
active_leave = null
29-
return response
15+
if use_seed:
16+
rng.seed = hash(seed)
17+
18+
19+
func tick(delta: float, actor: Node, blackboard: Blackboard):
20+
if active_leave == null:
21+
active_leave = leaves[rng.randi() % leaves.size()]
22+
23+
var response = active_leave.tick(delta, actor, blackboard)
24+
25+
if response == BTStatus.RUNNING:
26+
return response
27+
28+
active_leave = null
29+
return response
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@tool
12
@icon("res://addons/behaviour_toolkit/icons/BTCompositeRandomSelector.svg")
23
class_name BTRandomSelector extends BTComposite
34
## The selector composite but with a random order of the leaves.
@@ -7,24 +8,24 @@ var is_shuffled: bool = false
78
var current_leaf: int = 0
89

910

10-
func tick(actor: Node, blackboard: Blackboard):
11+
func tick(delta: float, actor: Node, blackboard: Blackboard):
1112
if not is_shuffled:
1213
leaves.shuffle()
1314

1415
if current_leaf > leaves.size() -1:
1516
current_leaf = 0
1617
is_shuffled = false
17-
return Status.FAILURE
18+
return BTStatus.FAILURE
1819

19-
var response = leaves[current_leaf].tick(actor, blackboard)
20+
var response = leaves[current_leaf].tick(delta, actor, blackboard)
2021

21-
if response == Status.SUCCESS:
22+
if response == BTStatus.SUCCESS:
2223
current_leaf = 0
2324
is_shuffled = false
2425
return response
2526

26-
if response == Status.RUNNING:
27+
if response == BTStatus.RUNNING:
2728
return response
2829

2930
current_leaf += 1
30-
return Status.RUNNING
31+
return BTStatus.RUNNING

0 commit comments

Comments
 (0)