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 game crashing when player dies. Instead, game restarts. #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions actors/player/PlayerController.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func fall(gap_size):

func _on_Die_finished(string):
set_dead(true)
get_tree().change_scene("res://Demo.tscn")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be in the player script. Rather in a file like Game.gd or something similar.



func get_health_node():
return $Health
Expand Down
3 changes: 2 additions & 1 deletion actors/player/PlayerStateMachine.gd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func _unhandled_input(event):
_change_state('attack')
get_tree().set_input_as_handled()
return
current_state.handle_input(event)
if current_state:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be part of the same commit as it's unrelated. Also, I have to check the code but I feel it's not the right fix to keep the code maintainable. There shouldn't be cases where the input gets forwarded to a nonexistent state.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because the player performed queue_free so the old reference does not exist anymore.

Additionally _unhandled_input is an internal engine method so it can't be avoided without other changes. I would suggest not doing queue_free on the player and instead just setting to a Dead state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to get back into the code, the state machine should belong to the player and be freed with it. And yeah it probably shouldn't call queue_free upon dying then.

current_state.handle_input(event)

func _on_Health_damage_taken(new_health):
_change_state('stagger')
Expand Down
3 changes: 2 additions & 1 deletion utils/state/StateMachine.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func set_active(value):
current_state = null

func _unhandled_input(event):
current_state.handle_input(event)
if current_state:
current_state.handle_input(event)

func _physics_process(delta):
current_state.update(delta)
Expand Down