Skip to content

Commit

Permalink
Issue #328 - Make the RandomizedComposite node know when to remove we…
Browse files Browse the repository at this point in the history
…ights when children exit (#329)

* Added test for issue #328

* Update randomized_composite.gd

Updated the node to keep track of whether the parent and children are all exiting and only remove the child weight if the parent is staying in the tree.

* Apply suggestions from code review

Co-authored-by: miguel <[email protected]>

---------

Co-authored-by: miguel <[email protected]>
  • Loading branch information
ksmithdev and bitbrain authored Apr 16, 2024
1 parent 645422f commit f5e9215
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
25 changes: 22 additions & 3 deletions addons/beehave/nodes/composites/randomized_composite.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const WEIGHTS_PREFIX = "Weights/"
notify_property_list_changed()

var _weights: Dictionary
var _exiting_tree: bool


func _ready():
Expand Down Expand Up @@ -111,22 +112,40 @@ func _update_weights(children: Array[Node]) -> void:
notify_property_list_changed()


func _exit_tree() -> void:
_exiting_tree = true


func _enter_tree() -> void:
_exiting_tree = false


func _on_child_entered_tree(node: Node):
_update_weights(get_children())

var renamed_callable = _on_child_renamed.bind(node.name, node)
if not node.renamed.is_connected(renamed_callable):
node.renamed.connect(renamed_callable)

if not node.tree_exited.is_connected(_on_child_tree_exited):
node.tree_exited.connect(_on_child_tree_exited.bind(node))


func _on_child_exiting_tree(node: Node):
var renamed_callable = _on_child_renamed.bind(node.name, node)
if node.renamed.is_connected(renamed_callable):
node.renamed.disconnect(renamed_callable)


func _on_child_tree_exited(node: Node) -> void:
# don't erase the individual child if the whole tree is exiting together
if not _exiting_tree:
var children = get_children()
children.erase(node)
_update_weights(children)

var children = get_children()
children.erase(node)
_update_weights(children)
if node.tree_exited.is_connected(_on_child_tree_exited):
node.tree_exited.disconnect(_on_child_tree_exited)


func _on_child_renamed(old_name: String, renamed_child: Node):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
extends Node2D

@onready var sequence_random: SequenceRandomComposite = %SequenceRandom

func set_weights(idle: int, run: int, attack_meele: int, attack_ranged: int):
sequence_random.set("Weights/Idle", idle)
sequence_random.set("Weights/Run", run)
sequence_random.set("Weights/Attack Meele", attack_meele)
sequence_random.set("Weights/Attack Ranged", attack_ranged)
30 changes: 30 additions & 0 deletions test/randomized_composites/runtime_changes/runtime_changes_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,33 @@ func test_rename_child() -> void:
.contains_key_value(node, weights_before[node])

runner.simulate_frames(10) # Everything should work fine afterwards.

func test_scene_reload() -> void:
var scene = create_scene()
var runner := scene_runner(scene)

runner.set_time_factor(100.0)

# set the Idle weight to non-default
scene.set_weights(2, 3, 4, 5)

var sequence_random: SequenceRandomComposite = scene.sequence_random
var weights_before: Dictionary = sequence_random._weights.duplicate()

await runner.simulate_frames(10)

var beehave_tree = runner.find_child("BeehaveTree")
beehave_tree.remove_child(sequence_random)

await runner.simulate_frames(10)

beehave_tree.add_child(sequence_random)

# Weights should be exactly the same.
var children = weights_before.keys()
for node in children:
assert_dict(sequence_random._weights)\
.contains_key_value(node, weights_before[node])

await runner.simulate_frames(10) # Everything should work fine afterwards.

0 comments on commit f5e9215

Please sign in to comment.