+1 vote

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 "isconnected()" returns true, it doesn't seem to run the "updatehealth_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.

Godot version Godot 3.3.2.stable.official
in Engine by (18 points)

2 Answers

0 votes

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.

by (430 points)
edited by
0 votes

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")
by (4,530 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.