• Godot Help
  • Best way to connect signals from "prefabs" to nodes in other scenes?

I'm following a YouTube tutorial series, and in this video we send a signal from a coin scene to a HUD to display the number of coins counted. The instructor mentions that he doesn't know a way to connect the coin scene itself to the HUD in the level scene.

I'm wondering, is there any way to do this? Manually setting the connection in every single instance seems like an anti-pattern. I know Unity has "prefab" objects and objects you can set to not destroy on load, so you can just create a HUD reference in the prefab that all of its instantiated copies will automatically receive as well.

Is there an analogue in Godot to the Unity pattern? Some way to set a connection from a "prefab" scene to a node in another scene, so that all of its instances will have the connection set up as well?

In Godot almost everything is a Scene. A Scene is like a Prefab in Unity, except in Godot it is more advanced (you can have arbitrary levels of nesting, use inheritance, and other things).

Signals can be connect to instances of objects. An object can be a Scene, which could be a coin, your player, the HUD, or the whole level itself. I don't know what they video was about, and I don't have time to watch it, but this feature should be no problem.

On the main script (root node of the scene called "App"):

onready var count = get_node("VBox/Count")
var number = 0

func add_to_count():
	number += 1
	count.text = str(number)

On the coin or a button:

onready var app = get_node("/root/App")

func _ready():
	var _id = connect("button_up", self, "handle_click")
	
func handle_click():
	app.add_to_count()

I will upload an example project but the forum is having problems with zip files right now.

    cybereality

    Gotcha, thank you! so in this case, the scene's node structure would look like:

    root
      App
      Coin
      Vbox	     # A CanvasLayer
        Count    # A Label

    Right?

    Yes, that is one structure that would work, but many others would too.