The connect method
- Game Development course
- Intermediate
- Lesson 69/83
- 4 min
Learn to connect a signal to a function with connect.
connect method and Callable
To connect a signal to a function, the connect method is used. In Godot 4, you access the signal directly with a dot and provide the target function as a Callable.
func _ready():
var buton = $Button
buton.pressed.connect(_on_button_pressed)
func _on_button_pressed():
print("Butona tiklandi!")Once this connection is established, whenever the signal triggers, the target function is called automatically.
Passing extra arguments with bind
Sometimes you want to pass extra data to the target function when the signal fires. For this, the bind method is used.
func _ready():
$BtnA.pressed.connect(_on_secim.bind("A"))
$BtnB.pressed.connect(_on_secim.bind("B"))
func _on_secim(ad: String):
print("Secilen: ", ad)bind appends extra arguments to the signal's parameters, making it easy to reuse the same handler across multiple nodes.
Practice Show connect syntax
Define a _ready function and print the text "button.pressed.connect(_on_pressed)" using print to demonstrate the signal connection syntax.
Starter code
extends Node
# _ready fonksiyonunu tanimla ve baglanti kodunu yazdirSample solution
extends Node
func _ready():
print("button.pressed.connect(_on_pressed)")Expected output
button.pressed.connect(_on_pressed)
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is the correct line in Godot 4 to connect a button's pressed signal to the '_on_tiklandi' function in code?
- button.call_when_clicked(_on_tiklandi)
- button.listen("_on_tiklandi")
- button.attach_event(_on_tiklandi)
- button.pressed.connect(_on_tiklandi) Correct answer
Explanation: In Godot 4, the 'connect' method on signal objects takes the target function reference (Callable) as an argument.
2 Connecting a signal
func _ready():
$Timer.timeout.____(_on_timer_timeout)
func _ready():
$Timer.timeout.connect(_on_timer_timeout)
Explanation: The 'connect' method is used to bind a signal to a receiving function.
3 Connection terms
| Term | Meaning |
|---|---|
connect |
Connects signal to function |
Callable |
Callable function reference |
bind |
Appends fixed argument to function |
disconnect |
Detaches the signal connection |