How to connect a signal with extra arguments in Godot 4

1.9k Views Asked by At

This would be the part of the code that gives me an error.

for y in range(rows):
        var row = []
        for x in range(columns):
            var label = Button.new()
            row.append(label)
            self.add_child(label)
            label.group = "letters"  # Asignar el grupo "letters" a los botones
            label.connect("pressed", self, "_on_LetterPressed", [x, y])  # Conectar la señal de presionado
        grid.append(row)

In Godot 3 the connect works fine but the label.groups doesn't work, how can I do this in Godot 4?

1

There are 1 best solutions below

0
On

How to upgrade connect from Godot 3 to Godot 4

Let us start with the simple case. If in Godot 3 you connect like this:

Godot 3

object.connect("signal_name", self, "method_name")

You can do the same in Godot 4, by making a Callable:

Godot 4

object.connect("signal_name", Callable(self, "method_name"))

By the way, Godot will make a Callable for us like this:

Godot 4

object.connect("signal_name", method_name)

Furthermore, you can use the Signal like this:

Godot 4

object.signal_name.connect(method_name)

With extra arguments

However, in your case you have extra arguments:

Godot 3

object.connect("signal_name", self, "method_name", [extra, arguments])

What we are going to do in Godot 4 is bind them to the Callable like this:

Godot 4

object.connect("signal_name", Callable(self, "method_name").bind([extra, arguments]))

Or like this:

Godot 4

object.connect("signal_name", method_name.bind([extra, arguments]))

Or like this:

Godot 4

object.signal_name.connect(method_name.bind([extra, arguments]))