Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Made BTRoot cleanups and made it to not check for entry point in processing.
  • Loading branch information
SirPigeonz committed Dec 10, 2023
1 parent 32e57b2 commit 6642533
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
40 changes: 27 additions & 13 deletions addons/behaviour_toolkit/behaviour_tree/bt_root.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ enum ProcessType {
@export var blackboard: Blackboard


var active: bool = false
var active: bool = false:
set(value):
active = value
if value and entry_point != null:
_setup_processing()
print("Activated ", str(name))
else:
set_physics_process(false)
set_process(false)

var current_status: BTBehaviour.BTStatus


Expand All @@ -36,7 +45,7 @@ var current_status: BTBehaviour.BTStatus
@onready var __connect_child_order_changed: int = \
child_order_changed.connect(_on_child_order_changed)

@onready var entry_point: Node = get_child(0)
@onready var entry_point: BTBehaviour = get_entry_point()


func _validate_property(property: Dictionary) -> void:
Expand All @@ -46,22 +55,22 @@ func _validate_property(property: Dictionary) -> void:


func _ready() -> void:
#set_physics_process(false)
#set_process(false)

if Engine.is_editor_hint():
set_physics_process(false)
set_process(false)
return

if blackboard == null:
blackboard = _create_local_blackboard()

if autostart:

if entry_point == null:
return
elif autostart:
active = true

if not process_type:
process_type = ProcessType.PHYSICS

_setup_processing()


## Swap this [BTRoot] nodes current entry point with the provided one.
## If root has no [BTBehaviour] as a child the provided one will be added.
Expand Down Expand Up @@ -92,9 +101,6 @@ func _process(delta: float) -> void:


func _process_code(delta: float) -> void:
if entry_point == null:
return

if not active:
return

Expand All @@ -117,8 +123,16 @@ func _setup_processing() -> void:
set_process(process_type == ProcessType.IDLE)


func get_entry_point() -> BTBehaviour:
var first_child := get_child(0)
if first_child is BTBehaviour:
return first_child
else:
return null


func _on_child_order_changed() -> void:
if Engine.is_editor_hint():
return

entry_point = get_child(0)
entry_point = get_entry_point()
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ func _on_update(_delta: float, _actor: Node, _blackboard: Blackboard) -> void:
if behaviour_tree == null:
return

if behaviour_tree.current_status == on_status:
if fire_event_on_status:
get_parent().fire_event(event)
if behaviour_tree.current_status == on_status and fire_event_on_status:
get_parent().fire_event(event)


## Executes before the state is exited.
Expand Down
17 changes: 17 additions & 0 deletions tmp/test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends Node2D


@onready var bt_root: BTRoot = $BTRoot as BTRoot


func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_accept"):
if bt_root.active == true:
bt_root.active = false
else:
bt_root.active = true

if Input.is_action_just_pressed("ui_left"):
var lp = LeafPrint.new()
lp.custom_text = "I'm a leaf."
bt_root.add_child(lp)
10 changes: 10 additions & 0 deletions tmp/test.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=3 uid="uid://bb5leiis4ldhe"]

[ext_resource type="Script" path="res://tmp/test.gd" id="1_7baav"]
[ext_resource type="Script" path="res://addons/behaviour_toolkit/behaviour_tree/bt_root.gd" id="2_efrcf"]

[node name="Test" type="Node2D"]
script = ExtResource("1_7baav")

[node name="BTRoot" type="Node" parent="."]
script = ExtResource("2_efrcf")

0 comments on commit 6642533

Please sign in to comment.