pass a variable to parent node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By r00n2468

First post here

There is a guy named chris whose done some tutorials on youtube and I have been enjoying them.

here is my damage script

    extends Node

class_name BoarDamageable
@export var health: float = 40:
		return health
		health  = value

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
func hit(damage :int):
	health -= damage
	if(health <= 0 ):

Here is the parent node:

    extends CharacterBody2D
    @export var speed : float = 30.0
    @export var direction : Vector2 = Vector2.ZERO
    @onready var sprite : AnimatedSprite2D = $AnimatedSprite2D
    var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
    func _ready():
    	direction.x = -1
    	sprite.flip_h = false
    func _physics_process(delta):
    	# Add the gravity.
    	var collision = move_and_collide(velocity * delta)
    	if not is_on_floor():
    		velocity.y += gravity * delta
    	velocity.x = direction.x * speed
    	if is_on_floor() and collision:
    		direction.x *= -1
    		$RayCast2D.position.x *= -1.0
    		if collision.get_collider().name == "Player" and speed < 60:
    			speed += 30
    	if !$RayCast2D.is_colliding():
    		speed = 30
    	if is_on_wall() or (is_on_floor() and !$RayCast2D.is_colliding()):
    		if direction.x > 0:
    			position.x -= 20
    		elif direction.x < 0:
    			position.x += 20
    		direction.x *= -1.0
    		$RayCast2D.position.x *= -1.0
func update_facing_direction():
	if direction.x > 0:
		sprite.flip_h = true
	elif direction.x < 0:
		sprite.flip_h = false
func update_Animation():
	if speed>30:"run")

I need to play a dead animation when health <0 from the animatedsprite2d but parent does not know about the health variable and health variable is in does not know about the animation. How can I get access to the health variable outside of that class? signals look good but there is a setter and getter there. How do I use that?
thanks for your help

:bust_in_silhouette: Reply From: betauer

Try to connect the parent node to the SignalBus “on_health_changed” in the _ready method, then play the animation there.

func _ready():
    direction.x = -1
    sprite.flip_h = false
    SignalBus.connect("on_health_changed", _on_health_changed)

func _on_health_changed(health_value):
    # play your animation

The signal is sending two parameters, the parent node and value-health. You can put there all the parameters you need, just make sure the method or callable connected to the signal has the same amount of parameters. So, following my snippet above, which is receiving the health value, you have to change emit like this:

SignalBus.emit_signal("on_health_changed", value)