How to connect signals through a script between nodes?

Godot Version

4.5

Question

Using the tutorial I found, I still don’t get how to connect a signal after its been emitted. Currently what I’m trying to do is have a button connect to the swordsman script which then emits a signal that passes on a health value, which should be connected in the EnemyStorage script which should then print the health value passed on by the Swordsman. However, I’m struggling with connecting the swordsman to the EnemyStorage.

scene tree

Swordsman Script

EnemyStorage Script

Look into Object class reference to see what arguments need to be passed to connect() call.

Where would I do that?

In Godot’s official documentation.

1 Like

I’ve looked and I don’t really get it tbh. Reading everything feels very confusing

The docs can be a bit overwhelming when you’re starting out, but it’s absolutely worth trying to understand them.

I assume you’ve found the page for the Object class?

If you scroll down a bit you’ll see a heading “methods”, where you can see all the methods you can use on an Object:


This list is also where you’ll find connect. If you click on its name it will take you to a detailed description of what it does.

Honestly this isn’t really a safe way to connect signals..
As the second note says, it’s better to use Signal.connect

1 Like

Hi,

it is not that complicated, make use of a singleton.

  • Create a new script that extends the node class, and define one or more signals on it.
    (just put in : “signal SwordsmanInteraction”), save it as SignalManager.gd

  • In the Project Settings, navigate to the Autoloads tab and register your new script as an auto-loaded node.

  • Connect another node to the Events singleton via code like you would with any other: SignalManager.connect(“SwordsmanInteraction”, self, “_on_Events_SwordsmanInteraction”).
    (this will be on the enemy storage script)

  • create the function, func _on_Events_SwordsmanInteraction(health): your code

  • from any script in your project, you can then write SignalManager.SwordsmanInteraction.emit(health) to emit the corresponding signal.
    (this will be on the swordsman script)

The singleton will act as a relay, whoever is connected to the signal will receive the emit.
The emiting script doesnt need to care who is connected to the signal.

Source for this workflow is gdquest:

1 Like

To use globalized signals is not a good advice for someone who doesn’t fully understand basic signal functionality.

after taking a break, I took a shot at it again, using the event singleton made my life a lot easier and I wasn’t able to connect the signals between nodes because I was using the godot-3 method while I was using godot-4. Thank you everyone for your help