Learning GDScript by Developing a Game with Godot 4

Book description

Learn the Godot 4 game engine and GDScript 2.0 as you build your own game and transform into a proficient programmer with this illustrated guide

Key Features

  • Learn the fundamentals of programming as you write GDScript 2.0
  • Explore the world of GDScript 2.0 with this comprehensive introduction to the language
  • Work with Godot 4’s robust features and built-in tools to create captivating 2D and 3D games, simulations, and interactive applications
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

In the digital landscape driven by interactive experiences, the demand for creative individuals with the skills to create captivating games has never been higher. Written by Sander Vanhove, a seasoned game developer with over 20 games to his credit, this book will serve as your entry point into game development, showing you how to leverage the powerful features of the open-source, versatile GDScript 2.0 to develop your ideas, from simple platformers to complex RPGs.

Whether you're an aspiring game developer, a hobbyist seeking a creative outlet, or simply someone intrigued by the world of game programming, this book will guide you through the intricacies of the Godot 4 game engine. Starting with a primer on the fundamentals of programming, you’ll cover everything from data to logic, while familiarizing yourself with Godot’s built-in tools such as the physics engine, navigation, and cameras. As you progress, you’ll unlock deeper insights into more advanced tools that will take your programming to the next level. Aided by easy-to-follow step-by-step tutorials, examples, exercises, and experiments, you’ll seamlessly integrate this newfound knowledge to create a Vampire Survivor-like game from scratch.

By the end of this book, you’ll have become proficient in leveraging the Godot 4 game engine to bring your gaming visions to life.

What you will learn

  • Develop your GDScript 2.0 programming skills from basic to advanced, emphasizing code cleanliness
  • Harness Godot 4's integrated physics engine to control and manipulate in-game objects
  • Design a vibrant and immersive game world by seamlessly integrating a diverse array of assets
  • Master the art of processing input from various sources for enhanced interactivity
  • Extend the reach of your game by learning how to export it to multiple platforms
  • Incorporate simple multiplayer functionality for a dynamic gaming experience

Who this book is for

This book is for programmers, game designers, game developers, and game artists who want to start creating games in Godot 4. If you’re new to coding or game development, looking for a new creative outlet, and want to give Godot 4 and GDScript 2.0 a try, this book is for you. While no prior knowledge of programming or Godot is required, this book gradually introduces more complex concepts as you advance through the chapters.

