-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Godot 4 Game Development Cookbook
By :
In this recipe, we will see how callables can be used with signals. We will also look at the call and bind callable methods. Callables can be held in variables and passed into functions. As such, you can use them in arrays and in dictionaries as the key or the value.
For this recipe, create a new scene by clicking + to the right of the current Scene tab and adding Node2D. Select Save Scene As and name it Callables.
Let’s start by creating a Button node and referencing it to the button variable:
Callables to Node2D and delete all of the default lines except line 1 and the _ready() function.Button node and make it big enough to see.@onready and create a variable called button to reference our Button node:1 extends Node2D 2 3 @onready var button = $Button
signal_callable():8 func signal_callable():
9 print("This method was called by the button pressed signal.")_ready() function, we connect a callable signal to the signal_callable() function:5 func _ready(): 6 button.pressed.connect(signal_callable)
.bind method when we connect the signal. On line 6, add .bind after signal_callable:6 button.pressed.connect(signal_callable.bind("binding_")) signal_callable function:8 func signal_callable(param): 9 print(param, "This method was called by the button pressed signal.")
player_text:10 func player_text(param: String): 11 print(param)
ready function.pt equal to player_text:7 var pt = player_text
8 pt.call("Hello, NPC!") We added a script called Callables to Node2D and deleted everything in the script except line 1 and the _ready() function. Then we created a Button node in the Scene tab. In the Callables script, we used @onready to declare a variable called button to the Button node.
We created a function that we want to run when the button pressed signal is true. Notice that we don’t have to connect the signal in the editor. We can use any method that we want.
In the _ready() function, we used the button reference to connect the signal_callable function when the signal pressed is true, which happens when the button is pressed.
We ran the current scene. In the console, we saw This method was called by the button pressed signal. after we clicked the button.
Figure 2.11 – Using callables with signals (code for steps 4–6)
We used the callable bind method. In line 6, we added .bind like this:
button.pressed.connect(signal_callable.bind("binding_")).
We needed to add a parameter to the function and inside of the print statement to see what we added with bind.
We ran the current scene. In the console, we saw binding_This method was called by the button pressed signal. printed after we clicked the button.
Figure 2.12 – Using callable with the bind method (code for steps 7–9)
We added a function called player_text, which takes a parameter called param that only accepts strings. It prints out the parameter that will be passed in when we use .call.
We created a variable for the player_text() function When you enter var pt = player_text, autocomplete wants to add the () at the end – make sure you delete it. We converted the function into a variable, so now we can use the function in arrays, dictionaries, or in any other way you can use a variable.
We ran it to see Hello, NPC! printed in the console.
Figure 2.13 – Using .call() with a variable of the function (code for steps 10–13)