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

Working with lambda functions

In this recipe, we are going to go through some examples of how to use lambdas in Godot 4. First, we create a lambda that takes a greeting parameter, then we will call the lambda and pass in "hello" to the parameter. In the next example, we declare a variable called health outside of the player_health lambda and call the variable inside of the lambda. We will learn two ways to write a lambda function with a button signal. Finally, we use a lambda function, moving the button across the screen with a tween.

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 Lambda.

How to do it…

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

  1. Add a script named Lambda to Node2D and delete all of the default lines except line 1 and the _ready() function.
  2. In the new scene named Lambda 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 5, in the _ready() function, we create a lambda that will pass in "hello" to the lambda parameter greeting:
    5  func _ready():            
    6      var lambda = func(greeting):                   
    7          print(greeting)                         
    8      lambda.call("hello")                        
  5. On line 10, we declare a variable called health:
    10     var health = 100 
  6. On line 11, we create a lambda function called player_health:
    11     var player_health = func(): print("Current health ", health)
  7. On line 12, we call the player_health lambda function:
    12     player_health.call()                    
  8. On line 14, we create a lambda function to run when the button is pressed:
    14     button.pressed.connect(func(): print("button was pressed"))
  9. On lines 16–18, we create a lambda to run when the button is released:
    16     var button_released = func():               
    17         print("Button released")                  
    18     button.button_up.connect(button_released)
  10. On lines 20–21, we create a tween to move the button across the screen:
    20     var tween = create_tween()        
    21     tween.tween_method(func(pos): button.position.x = pos, 0, 500, 1)
  11. Now click the Run the current scene button or hit the F6 key.
Figure 2.9 – Lambda code (GDScript for steps 3–10)

Figure 2.9 – Lambda code (GDScript for steps 3–10)

How it works…

We added a script called Lambda 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 Lambda script, we used @onready to declare a variable called button to the Button node.

We created a variable called lambda equal to the lambda function with the greeting parameter, which prints the greeting. Since lambdas are a type of callable, we call the lambda variable with the "hello" string to be used as the greeting.

We declared a variable called health and gave it a value of 100 in line 10. You can use variables from the outer class or outer function inside the lambda. In line 11, we created a lambda called player_health, which prints out Current health and the value of the health variable. In line 12, we call the player_health lambda to print out ("Current health ", health) to the console.

We pass the lambda as a function argument. When the pressed() button signal is emitted, Button was pressed will be printed to the console.

We essentially do the same thing we did last in the step except we use more than one line. When the button_up() signal is emitted, Button released is printed to the console.

On line 20, we create a tween. On line 21, we use a lambda in tween_method() to move the Button node from position (0) to position (500) with a duration of (1). If we wanted the button to go slower, we would increase the duration number.

We run the current scene. It shows the button move across the screen and on the console, you will see "hello" and "Current health 100". After you click the button, you will see Button was pressed and Button released.

Figure 2.10 – Button and console output

Figure 2.10 – Button and console output

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