Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning GDScript by Developing a Game with Godot 4

You're reading from  Learning GDScript by Developing a Game with Godot 4

Product type Book
Published in May 2024
Publisher Packt
ISBN-13 9781804616987
Pages 378 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Sander Vanhove Sander Vanhove
Profile icon Sander Vanhove
Toc

Table of Contents (22) Chapters close

Preface 1. Part 1:Learning How to Program
2. Chapter 1: Setting Up the Environment 3. Chapter 2: Getting Familiar with Variables and Control Flow 4. Chapter 3: Grouping Information in Arrays, Loops, and Dictionaries 5. Chapter 4: Bringing Structure with Methods and Classes 6. Chapter 5: How and Why to Keep Your Code Clean 7. Part 2: Making a Game in Godot Engine
8. Chapter 6: Creating a World of Your Own in Godot 9. Chapter 7: Making the Character Move 10. Chapter 8: Splitting and Reusing Scenes 11. Chapter 9: Cameras, Collisions, and Collectibles 12. Chapter 10: Creating Menus, Making Enemies, and Using Autoloads 13. Chapter 11: Playing Together with Multiplayer 14. Part 3: Deepening Our Knowledge
15. Chapter 12: Exporting to Multiple Platforms 16. Chapter 13: OOP Continued and Advanced Topics 17. Chapter 14: Advanced Programming Patterns 18. Chapter 15: Using the File System 19. Chapter 16: What Next? 20. Index 21. Other Books You May Enjoy

Creating the player script

This is the moment we have been training for. We already know how to do this! So, start by creating a new script that is attached to the Player node:

  1. Right-click the Player node and select Attach Script:
Figure 6.18 – Attaching a script to the Player node

Figure 6.18 – Attaching a script to the Player node

  1. In the dialogue that pops up, call the script player.gd:
Figure 6.19 – Calling the script player.gd

Figure 6.19 – Calling the script player.gd

  1. We’ll keep it simple for now and just add some code to manage the health of the player:
    extends Node2D
    const MAX_HEALTH: int = 10
    var health: int = 10
    func add__health_points(difference: int):
       health += difference
       health = clamp(health, 0, MAX_HEALTH)

    The clamp() function we use in the add_health_points() function takes a numeric value as the first argument and keeps it in between the second two numeric parameters.

This way, the health is always between 0 and MAX_HEALTH...

lock icon The rest of the chapter is locked
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 $15.99/month. Cancel anytime