Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corrupted state machine resource #8

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addons/imjp94.yafsm/scenes/StateMachineEditor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ func _on_state_machine_changed(new_state_machine):
select_layer(root_layer)
if new_state_machine:
current_layer.state_machine = state_machine
var validated = StateMachine.validate(new_state_machine)
if validated:
print("gd-YAFSM: Corrupted StateMachine Resource fixed, save to apply the fix.")
draw_graph()
check_has_entry()

Expand Down
8 changes: 4 additions & 4 deletions addons/imjp94.yafsm/scenes/flowchart/FlowChart.gd
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,26 @@ func _gui_input(event):
if _current_connection:
# Connection end
var from = _current_connection.from_node.name
if hit_node is FlowChartNode and _request_connect_to(hit_node.name if hit_node else null):
var to = hit_node.name if hit_node else null
if hit_node is FlowChartNode and _request_connect_to(to) and from != to:
# Connection success
var line
var to = hit_node.name
if _current_connection.to_node:
# Reconnection
line = disconnect_node(from, _current_connection.to_node.name)
_current_connection.to_node = hit_node
_on_node_reconnect_end(from, to)
connect_node(from, to, line)
else:
# New Connection
current_layer.content_lines.remove_child(_current_connection.line)
line = _current_connection.line
_current_connection.to_node = hit_node
connect_node(from, to, line)
connect_node(from, to, line)
else:
# Connection failed
if _current_connection.to_node:
# Reconnection
var to = _current_connection.to_node.name
_current_connection.join()
_on_node_reconnect_failed(from, name)
else:
Expand Down
39 changes: 39 additions & 0 deletions addons/imjp94.yafsm/src/states/StateMachine.gd
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,42 @@ static func join_path(base, dirs):
else:
path = str(path, "/", dir)
return path

# Validate state machine resource to identify and fix error
static func validate(state_machine):
var validated = false
for from_key in state_machine.transitions.keys():
# Non-existing state found in StateMachine.transitions
# See https://github.com/imjp94/gd-YAFSM/issues/6
if not (from_key in state_machine.states):
validated = true
push_warning("gd-YAFSM ValidationError: Non-existing state(%s) found in transition" % from_key)
state_machine.transitions.erase(from_key)
continue

var from_transition = state_machine.transitions[from_key]
for to_key in from_transition.keys():
# Non-existing state found in StateMachine.transitions
# See https://github.com/imjp94/gd-YAFSM/issues/6
if not (to_key in state_machine.states):
validated = true
push_warning("gd-YAFSM ValidationError: Non-existing state(%s) found in transition(%s -> %s)" % [to_key, from_key, to_key])
from_transition.erase(to_key)
continue

# Mismatch of StateMachine.transitions with Transition.to
# See https://github.com/imjp94/gd-YAFSM/issues/6
var to_transition = from_transition[to_key]
if to_key != to_transition.to:
validated = true
push_warning("gd-YAFSM ValidationError: Mismatch of StateMachine.transitions key(%s) with Transition.to(%s)" % [to_key, to_transition.to])
to_transition.to = to_key

# Self connecting transition
# See https://github.com/imjp94/gd-YAFSM/issues/5
if to_transition.from == to_transition.to:
validated = true
push_warning("gd-YAFSM ValidationError: Self connecting transition(%s -> %s)" % [to_transition.from, to_transition.to])
from_transition.erase(to_key)
return validated