Book Image

Godot 4 Game Development Cookbook

By : Jeff Johnson
5 (1)
Book Image

Godot 4 Game Development Cookbook

5 (1)
By: Jeff Johnson

Overview of this book

Want to transition from Godot 3 to 4? Look no further than the Godot 4 Game Development Cookbook. This comprehensive guide covers everything you need to become proficient with the latest GUI, GDscript 2.0, Vulkan 2D/3D rendering, shaders, audio, physics, TileSet/TileMap, importing, sound/music, animation, and multiplayer workflows. With its detailed recipes, the book leaves no stone unturned. The Godot 4 Cookbook begins by exploring the updated graphical user interface and helps you familiarize yourself with the new features of GDscript 2.0. Next, it delves into the efficient rendering of 2D and 3D graphics using the Vulkan renderer. As it guides you in navigating the new Godot 4 platform, the book offers an in-depth understanding of shaders, including the latest enhancements to the shader language. Moreover, it covers a range of other topics, including importing from Blender, working with audio, and demystifying the new Vulkan Renderer and the physics additions for 2D and 3D. The book also shows you how the new changes to TileSet and TileMap make 2D game development easy. Advanced topics such as importing in Godot 4, adding sound and music to games, making changes in the Animation editor, and including workflows for multiplayer in Godot 4 are covered in detail. By the end of this game development book, you’ll have gained a better understanding of Godot 4 and will be equipped with various powerful techniques to enhance your Godot game development efficiency.
Table of Contents (13 chapters)

Investigating annotations in Godot 4

In this recipe, we will first look at @export and some of the variations associated with that annotation and then show how to use @onready.

Getting ready

For this recipe, open Godot 4 and start a new project called Chapter 2.

How to do it…

To investigate annotations, we will use each in a script by doing the following:

  1. Open a new project in Godot.
  2. In the Scene tab, click 2D Scene.
  3. Click on the paper icon above Node2D and to the right of the filter Search box or in the Inspector tab under Node | Script. Click <empty>, then select New Script, and name the script Annotations.
  4. Click on Script at the top of the editor to look at the new script we added.
  5. @export is the same as export in Godot 3.x, so we will add a score to the node so we can see it in the Inspector tab:
    1  extends Node2D                           
    2     
    3  @export var score = 0
  6. As you saw from the auto-completion, there are new @export options available now. Let’s add @export_range() to line 4:
    4  @export_range(0, 100, .1) var input_range

Note

This gives a range from 0 to 100 in increments of .1 to the input_range variable, which you can change in the Inspector tab.

  1. We can show named enum values in the Inspector tab by adding the following to lines 5 and 6:
                                                          
    5    enum WeatherEnum {Sunny, Rainy, Cloudy = -1}
                                                       
    6    @export var weather: WeatherEnum
  2. On line 7, let’s add a file to show up on the Inspector tab using @export_file:
    7    @export_file("*.txt") var file
  3. Click on Node2D in the Scene tab. Then, click on + under Scene in the Scene tab to bring up the Create New Node window.
  4. Then, type camer2d in the Search box. Select and add Camera2D to Node2D.
  5. The onready keyword is now @onready. We will add a Camera2D node and use @onready with the camera on line 7:
    8    @onready var camera = $Camera2d
  6. Click on Node2D at the top of the tree in the Scene tab to see everything we used with @export in the Inspector tab.
Figure 2.1 – Annotations.gd

Figure 2.1 – Annotations.gd

How it works…

We added a score variable so we can could see it in the Inspector tab using @export instead of the export keyword.

We added @export_range(0, 100, .1) var input_range to show a range of 0 to 100 that snapped the value in .10 increments. You could also just use @export_range (0, 100) var input_range or @export_range (0, 100) var input_range: float.

We created an enum and then we used @export var weather: WeatherEnum to put the enum into the weather variable so we could see it in the Inspector tab. You could also do @export_enum(Sunny, Rainy, Cloudy) var weather.

We used the @export_file("*.txt") var file to show how a file is seen in the Inspector tab.

We added a Camera2D node to Node2D so we could use @onready with it. First, we just used @onready var camera = $Camera2d to add the Camera2D node to the camera variable.