0% found this document useful (0 votes)
15 views

Class Inheritance Godot GDScript Tutorial Ep 17 Godot Tutorials

The document discusses class inheritance in Godot and GDScript. It explains that a subclass inherits from a superclass, gaining its functions and variables. Subclasses can override superclass functions. Inheritance allows for cleaner code and easier definition of classes by allowing the reuse of classes. The tutorial uses an Animal superclass and Horse subclass as an example, showing how the Horse class inherits health and a getHealth() method from Animal and can override the attack() method.

Uploaded by

Chris Lewinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Class Inheritance Godot GDScript Tutorial Ep 17 Godot Tutorials

The document discusses class inheritance in Godot and GDScript. It explains that a subclass inherits from a superclass, gaining its functions and variables. Subclasses can override superclass functions. Inheritance allows for cleaner code and easier definition of classes by allowing the reuse of classes. The tutorial uses an Animal superclass and Horse subclass as an example, showing how the Horse class inherits health and a getHealth() method from Animal and can override the attack() method.

Uploaded by

Chris Lewinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Class Inheritance | Godot GDScript Tutorial | Ep 17 | Godot Tutorials 5/8/23 4:44 PM

Courses / Introduction-To-Gdscript / Godot-Tutorials-Gdscript-17

Class Inheritance | Godot GDScript Tutorial | Ep


17

Class Inheritance | Godot GDScript Tutorial | Ep 17

PREV EPISODE NEXT EPISODE

Learning Materials
 Article  Resource

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-17 Page 1 of 4
Class Inheritance | Godot GDScript Tutorial | Ep 17 | Godot Tutorials 5/8/23 4:44 PM

Class Inheritance
Class inheritance is a mechanism where you can derive a class from another class for a hierarchy of classes that share a set of attributes
and methods

To inherit from a class, use the extends keyword:

# Animal.gd
extends Node2D # Inherits from the Global Node2D class

class_name Animal

# Horse.gd
extends Animal # Inherits from the Global Node2D class

class_name Horse

Animal.gd

Horse Inherits from Animal

Horse.gd

In this case, the Animal class is called a superclass, while the Horse class is called the subclass.

When a subclass inherits from a superclass, the subclass gets all the functions and class variables of the superclass.

# Animal.gd
extends Node2D # Inherits from the Global Node2D class

class_name Animal

var health: int = 100

func getHealth() -> int:


return health

# Horse.gd
extends Animal

# Horse class has the health variable and the getHealth() method

class_name Horse

func newlyCreatedFunction() -> void:


print(getHealth()) # 100

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-17 Page 2 of 4
Class Inheritance | Godot GDScript Tutorial | Ep 17 | Godot Tutorials 5/8/23 4:44 PM

# Animal.gd
extends Node2D # Inherits from the Global Node2D class

class_name Animal

var health: int = 100

func getHealth() -> int:


return health

# Horse.gd
extends Animal

# Horse class has the health variable and the getHealth() method

class_name Horse

func newlyCreatedFunction() -> void:


print(getHealth()) # 100

Overriding a Superclass Function


You can override a superclass function by merely writing out the function in the subclass.

# Animal.gd
extends Node2D # Inherits from the Global Node2D class

class_name Animal

func attack() -> void:


print("Animal Attacks")

# Horse.gd
extends Animal

# Horse class has the health variable and the getHealth() method

class_name Horse

func attack() -> void:


print("Horse Attacks") # Override function attack()

Why is using inheritance necessary?


Inheritance allows for cleaner code, and it makes it easier for us to define classes.

Basically, you use inheritance when you want to reuse a class.

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-17 Page 3 of 4
Class Inheritance | Godot GDScript Tutorial | Ep 17 | Godot Tutorials 5/8/23 4:44 PM

Creative Common License

Class Inheritance | Godot GDScript Tutorial | Ep 17 video & article by Godot Tutorials is licensed under a Creative Commons Attribution-
ShareAlike 4.0 International License .

Spread the love, share this if it helped you!

 SHARE ON TWITTER  SHARE ON FACEBOOK  SHARE ON REDDIT  SHARE WITH EMAIL

Subscribe to my Newsletter
Join our newsletter and get news in your inbox! We hate spam too, you
won't get any from me :)

 Email... SUBSCRIBE

GODOT TUTORIALS
WEBSITE RESOURCES LEGAL OTHER

Courses Godot Privacy Policy Contact Me

Trello Cookie Policy Website Info

Github Terms & About Me


Conditions
  
Disclaimer

©2021 Godot Tutorials | Website Powered by Hugo Framework

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-17 Page 4 of 4

You might also like