Skip to content

Commit

Permalink
fix: compiler warning about overriding add_child
Browse files Browse the repository at this point in the history
fixes #128
  • Loading branch information
derkork committed Aug 16, 2024
1 parent f6b5417 commit 48443c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 20 additions & 13 deletions addons/godot_state_charts/compound_state.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 48443c5

Please sign in to comment.