0 votes

I'm working my way through the "Godot 3 Complete Developer Course - 2D and 3D" course on Udemy. At the end of the Space Attack section is the assignment to add a health bar for the player. Here is a link to download my full project.

The game crashes when the player ship gets hit and I've included an argument with the emit_signal in Player.gd, Line 63:

func _on_Player_health_changed(new_health):
print(new_health) #<-- WORKS!!!
emit_signal("health_changed", new_health)

The debugger output:
** Debug Process Started **
OpenGL ES 3.0 Renderer: GeForce GTX 1060 6GB/PCIe/SSE2
drivers/unix/netsocketposix.cpp:190 - Socket error: 10054
** Debug Process Stopped **

If I remove the "new_health" argument, there's no crash. (Of course, the LifeBar also doesn't get updated.)

in Engine by (15 points)

1 Answer

+1 vote
Best answer

I finally found what the problem is!
I downloaded your project and messed around a bit. I noticed that your print(new_health) statement in the func _on_Player_health_changed(new_health) function printed out the same number several times before crashing and I figured that there's got to be a loop in there somewhere. Then I figured out that you have connected the wrong node to the health_changed signal! You had connected the signal to the Player node, when you should have connected it to the LifeBar node. Go to your Player node, disconnect the signal and reconnect it to LifeBar.
The func _on_Player_health_changed(new_health) will appear in the LifeBar node's script, where you can place the self.value = new_health logic.

By the way, I noticed that your Player scene has the Player.gd script attached to it, and the Player node in the main scene also has the same script attached to it! Why is that? You do not need to have the same script twice. Also, remember to disconnect the signal both from the Player scene and the Player node in the Main scene.

Please upvote mark this answer as best, so that others can find it useful too!

by (1,859 points)
selected by

Thank you for your response, this and the response I got here got me going in the right direction and I was able to complete the task!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.