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 the super keyword to call a function

In Godot 3.x, we used to call a function of the parent class from a subclass by using the .function of the parent class. Now we use the super keyword. In the example, we will use in this recipe, we have SniperEnemyClass, which is inherited from DefaultEnemyClass. If we wanted to call the rifle function in DefaultEnemyClass from SniperEnemyClass, we would use the super keyword, but in Godot 3.x, we use .rifle().

Getting ready

For this recipe, create a new scene by clicking + to the right of the current Scene tab and clicking Node2D. Select Save Scene As and name it Super.

How to do it…

We will create two classes called SniperEnemyClass, which is inherited from DefaultEnemyClass. The DefaultEnemyClass class has two functions called rifle and orders. We will use the super keyword from SniperEnemyClass to call the two functions:

  1. Add a script named Super to Node2D and delete all of the default lines except line 1.
  2. Let’s start on line 11 and create DefaultEnemyClass:
    11 class DefaultEnemyClass extends Node2D:          
    12     func rifie():                   
    13        print("Basic rifle")                        
    14     func orders():                              
    15        print("Guard the front gate.")
  3. Now let’s create SniperEnemyClass starting on line 17:
    17 class SniperEnemyClass extends DefaultEnemyClass:
    18     func rifle():                              
    19        print("Sniper rifle")                   
    20        super.orders()                          
    21        super()
  4. We need to add a couple of variables to the _ready() function so we can see the output of the print statement on the console. Let’s start on line 3:
    3  func _ready():                           
    4      var enemy = DefaultEnemyClass.new()          
    5      var sniper = SniperEnemyClass.new()          
    6  
    7      enemy.rifle()                                
    8      enemy.orders()                              
    9      sniper.rifle()
  5. Now click the Run the current scene button or hit the F6 key.
Figure 2.6 – super keyword code (GDScript for steps 2–4)

Figure 2.6 – super keyword code (GDScript for steps 2–4)

How it works…

We added a script to Node2D and named it Super. Then we deleted all of the lines in the script except line 1.

We made a class called DefaultEnemyClass, which extends Node2D so, later, we can see the output on the console. In lines 12–13, we made a function called rifle to print out a rifle. In lines 14–15, we made a function called orders to print out the orders.

We made a class called SniperEnemyClass, which extends DefaultEnemyClass. In lines 18–19, we created a rifle function, which prints out a sniper rifle. In line 20, we called the orders function in the DefaultEnemyClass class using the super keyword. In line 21, we called the rifle function in DefaultEnemyClass from SniperEnemyClass using the super() keyword. As long as you are in a function that is in both parent and child classes, you can use super(). If you want to call a different function in DefaultEnemyClass, then you have to use super.function.

In lines 4–5, we created enemy and sniper instances for the two classes. In line 7, we called the rifle() function in DefaultEnemyClass to see which rifle was set as the default. In line 8, we called the orders() function in DefaultEnemyClass to see the enemy orders. In line 9, we called the rifle() function in SniperEnemyClass.

We ran the current scene. We saw Basic rifle, Guard the front gate., Sniper rifle, Guard the front gate., and Basic rife on the console.

We get the following console output when we run the current scene:

Figure 2.7 – super keyword console output

Figure 2.7 – super keyword 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