Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Godot 4 Game Development Cookbook

You're reading from   Godot 4 Game Development Cookbook Over 50 solid recipes for building high-quality 2D and 3D games with improved performance

Arrow left icon
Product type Paperback
Published in Jun 2023
Publisher Packt
ISBN-13 9781838826079
Length 258 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jeff Johnson Jeff Johnson
Author Profile Icon Jeff Johnson
Jeff Johnson
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Chapter 1: Exploring the Godot 4 Editor 2. Chapter 2: Transitioning to GDScript 2.0 FREE CHAPTER 3. Chapter 3: 2D and 3D Rendering with Vulkan 4. Chapter 4: Practicing Physics and Handling Navigation in Godot 4 5. Chapter 5: Playing with Shaders in Godot 4 6. Chapter 6: Importing 3D Assets in Godot 4 7. Chapter 7: Adding Sound and Music to Your Game 8. Chapter 8: Making 2D Games Easier with TileSet and TileMap 9. Chapter 9: Achieving Better Animations Using the New Animation Editor 10. Chapter 10: Exploring New Multiplayer Features in Godot 4 11. Index 12. Other Books You May Enjoy

Using callables with signals

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.

Getting ready

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.

How to do it…

Let’s start by creating a Button node and referencing it to the button variable:

  1. Add a script named Callables to Node2D and delete all of the default lines except line 1 and the _ready() function.
  2. In the new scene named Callables that you created, add a Button node and make it big enough to see.
  3. Let’s use @onready and create a variable called button to reference our Button node:
    1  extends Node2D                                   
    2       
    3  @onready var button = $Button
  4. On line 8, we create a function called signal_callable():
    8  func signal_callable():             
    9      print("This method was called by the button pressed signal.")
  5. On line 5, in the _ready() function, we connect a callable signal to the signal_callable() function:
    5  func _ready():                                    
    6      button.pressed.connect(signal_callable)
  6. Now click the Run the current scene button or hit the F6 key.
  7. Let’s use the .bind method when we connect the signal. On line 6, add .bind after signal_callable:
    6      button.pressed.connect(signal_callable.bind("binding_")) 
  8. We need to add a parameter to the signal_callable function:
    8  func signal_callable(param):         
    9      print(param, "This method was called by the button pressed signal.")
  9. Now click the Run the current scene button or hit the F6 key.
  10. Let’s add a new function on line 11 called player_text:
    10 func player_text(param: String):                
    11     print(param)              
  11. Go to line 7, hit the Tab key, and add more code to the ready function.
  12. Let’s create a variable called pt equal to player_text:
    7      var pt = player_text      
    8      pt.call("Hello, NPC!")          
  13. Now click the Run the current scene button or hit the F6 key.

How it works…

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)

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)

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)

Figure 2.13 – Using .call() with a variable of the function (code for steps 10–13)

You have been reading a chapter from
Godot 4 Game Development Cookbook
Published in: Jun 2023
Publisher: Packt
ISBN-13: 9781838826079
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image