Skip to content

Commit

Permalink
fix: properly handle the case where the initial state is a history state
Browse files Browse the repository at this point in the history
  • Loading branch information
derkork committed Apr 17, 2024
1 parent bf673ce commit 44e7035
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions addons/godot_state_charts/compound_state.gd
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ func _state_enter(expect_transition:bool = false):
# - we are no longer active becasue entering the state triggered an immediate transition to some other state
if not expect_transition and not is_instance_valid(_active_state) and _state_active:
if _initial_state != null:
_active_state = _initial_state
_active_state._state_enter()
if _initial_state is HistoryState:
_restore_history_state(_initial_state)
else:
_active_state = _initial_state
_active_state._state_enter()
else:
push_error("No initial state set for state '" + name + "'.")

Expand Down Expand Up @@ -164,22 +167,8 @@ func _handle_transition(transition:Transition, source:StateChartState):
# now check if the target is a history state, if this is the
# case, we need to restore the saved state
if target is HistoryState:
# print("Target is history state, restoring saved state.")
var saved_state = target.history
if saved_state != null:
# restore the saved state
_state_restore(saved_state, -1 if target.deep else 1)
return
# print("No history saved so far, activating default state.")
# if we don't have history, we just activate the default state
var default_state = target.get_node_or_null(target.default_state)
if is_instance_valid(default_state):
_active_state = default_state
_active_state._state_enter()
return
else:
push_error("The default state '" + target.default_state + "' of the history state '" + target.name + "' cannot be found.")
return
_restore_history_state(target)
return

# else, just activate the target state
_active_state = target
Expand Down Expand Up @@ -211,6 +200,24 @@ func _handle_transition(transition:Transition, source:StateChartState):
get_parent()._handle_transition(transition, source)


func _restore_history_state(target:HistoryState):
# print("Target is history state, restoring saved state.")
var saved_state = target.history
if saved_state != null:
# restore the saved state
_state_restore(saved_state, -1 if target.deep else 1)
return
# print("No history saved so far, activating default state.")
# if we don't have history, we just activate the default state
var default_state = target.get_node_or_null(target.default_state)
if is_instance_valid(default_state):
_active_state = default_state
_active_state._state_enter()
return
else:
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
Expand Down

0 comments on commit 44e7035

Please sign in to comment.