Signal works when connecting through GUI but not through code using connect()

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

Currently on my player script there’s this code that emits its current health when the game starts:

signal update_health(h)

func _ready():
	emit_signal("update_health",health)

I’ve been trying to connect it to the top node on my tree so it can update the GUI by using this code (player node is currently on a group called ‘Players’):

onready var in_game_HUD = $CanvasLayer/InGameHUD
onready var player = get_tree().get_nodes_in_group("Player")[0]

func _ready():
	player.connect("update_health",self,"function")
	
	print(player.is_connected("update_health",self,"update_health_UI"))

func update_health_UI(h):
	in_game_HUD.update_health_GUI(h)

While the “is_connected()” returns true, it doesn’t seem to run the “update_health_UI” and I dont seem to understand why, since connecting this signal with Godot’s GUI works, but sadly I cannot use this method for my project.

:bust_in_silhouette: Reply From: vnmk8

maybe you are emitting the signal before it is connected? since both are on the ready func if your player script is running before the other script the signal doesn’t emitted at the right time.

:bust_in_silhouette: Reply From: Wakatta

Not sure why you get positives however this line

player.connect("update_health",self,"function")

It’s supposed to read as

player.connect("update_health",self, "update_health_UI")