Having trouble scripting health bar(which uses hearts and not the progress bar)

Godot Version

godot 4

im new to godot and im trying to make a game where there are falling objects from the sky and the player has to catch them with a trash can ive done most of the code but im having trouble with the health management. im using signals and every time the signal is emited the hearts bar is updated but im having troubles connecting the signal with the update system and i get this error “Invalid get index ‘health_changed’ (on base: ‘PackedScene’).”

heres my code for my main scene`

extends Node
@onready var banana = $"."
@onready var paper = $"."
@onready var bottle = $"."
@onready var hearts_container = $HeartsDisplay/HeartsContainer
@onready var label = %Label

var rand_x
var rand_y
var banana_item = load("res://scenes/banana.tscn")
var bottle_item = load("res://scenes/bottle.tscn")
var paper_item = load("res://scenes/paper.tscn")

var trash_items = [banana_item, bottle_item, paper_item]

func _ready():

func _on_timer_timeout():
		for i in range(Global.max_trash):
			var index = randi_range(0, 2)
			var trash_instance = trash_items[index].instantiate()
			rand_x = randi_range(0, 568)
			rand_y = randi_range(-900, 0)
			trash_instance.global_position = Vector2(rand_x,rand_y )
			label.text = "Score: " + str(Global.points)

i think the problem is how the bannana,paper,and bottle are imported and i get the error where im trying to access the signal from the banana, paper, bottle nodes

here is the code for the banana scene(its similar to the paper and bottle one)

extends Area2D

signal health_changed

func _process(delta):
	global_position.y += Global.speed * delta 

func _ready():

func _on_body_entered(body):
	if body.is_in_group("player"):
		Global.points += 1

		Global.current_health -= 1

any help will be greatly appreciated

You cannot connect the signal to the banana_item directly, as it is a PackedScene and not a trash item.

Try instead to connect the signal with the trash_instance in your timeout handler.

1 Like

thank you for the solution it worked

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.