Godot Version
4.2.2
Question
Problem: Health Bar only appears when the beginning scene has the player.
I have three autoloaded nodes/scripts:
FailureManager
AudioContainer
Hud
I have a player.gd
script. Part of it goes:
func _ready():
FailureManager.initialize_player()
Hud.connect_player_to_health_bar()
Here are the relevant functions in both FailureManager
and Hud
:
FailureManager.gd:
func initialize_player()->void:
_player = get_tree().get_first_node_in_group("Player")
if !(_player && _player is Player):
return
player = _player as Player
Hud:
func connect_player_to_health_bar():
health_bar.get_player()
health_bar.update()
Here is the relevant code for the health_bar
:
@onready var change_timer:Timer = $HealthChangeTimer
...
func get_player():
player = get_tree().get_first_node_in_group("Player")
player.health_changed.connect(func(): change_timer.start())
...
func update()->void:
if !player:
visible = false
return
value = player.player_health / player.PLAYER_MAX_HEALTH * 100.0
Why did I include FailureManager? Because that works without a hitch. The player dies as it should, and the death screen appears without flaw, yet the health bar doesn’t appear at all.