Godot Version
4.6
Question
I’ve had my heart container HUD start to print the starting health twice since I introduced a global auto load to keep track of the player’s health between scenes. It seems to be firing the health signal twice according to my output print.
Health Component Script
extends Node
signal grant_health (HP :int)
signal health_changed(diff: int)
signal health_depleted
@export_subgroup("Settings")
@export var max_health: int = 3 : set = set_max_health, get= get_max_health
@export var immortality : bool = false: set = set_immortality, get = get_immortality
@export var immortality_time : float
var immortality_timer :Timer = null
@onready var health : int = max_health: set = set_health, get = get_health
#func _ready() -> void:
#health=HealthTracker.player_HP
#get_health()
func set_max_health( value:int)->void:
max_health=value
func get_max_health()-> int:
return max_health
func set_immortality(value:bool)-> void:
immortality= value
func get_immortality() -> bool:
return immortality
func set_temporary_immortality(_immortality_time)->void :
if immortality_timer==null:
immortality_timer= Timer.new()
immortality_timer.one_shot=true
add_child(immortality_timer)
if immortality_timer.timeout.is_connected(set_immortality):
immortality_timer.timeout.disconnect(set_immortality)
immortality_timer.set_wait_time(_immortality_time)
immortality_timer.timeout.connect(set_immortality.bind(false))
immortality = true
immortality_timer.start()
func set_health(value:int)->void:
if value < health and immortality:
return
var clamped_value = clampi(value, 0 , max_health)
if clamped_value !=health:
var difference = clamped_value - health
health = clamped_value
HealthTracker.player_HP=health
health_changed.emit(difference)
if health == 0 :
health_depleted.emit()
func get_health()-> int:
var HP:int = health
grant_health.emit(HP)
return health
I’m very confused on why its firing twice, and the HUD behaves the way I expect it to once the player gets hit, which makes it more puzzling to me, however, according to the output print , it does seem to be getting 6 to 4 signals still.
HUD Script
extends CanvasLayer
@export_subgroup("Scenes")
@export_file ("*.tscn") var heart_full : String
@export_subgroup("Nodes")
@export var health_component : HealthComponent
@export var hbox : HBoxContainer
func _ready() -> void:
setup_hp()
func setup_hp():
clean_current_representation()
#_on_health_component_grant_health (Health_Points)
func _on_health_component_grant_health(HP: int) -> void:
print(HP)
draw_hearts(HP)
func _on_health_component_health_changed(_diff: int) -> void:
clean_current_representation()
func draw_hearts(HP:int):
for x in HP :
var heartf = load(heart_full)
var heart_instance = heartf.instantiate()
hbox.add_child(heart_instance)
#if x < HP +1:
#break
#if x < 4:
#continue
func clean_current_representation():
for child in hbox.get_children():
child.queue_free()
and it seems to be primarily due to the introduction of the global Autoload, which by itself is quite simple, any help would be greatly appreciated.
Health Tracker Global Script
extends Node
const HEALTH_COMPONENT = preload("uid://dtkmr0o3nr2li")
var health_component=HEALTH_COMPONENT.instantiate()
var player_HP : int = health_component.max_health