From 48443c5f87caa1532b552d055da6b9a2fb5c5505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Thom=C3=A4?= Date: Fri, 16 Aug 2024 19:10:49 +0200 Subject: [PATCH] fix: compiler warning about overriding `add_child` fixes #128 --- CHANGES.md | 1 + addons/godot_state_charts/compound_state.gd | 33 +++++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e40c7ec..e142c10 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - The library now handles cases better where code tries to access a state chart that has been removed from the tree. This may happen when using Godot's `change_scene_to_file` or `change_scene_to_packed` functions. Debug output in these cases will no longer try to get full path names of nodes that have been removed from the tree. This should prevent errors and crashes in these cases ([#129](https://github.com/derkork/godot-statecharts/issues/129)). - The error messages for evaluating expressions have been improved. They now show the expression that was evaluated and the result of the evaluation ([#138](https://github.com/derkork/godot-statecharts/issues/138)) +- Compound state should no longer show a warning for overriding `add_child`. A big thanks goes out to [yesfish](https://github.com/huwpascoe) for finding this and providing a fix ([#128](https://github.com/derkork/godot-statecharts/issues/128)). ## [0.16.0] - 2024-06-06 ### Added diff --git a/addons/godot_state_charts/compound_state.gd b/addons/godot_state_charts/compound_state.gd index 31a9b7b..ba13153 100644 --- a/addons/godot_state_charts/compound_state.gd +++ b/addons/godot_state_charts/compound_state.gd @@ -29,8 +29,26 @@ var _active_state:StateChartState = null ## The history states of this compound state. var _history_states:Array[HistoryState] = [] ## Whether any of the history states needs a deep history. -var _needs_deep_history = false +var _needs_deep_history:bool = false + +func _init() -> void: + # subscribe to the child_entered_tree signal in edit mode so we can + # automatically set the initial state when a new sub-state is added + if Engine.is_editor_hint(): + child_entered_tree.connect( + func(child:Node): + # when a child is added in the editor and the child is a state + # and we don't have an initial state yet, set the initial state + # to the newly added child + if child is StateChartState and initial_state.is_empty(): + # the newly added node may have a random name now, + # so we need to defer the call to build a node path + # to the next frame, so the editor has time to rename + # the node to its final name + (func(): initial_state = get_path_to(child)).call_deferred() + ) + func _state_init(): super._state_init() @@ -218,18 +236,7 @@ func _restore_history_state(target:HistoryState): push_error("The default state '" + str(target.default_state) + "' of the history state '" + target.name + "' cannot be found.") return -func add_child(node:Node, force_readable_name:bool = false, internal:InternalMode = INTERNAL_MODE_DISABLED) -> void: - super.add_child(node, force_readable_name, internal) - # when a child is added in the editor and the child is a state - # and we don't have an initial state yet, set the initial state - # to the newly added child - if Engine.is_editor_hint() and node is StateChartState: - if initial_state.is_empty(): - # the newly added node may have a random name now, - # so we need to defer the call to build a node path - # to the next frame, so the editor has time to rename - # the node to its final name - (func(): initial_state = get_path_to(node)).call_deferred() + func _get_configuration_warnings() -> PackedStringArray: