Connecting the HUD to multiple nodes

Godot Version

4.5

Question

I’m making a turn-based strategy game, what I’m currently doing right now is making the button in the swordsman to overlay a HUD. However I don’t know whether it would be better to connect it another way because I have it directly connected to the HUD. This is because I want to insatiate the swordsman multiple times on the main scene. If there is a better way to connect the button please let me know!

Connect it to swordsman and then connect swordsman to hud.

How would I exactly do that?

Implement a custom signal in swordsman script that forwards button’s signal.

1 Like

You can also do something like

func _ready():
	var swordsman_interact_button = get_node("../HUD/Canvas")
	swordsman_interact_button.pressed.connect(_swordsman_interact_button_pressed)

As long as you have a reference to the signal (like button.pressed) and the function (like my_swordsman._button_pressed_response) you want to run when that signal is sent you can do .connect()

Oftentimes this makes sense to do right when you instantiate something like the Swordsman because you already have a reference to everything you need.

1 Like

Technically you can but if swordsman is meant to be a reusable component this would break component’s encapsulation. So whenever the internal structure of the scene changes for whatever reason, each place in code where you rely on it in this way will need to be updated. Whereas if you funnel everything into the interface in the top node, the users of the component don’t need to concern themselves about its internal structure.

1 Like