Ok, we basically have 5 scenes:
1. The enemy slime scene
2. The player scene
3. The hearts scene
4. The level scene that contains the enemy, the player, and the hearts
5. The autoloaded event bus scene
Let's go over what happens when the slime attacks:
When your slime attacks, you'll use your raycast nodes to determine if they hit the player and if they did, you'll get the Player's collider:
var player_collider = null
if $AttackRaycast1.is_colliding():
player_collider = $AttackRaycast1.get_collider()
elif $AttackRaycast2.is_colliding():
player_collider = $AttackRaycast2.get_collider()
After that, you'll want the slime to communicate directly with the player to let it know it's been damaged. However, you'll want to do this in a way that assumes very little about the node structure in the player scene. So we'll use propagate_call so that any node inside the player scene that has a take_damage method will have that method called:
if player_collider != null:
player_collider.propagate_call("take_damage", [damage_amount])
OK, so inside your player scene you'll make a node and call it something like player_health. That node will have a take_damage method that looks something like this:
func take_damage(amount):
health -= amount
$event_bus.emit_signal("on_player_damaged", health)
That event bus is an autoloaded scene that has a single node. The event bus will have a single node that has this in it:
signal on_player_damaged(new_health)
Your event bus acts a a global messaging system. Nodes can emit these "global" events by emitting them from the event bus. Nodes that need to receive these events connect to the signal on the event bus. This allows your player to notify your hearts that the player has been damaged without the hearts needing to know anything about the player. So your hearts will have some code that looks something like this:
_ready():
$event_bus.connect("on_player_damaged", self, "_on_player_damaged")
func _on_player_damaged(health):
play the heart animation
As your game develops, you'll add more signals to the event bus as you need them.