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.
http://www.markalmighty.com/f/ez/downloader.php?d=godot&f=SpaceAttack_by_MarkAlmighty.zip

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
465.249115
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.)

asked Aug 5 in Projects by MarkAlmighty (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!

answered Aug 6 by johnygames (614 points)
selected Aug 7 by MarkAlmighty

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.