How does signals actually work?

Godot Version

4.5.1

Question

I am new and learning how to make games. I watched a few videos about it. In videos they say, that you need to separate components to make cleaner code. To do that you use signals. So, of course I want to use signals, but I build the buttons dynamically, so only parent can catch the signal. And of course I build parent dynamically, because of course I would…

So what I can do is: get a signal, retranslate it in the parent and do it again it the next parent… But that seems not how code separation should work. Is there a better way of doing things?

My node structure looks something like this:

Main_node
- Background
-- Action_node -> I want to catch signal here
- Control
-- ScrollContainer
--- DynamicParent
---- DynamicButton -> I emit signal here

Any suggestions are very welcome.

EDIT:
By Dynamic, I mean I have a scene file and I create ~20 elements in for loop.

In your ActionNode script, make an export variable of type DynamicButton. In the editor, assign the button to this variable. In _ready(), you can then connect to the pressed signal of the button.

There are several different ways. One way is from Main_node:

$Control/ScrollContainer/Dynamic/DynamicButton.connect($Action_node.processDynamicButtonPressed)

Thank you for suggestions, but I don’t think that it will work in my case.

First of all I don’t know the names of DynamicParent and Dynamic button, I think, I can name them, but there lies the second problem my live structure looks something like this:
[code]

  • Scroll
    • DynamicParent1
      • DynamicButton1
      • DynamicButton2
        ….
      • DynamicButton20

- - DynamicParent2

      • DynamicButton1
      • DynamicButton2
        ….
      • DynamicButton20

[/code]
So I would have to map all the buttons if that is even possible.

For me DynamicButton is a scene file. If I attach scene via Inspector, but I don’t create DynamicButtons in the ActionNode I don’t think that I can access the signal. Or I don’t know/understand how to do that.

You can utilize the "static” parents “child entered” signal to connect the child’s pressed signal / child entered signal to whoever you’d like, as long as the “static” parent has a reference.

I would suggest reparenting / refactoring your action node to make the connection easier.

Connect the signals in the same piece of code that instantiates “dynamic” things.

1 Like
  1. I have two levels of Dynamic Objects, so I don’t think that Static parent will help me much.

  2. I am making tower defense game. ActionNode is my main map. DynamicButtons are in the control panel, for a player to buy towers and place them on the map.

My suggestion is to stop what you’re doing, read the documentation about signals, and make a project where you play with the various ways in which signals can be used.

1 Like
  1. I’ve got a setup that would work as described.
  2. Okay. As long as you can make it work, I would’ve structured my tree the same. But I am not so great at scene tree construction. I’ve got a similar thing in a side project but it feels convoluted, but this is off topic.

For now - take a break from the tower defense project, start a new project that just messes around with Node and it’s signals, read the docs (embedded in the editor! Don’t forget it’s always there, waiting…) for Signal and look at the built in methods.