Skip to content

Commit

Permalink
fix: behaviour when transitioning from a child to a parent state
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
Jan Thomä committed Apr 12, 2023
1 parent 56e2a3c commit 165b1cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.1] - 2023-04-12
### Fixed
- Transitioning from a child to a parent state should no longer throw an error when the parent is the root node ([#3](https://github.com/derkork/godot-statecharts/issues/3)).
- Transitioning from a child to a parent compound / parallel state now properly counts as [self transition](https://statecharts.dev/glossary/self-transition.html), so that the parent node is exited and re-entered.


## [0.1.0] - 2023-04-06
### Breaking changes
- The state chart debugger now is no longer a single node but a full scene. This allows to have more complex UI in the debugger. Please replace the old debugger node with the new scene which is located at `addons/godot-statecharts/utilities/state_chart_debugger.tscn`. The debugger will no longer appear in the node list. You can quickly add it using the "Instatiate child scene" button in the scene inspector.
Expand Down
8 changes: 8 additions & 0 deletions addons/godot_state_charts/compound_state.gd
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func _handle_transition(transition:Transition, source:State):
return

# the target state can be
# 0. this state. in this case exit this state and re-enter it. This can happen when
# a child state transfers to its parent state.
# 1. a direct child of this state. this is the easy case in which
# we will deactivate the current _active_state and activate the target
# 2. a descendant of this state. in this case we find the direct child which
Expand All @@ -133,6 +135,12 @@ func _handle_transition(transition:Transition, source:State):
# 3. no descendant of this state. in this case, we ask our parent state to
# perform the transition

if target == self:
# exit this state and re-enter it
_state_exit()
_state_enter(false)
return

if target in get_children():
# all good, now first deactivate the current state
if is_instance_valid(_active_state):
Expand Down
9 changes: 9 additions & 0 deletions addons/godot_state_charts/parallel_state.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func _handle_transition(transition:Transition, source:State):
return

# the target state can be
# 0. this state. in this case just activate the state and all its children.
# this can happen when a child state transfers back to its parent state.
# 1. a direct child of this state. this is the easy case in which
# we will do nothing, because our direct children are always active.
# 2. a descendant of this state. in this case we find the direct child which
Expand All @@ -36,6 +38,13 @@ func _handle_transition(transition:Transition, source:State):
# 3. no descendant of this state. in this case, we ask our parent state to
# perform the transition

if target == self:
# exit this state
_state_exit()
# then re-enter it
_state_enter(false)
return

if target in get_children():
# all good, nothing to do.
return
Expand Down

0 comments on commit 165b1cc

Please sign in to comment.