Connecting Nodes that aren't instantiated with Signals

Godot Version

4.3.3

Question

This whole day I have been trying to figure out the use of the signals and how they can be properly utilized in my project, just before I wanted to post here to ask for some advice I managed to find a solution which I was amazed to do.
But now that I have the solution and the code works, can I get some of your feedback on the solution that I came up with?

Now this whole thing came up for the solution to calculate the value of a killed monster and to add it to your total gold and update the HUD. The issue for me for the most part was that the Monsters were instantiated and the whole Level which holds The Spawner, Tilesets, Towers… gets instantiated when you choose a level. My enemy is a pathfollow2d which has a character2d as a child node and that has the whole script for the given enemy.
The enemy also has a Hurtboxcomponent which exports a HealthComponent and keeps track of the health for that given enemy.

My solution to this was to send a signal that the enemy died from the HealthComponent to the Enemy, then from there to the spawner and then to the GameScene. So in total I have 3 signals in total for this enemy to function correctly.

This is the function I came up with after looking a lot of forum posts and with this it finally works. I even added a new enemy and copied the code from the first one and I made some changes to the value of that enemy and it works the signal is sending the different value. I will now just reuse that code for every enemy I create and copy the base code to them. Would an EnemyClass be a good thing to add to just streamline the process?

This was the thing that also gave me a lot of trouble since the whole level is instantiated and I can’t easily connect the signals from one place to another. Any tips on how can I manage that going forwards with this node setup?

Screenshot_5
Screenshot_4

Loos good to me! Signals are the idiomatic way of doing things like this in Godot; an EnemyClass that holds all those signals might be a good idea if you want to have other duplicated logic between enemy types.

1 Like

Thank you, I was thinking of maybe doing that because I was using a resource EnemyStats which had the basic stats for the enemies.