Table of contents

  1. Learning GDScript by Developing a Game with Godot 4
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1:Learning How to Program
  7. Chapter 1: Setting Up the Environment
    1. Technical requirements
    2. Godot game engine and open-source software
      1. Some background on the engine
      2. What is open-source software?
    3. Getting and preparing Godot
      1. Downloading the engine
      2. Creating a new project
      3. Light mode
      4. Creating the main scene
      5. A brief UI overview
      6. Writing our first script
    4. The Godot Engine Documentation
    5. Join our community!
    6. Summary
    7. Quiz time
  8. Chapter 2: Getting Familiar with Variables and Control Flow
    1. Technical requirements
    2. What are variables?
      1. Variables – Drawers in a filing cabinet full of data
      2. Naming variables
      3. Variables in GDScript
      4. Printing out variables
      5. Changing a variable’s value
      6. Mathematical operators
      7. Other assignment operators
    3. Data types – Integers, floats, and strings
      1. Integers
      2. Floats
      3. Strings
    4. What are constants?
      1. Constants in GDScript
      2. Magic numbers
    5. Creating new scenes
    6. Getting started with control flow
      1. The if statement
      2. The if-else statement
      3. The elif statement
      4. Commenting in code
      5. Indentation
    7. Boolean logic
      1. The match statement
      2. The ternary-if statement
    8. Additional exercises – Sharpening the axe
    9. Summary
    10. Quiz time
  9. Chapter 3: Grouping Information in Arrays, Loops, and Dictionaries
    1. Technical requirements
    2. Arrays
      1. Creating an array
      2. Accessing values
      3. Accessing elements backward
      4. Changing the elements of an array
      5. Data types in arrays
      6. Strings are secretly arrays
      7. Manipulating arrays
      8. Don’t be scared of errors or warnings
    3. Loops
      1. For loops
      2. While loops
      3. Continuing or breaking a loop
    4. Dictionaries
      1. Creating a dictionary
      2. Data types in dictionaries
      3. Accessing and changing values
      4. Creating a new key-value pair
      5. Useful functions
      6. Looping through dictionaries
      7. Nested loops
    5. Null
    6. Additional exercises – Sharpening the axe
    7. Summary
    8. Quiz time
  10. Chapter 4: Bringing Structure with Methods and Classes
    1. Technical requirements
    2. Methods are reusable bits of code
    3. What is a function?
      1. Defining a function
      2. Naming a function
      3. The return keyword
      4. The pass keyword
      5. Optional parameters
    4. Classes group code and data together
      1. Defining a class
      2. Instancing a class
      3. Naming a class
      4. Extending a class
      5. Each script is a class!
      6. When are certain variables available?
      7. The scope of a function
    5. Types help us to know how to use a variable
    6. What is type hinting?
      1. Type hinting variables
      2. Type hinting arrays
      3. Learning about the Variant type
      4. Type hinting function parameters
      5. Type hinting function returns
      6. Using void as a function return
      7. Inferred types
      8. null can be any type
      9. Autocompletion
      10. Using type hinting for named classes
      11. Performance
      12. Editor adding type hints
    7. OOP primer
      1. Inheritance
      2. Abstraction
      3. Encapsulation
      4. Polymorphism
    8. Additional exercises – Sharpening the axe
    9. Summary
    10. Quiz time
  11. Chapter 5: How and Why to Keep Your Code Clean
    1. Technical requirements
    2. Back to naming things
      1. Naming conventions
      2. General naming tips
      3. Public and private class members
      4. Make short functions
    3. DRY
    4. Do one thing (KISS)
    5. Defensive programming
    6. Programming style guides
      1. White spacing
      2. Blank lines
      3. Line length
    7. Utilizing the documentation
      1. Accessing a class’s documentation
      2. Directly accessing a function or variable’s documentation
      3. Going to the definition of a function or variable
      4. Searching the documentation
      5. Accessing the online documentation
    8. Summary
    9. Quiz time
  12. Part 2: Making a Game in Godot Engine
  13. Chapter 6: Creating a World of Your Own in Godot
    1. Technical requirements
    2. Game design
      1. Genre
      2. Mechanics
      3. Story
    3. Creating a player character
      1. Adding a sprite
      2. Displaying health
      3. Manipulating nodes in the editor
    4. Creating the player script
      1. Referencing nodes in a script
      2. Caching node references
      3. Trying out the player script
    5. Exporting variables to the editor
      1. Setters and getters
      2. Changing values while the game is running
      3. Different types of exported variables
    6. Creating a little world
      1. Changing the background color
      2. Adding Polygon2D boulders
      3. Node drawing order
      4. Creating an outer wall
      5. Getting creative
    7. Additional exercises – Sharpening the axe
    8. Summary
    9. Quiz time
  14. Chapter 7: Making the Character Move
    1. Technical requirements
    2. Vector math refresher
      1. The 2D coordinate system
      2. What is a vector?
      3. Scaling vectors
      4. Adding and subtracting vectors
      5. More vector operations
    3. Moving the player character
      1. Changing the current player node
      2. Applying forces to the player
      3. Process and physics process functions
      4. Mapping input
      5. Using the input
      6. Smoothing out the movement
    4. Debugging a running game
      1. Breakpoints
      2. Remote tree
    5. Additional exercises – Sharpening the axe
    6. Summary
    7. Quiz time
  15. Chapter 8: Splitting and Reusing Scenes
    1. Technical requirements
    2. Saving a branch as a new scene
      1. Creating a separate player scene
      2. The root node of a scene
    3. Using saved scenes
    4. Organizing scene files
    5. Additional exercises – Sharepening the axe
    6. Summary
    7. Quiz time
  16. Chapter 9: Cameras, Collisions, and Collectibles
    1. Technical requirements
    2. Making a camera that follows the player
      1. Setting up a basic camera
      2. Adding drag margins
      3. Making the camera look ahead
      4. Smoothing out the look ahead
    3. Collisions
      1. The different physics bodies
      2. The Area2D node
      3. Adding a collision shape to the player node
      4. Creating static bodies for the boulders
      5. Creating static bodies for the walls
    4. Creating collectibles
      1. Creating the base collectible scene
      2. Inheriting from a base scene
      3. Connecting to a signal
      4. Writing the code for collectibles
      5. Using collision layers and masks
      6. Your turn!
    5. Additional exercises – Sharpening the axe
    6. Summary
    7. Quiz time
  17. Chapter 10: Creating Menus, Making Enemies, and Using Autoloads
    1. Technical requirements
    2. Creating a menu
      1. Control nodes
      2. Creating a basic start menu
      3. Setting the main scene
    3. Making enemies
      1. Constructing the base scene
      2. Navigating enemies
      3. Writing the enemy script
      4. Damaging the player in a collision
      5. Spawning enemies and collectibles
      6. Making a Game Over screen
    4. Shooting projectiles
      1. Creating the base scene
      2. Writing the logic of the projectile
      3. Spawning projectiles
    5. Storing highscores in autoloads
      1. Using an autoload
      2. Creating a HighscoreManager autoload
      3. Autoloads in the remote tree
      4. Adding a UI in the main menu and game scene
      5. Using the highscore in the main menu
    6. Additional exercises – Sharpening the axe
    7. Summary
    8. Quiz time
  18. Chapter 11: Playing Together with Multiplayer
    1. Technical requirements
    2. A crash course in computer networking
      1. What is a Transport Layer?
      2. What is an Application Layer?
      3. Networking in Godot Engine
      4. Learning about IP addresses
      5. Using port numbers
    3. Setting up the base networking code
      1. Creating the client-server connection
      2. Adding UI
      3. Running multiple debug instances at the same time
    4. Synchronizing different clients
      1. Updating the player scene for multiplayer
      2. Synchronizing EntitySpawner
      3. Synchronizing the enemy and collectibles
      4. Synchronizing the projectile
    5. Fixing the timer and end game
      1. Synchronizing the timer
      2. Synchronizing the end of the game
    6. Running the game on multiple computers
      1. Showing the IP address of the server
      2. Connecting from another computer
    7. Additional exercises – Sharpening the axe
    8. Summary
    9. Quiz time
  19. Part 3: Deepening Our Knowledge
  20. Chapter 12: Exporting to Multiple Platforms
    1. Technical requirements
    2. Exporting for Windows, Mac, and Linux
      1. Downloading the export template
      2. Making the actual exports of the game
    3. Uploading our game to Itch.io
      1. What is Itch.io?
      2. Exporting our game to the web
      3. Uploading to Itch.io
    4. Exporting our game to other platforms
      1. Mobile platforms
      2. Consoles
    5. Summary
    6. Quiz time
  21. Chapter 13: OOP Continued and Advanced Topics
    1. Technical requirements
    2. The super keyword
    3. Static variables and functions
    4. Enumerations
    5. Lambda functions
      1. Creating a lambda function
      2. Where to use lambda functions
    6. Passing parameters by value or reference
      1. Passing by value
      2. Passing by reference
    7. The @tool annotation
    8. Summary
    9. Quiz time
  22. Chapter 14: Advanced Programming Patterns
    1. Technical requirements
    2. What are programming patterns?
    3. Exploring the Event Bus
      1. The problem
      2. The solution
    4. Understanding Object Pooling
      1. The problem
      2. The solution
      3. Implementing the Object Pool in our game
    5. Working with State Machines
      1. The problem
      2. The solution
      3. An example state
    6. Additional exercises – Sharpening the axe
    7. Summary
    8. Quiz time
  23. Chapter 15: Using the File System
    1. Technical requirements
    2. What is the file system?
      1. File paths
      2. User path
    3. Creating a save system
      1. Writing data to the disk
      2. Reading data from disk
      3. Preparing the save manager for use in the game
      4. Adjusting the game to use the save manager
      5. Having a look at the save file
    4. Summary
    5. Quiz time
  24. Chapter 16: What Next?
    1. Ideas for your next projects
      1. Starting a new project
      2. Extending the survivor-like game
      3. Creating another game
      4. Free game assets
    2. Learning about new topics
      1. Following specific tutorials
      2. Reading more books
      3. Reading the Godot Engine documentation
      4. Looking at the game code of other people’s projects
    3. Joining the community
      1. Joining the Forum, Discord, Reddit, or any other platform
      2. Contributing to the Godot Engine project
      3. Joining a game jam
    4. Goodbyes
  25. Index
    1. Why subscribe?
  26. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Learning GDScript by Developing a Game with Godot 4
  • Author(s): Sander Vanhove
  • Release date: May 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781804616987