Class Inheritance | Godot GDScript Tutorial | Ep 17

Godot Tutorials
22 Apr 202015:15

Summary

TLDRThis tutorial episode explores class inheritance in Godot's GDScript, explaining the concept of deriving classes to share attributes and methods. It clarifies that multiple inheritance is not supported in GDScript. The script demonstrates how to use the 'extends' keyword, the roles of base and derived classes, and the importance of constructors for variable assignment and method overriding. It showcases practical examples, including creating animal subclasses with unique behaviors, and emphasizes inheritance's role in promoting cleaner, more efficient code reuse.

Takeaways

  • 📚 Class inheritance in Godot allows for creating a hierarchy of classes that share attributes and methods.
  • ❌ Godot does not support multiple inheritance, meaning a class can only inherit from one other class.
  • 🔑 To inherit from a class, use the 'extends' keyword followed by the class name or a file path enclosed in double quotes.
  • 👉 The terms 'base class' and 'superclass' are synonymous, referring to the class whose properties are inherited.
  • 🐴 The 'derived class' or 'subclass' is the class that inherits properties from the base class, such as the 'horse' class inheriting from the 'animal' class.
  • 🚫 A subclass cannot have a member variable with the same name as one in the superclass; use the constructor to assign or change values from the superclass.
  • 💡 The power of inheritance lies in reusing code and making small tweaks to subclasses, reducing the amount of code needed for each class.
  • 🔄 Inheritance enables overriding functions in subclasses, allowing specific implementations of methods provided by the superclass.
  • 🌐 Godot supports multiple layers of inheritance, where a class can inherit from another, which in turn inherits from another, and so on.
  • 👶 Using constructors and class objects with inheritance can save time and lead to cleaner code, especially when deriving many classes from a base class.
  • 🛠️ The video script provides practical examples, such as the 'animal', 'horse', and 'frog' classes, to illustrate inheritance concepts and their application.

Q & A

  • What is class inheritance in the context of programming?

    -Class inheritance is a mechanism where a new class, known as the derived or subclass, is based on an existing class, known as the base or superclass. The subclass inherits attributes and methods from the superclass, creating a hierarchy of classes that share a set of functionalities.

  • Is multiple inheritance allowed in Godot's GDScript?

    -No, in Godot's GDScript, multiple inheritance is not allowed. A class can only inherit from one other class or file.

  • How do you implement inheritance in GDScript?

    -In GDScript, inheritance is implemented using the 'extends' keyword followed by the class name or a file path wrapped in double quotes, which turns it into a string.

  • What is the difference between a base class and a derived class?

    -A base class, also known as a superclass, is the class from which properties are inherited. A derived class, also known as a subclass, is the class that inherits properties from the base class.

  • Why can't a subclass have a member variable with the same name as one in the superclass?

    -A subclass cannot have a member variable with the same name as the superclass to avoid conflicts and ensure that the subclass does not unintentionally override or shadow the superclass's variable.

  • How can a subclass change the value of a member variable inherited from the superclass?

    -A subclass can change the value of an inherited member variable by using the constructor method to assign or change the value of the variable from the subclass to the superclass.

  • What is the purpose of using inheritance in programming?

    -Inheritance is used to promote code reuse, reduce redundancy, and create a hierarchical structure of classes. It allows for cleaner code and easier definition of classes with similar functionalities but with additional or modified behaviors.

  • Can functions be overridden in GDScript?

    -Yes, in GDScript, you can override functions from the superclass in the subclass to provide a specific implementation of a method that is already provided by the superclass.

  • What does it mean to override a function in a subclass?

    -Overriding a function in a subclass means providing a new implementation for a method that already exists in the superclass, allowing the subclass to have its own specific behavior for that method.

  • Can you have multiple layers of inheritance in GDScript?

    -Yes, GDScript allows for multiple layers of inheritance. A class can inherit from another class, which in turn can inherit from another, creating a chain of inheritance where a subclass can access methods and variables from all its ancestor classes.

  • What are some tips for using inheritance effectively in GDScript?

    -Effective use of inheritance in GDScript includes using it for cleaner code, reusing code from a base class, and making small tweaks for individual subclasses. It's also important to use constructors and class objects to pass down values and change them as needed.

Outlines

00:00

📚 Introduction to Class Inheritance in Godot

This paragraph introduces the concept of class inheritance in Godot, a game engine's scripting language. It explains that inheritance allows for the creation of a hierarchy of classes that share attributes and methods. The tutorial clarifies that multiple inheritance is not supported in Godot's GDScript. The paragraph also defines base and derived classes, also known as superclass and subclass, and their roles in inheritance. An example is provided with an 'Animal' class and a 'Horse' class, illustrating how the 'Horse' class inherits properties from the 'Animal' superclass. The importance of constructors in modifying inherited properties is highlighted, and the limitations regarding member variables with the same name are discussed.

05:03

🐎 Utilizing Inheritance for Code Reusability

The second paragraph delves into the practical applications of inheritance, emphasizing its role in promoting cleaner and more efficient code. It uses the 'Animal' base class as an example to demonstrate how different animal types can inherit from it, thus avoiding repetitive code. The paragraph explains the concept of overriding functions in subclasses to provide specific implementations, as opposed to using the superclass's method. It also touches on the possibility of multiple layers of inheritance, where a class can inherit from another class, which in turn inherits from yet another class, thereby accessing a broader range of methods and variables. An example of a 'Node' class, an 'Animal' subclass, and a 'Horse' subclass illustrates this concept.

10:04

🐸 Inheritance Best Practices and Code Efficiency

This paragraph focuses on best practices for using inheritance in coding, particularly in the context of Godot's GDScript. It underscores the efficiency gains from using inheritance when creating multiple classes with similar codebases. The 'Frog' and 'Horse' class files are used as examples to show how inheritance reduces the amount of code needed. The importance of constructors in passing values to base classes is reiterated, and the paragraph provides a step-by-step guide on how to implement inheritance in practical scenarios, including how to handle member variables and function overriding.

15:05

🔚 Wrapping Up the Inheritance Tutorial

The final paragraph serves as a conclusion to the tutorial on class inheritance. It summarizes the key points discussed in the previous paragraphs and encourages viewers to download a GitHub file provided in the description to practice inheritance. The tutorial aims to improve understanding and application of inheritance in programming projects. The host expresses gratitude for the viewers' engagement and looks forward to their participation in the next episode.

Mindmap

Keywords

💡Class Inheritance

Class inheritance is a fundamental concept in object-oriented programming that allows a new class, known as a subclass or derived class, to inherit attributes and methods from an existing class, referred to as a superclass or base class. In the context of the video, class inheritance is used to create a hierarchy of classes in Godot that share common characteristics, streamlining code reuse and maintenance.

💡GDScript

GDScript is the scripting language used in the Godot game engine for creating game logic and interactions. It is designed to be simple and easy to learn, with a syntax similar to Python. The video script discusses how GDScript handles class inheritance, emphasizing that while multiple inheritance is not allowed, GDScript provides the 'extends' keyword to facilitate inheritance.

💡Base Class

A base class, also known as a superclass, is the class from which other classes inherit properties. It typically contains general attributes and methods that can be shared by derived classes. In the script, the 'Animal' class serves as a base class with a function 'eat' and a member variable 'food', which are inherited by the 'Horse' class.

💡Derived Class

A derived class, or subclass, is a class that inherits properties from a base class. It can use the member variables and methods of the base class and can also introduce new or override existing functionality. In the video, 'Horse' and 'Frog' are derived classes that inherit from the 'Animal' base class, with 'Horse' overriding the 'eat' method.

💡Constructor Method

The constructor method is a special type of method in a class that is automatically called when a new object of that class is created. It is used to initialize the object's state. In the script, the constructor method is used to assign values to member variables, such as 'food', and to pass values to the base class's constructor.

💡Member Variables

Member variables are variables that are defined within a class and are associated with each instance of the class. They represent the state of the object. In the video, 'food' is a member variable of the 'Animal' class, and it is inherited by the 'Horse' and 'Frog' classes, with the 'Horse' class being unable to declare a member variable with the same name without causing an error.

💡Override

To override a method means to provide a new implementation of a method that is already defined in a base class. In GDScript, subclasses can override functions from their superclasses to provide specific behavior. The script gives an example where the 'Horse' class overrides the 'eat' function from the 'Animal' class to print a customized message.

💡Multiple Layers of Inheritance

Multiple layers of inheritance refer to the ability of a class to inherit from another class, which in turn may inherit from yet another class, creating a chain or hierarchy of classes. The script explains that in GDScript, a class like 'Horse' can inherit from 'Animal', which itself inherits from 'Node', thus gaining access to methods and variables from both classes.

💡Class Objects

Class objects are instances of classes created during runtime. They represent individual entities with their own set of member variables and methods. In the script, creating a 'Horse' class object involves using the 'new' keyword and passing parameters to the constructor method, which then assigns values like 'grass' to the 'food' variable.

💡Code Reusability

Code reusability is the practice of using existing code in new programs or applications, which helps in reducing development time and avoiding redundancy. The video emphasizes the importance of inheritance in promoting code reusability, as it allows creating a base class with common functionality that can be shared by multiple subclasses.

Highlights

Introduction to class inheritance in Godot's GDScript, emphasizing its role in creating a hierarchy of classes that share attributes and methods.

Explanation of the 'extends' keyword used to inherit from another class, with the option to use a class name or a file path.

Clarification that multiple inheritance is not allowed in GDScript.

Definition of base (super) class and derived (sub) class in the context of inheritance.

Demonstration of how a subclass can use member variables and methods from the base class, with an example of the 'Animal' and 'Horse' classes.

Important note on avoiding variable name conflicts between a subclass and its superclass.

Use of the constructor method to assign or change the value of a member variable inherited from the superclass.

The power of inheritance to allow multiple subclasses to inherit from a single base class, as illustrated with the 'Horse' and 'Frog' classes.

How to pass values to the base class from the subclass using the constructor method.

The ability to add variation to objects by passing parameters in the subclass's initialize function.

Inheritance's role in promoting cleaner code and reusing code from a base class to avoid redundancy.

The concept of overriding functions in GDScript, with an example of the 'eat' function being overridden in the 'Horse' class.

Explanation of multiple layers of inheritance in GDScript, allowing a class to inherit from another class that itself inherits from another.

Practical tips on using inheritance effectively with class constructors and objects, especially when deriving many classes from a base class.

Code examples provided to illustrate the concepts discussed, including the 'Animal', 'Horse', and 'Frog' classes with inheritance and overriding.

Final thoughts on the importance of inheritance in simplifying code and allowing for specific implementations in subclasses.

Invitation to download the GitHub file for further practice with inheritance in GDScript.

Transcripts

play00:01

hello and welcome to another episode in

play00:04

the GT script fundamental tutorial

play00:06

series in this episode we will be

play00:08

talking about class inheritance now to

play00:11

go over the definition again a class

play00:13

inheritance is a mechanism where you can

play00:16

derive a class from another class for a

play00:18

hierarchy of classes that share a set of

play00:21

attributes and methods in Godot you can

play00:23

inherit from the following a global

play00:26

class another class file and an inner

play00:29

class inside another class file one

play00:31

thing to keep in mind is that in GT

play00:34

script multiple inheritance is not

play00:36

allowed now to inherit from a class

play00:39

simply use the extends keyword followed

play00:42

by a class name or a file path now keep

play00:45

in mind when you decide to use a file

play00:47

path you need to wrap it in double

play00:49

quotations essentially turning it into a

play00:51

string now when you do inheritance you

play00:54

deal with two things a base class and a

play00:57

derived class a base class can also be

play00:59

referred to as a superclass and a

play01:01

derived class can also be referred to as

play01:04

a subclass now a subclass is the class

play01:07

that inherits properties from another

play01:10

class the other class which is referred

play01:12

to as the superclass a super class is

play01:15

the class whose properties are inherited

play01:18

by subclass the word property is just a

play01:21

broad concept basically it encompasses

play01:24

attributes such as class members and

play01:26

methods and relationships to other

play01:28

classes such as inherited classes now

play01:31

when you inherit a derived class has the

play01:34

ability to use member variables and

play01:37

methods from the base class let's go

play01:39

ahead and take a look at an example as

play01:41

you can see here we have our classes an

play01:43

animal file or rather an animal class

play01:47

and we have a horse class now the horse

play01:50

class is the derived class and the

play01:52

animal class is the base class so animal

play01:55

is the superclass and horse is the

play01:57

subclass our base class or rather the

play02:00

superclass has a function called eat

play02:02

which just prints out a statement to

play02:04

console and it has a member variable

play02:07

food one thing to keep in mind is that

play02:10

the horse class cannot have a member

play02:13

variable with the same

play02:15

named as food now if you try in your

play02:17

horse class to declare a member variable

play02:20

called food you will throw an error and

play02:22

the error you will receive is a variable

play02:25

already exist error to get around that

play02:28

you use the constructor method and from

play02:32

the horse class you can assign or change

play02:34

the value of a member variable from your

play02:37

superclass in this case the animal class

play02:40

so as you can see here in our

play02:41

constructor method in horse class we are

play02:44

assigning the food with the value grass

play02:47

now what this does is we change the

play02:49

value from our derived class to the

play02:51

superclass and now if we were to access

play02:54

the method eat what we will get is

play02:56

instead of saying eats something what we

play02:59

will print out instead is eat grass now

play03:02

normally for something as simple as this

play03:04

you wouldn't go ahead and use

play03:06

inheritance you would probably use class

play03:08

objects basically class instantiation

play03:11

however the real power of inheritance

play03:13

comes from the fact that you can have

play03:15

multiple classes as subclass and they

play03:18

can all inherit from a single base class

play03:21

so for example we have a horse class and

play03:23

through the constructor method again we

play03:26

are assigning food to the string value

play03:29

grass and in our new class file called

play03:32

frog we're passing a value bugs into our

play03:35

food variable again through the

play03:38

construct method we'll take a better

play03:39

look at that through code examples later

play03:41

now if you want to pass a value to your

play03:44

base class from your subclass all you

play03:46

have to do is simply call the

play03:48

initialized function in your subclass so

play03:51

in this example we're in our horse class

play03:54

and after the initialized function we

play03:57

use the dot notation

play03:59

followed by a value inside the

play04:02

parentheses we want to pass to our

play04:04

superclass in this case every time we

play04:07

create a horse class object what we're

play04:10

going to do is initialize our horse

play04:13

class with the grass literal value and

play04:16

pass that to our food variable if we

play04:19

want to actually use this class for

play04:22

example in a class object all you have

play04:23

to do is simply create your standard

play04:25

variable followed by the class

play04:28

name in this case horse followed by the

play04:31

new method now when we create a class

play04:32

object like this we're going to assign

play04:35

the grass variable and that's no fun

play04:37

what if you want to add variation to to

play04:40

your objects well to do that instead of

play04:43

passing a literal value inside the

play04:45

parentheses what you'll do is in your

play04:48

subclass in the initialize function what

play04:51

you're going to do is receive a

play04:52

parameter or rather use a parameter and

play04:55

then you take that parameter you just

play04:57

created in this case our parameter name

play04:59

is value and we pass that value into the

play05:02

parentheses after it so in this case

play05:05

anishka sees value dot parentheses

play05:08

values and now when we create a class

play05:11

object as you can see here when we call

play05:13

the new method and we pass let's say a

play05:15

literal value grass what we do is we end

play05:19

up in the constructor method and now

play05:21

grass is our value and we simply pass

play05:24

that value to our superclass and so now

play05:27

our superclass has the literal value

play05:29

grass and can do whatever it needs to do

play05:32

in the case of our simple example the

play05:35

base class will just assign the food

play05:38

variable the literal string value grass

play05:41

now why exactly is inheritance important

play05:44

well for one thing inheritance allows

play05:46

for cleaner code it makes it easier for

play05:49

you to define a class especially when we

play05:52

want to make additional tweaks to a

play05:54

class now basically you use inheritance

play05:56

when you want to reuse code from a class

play05:58

take for example our base class animal

play06:01

what if your game needs 20 different

play06:05

types of animals instead of writing

play06:06

similar code in each class what you can

play06:09

do is create one base class one source

play06:12

of truth and from there your animals can

play06:15

derive from it can it or rather they can

play06:17

inherit from that base class and then

play06:20

you can just make small tweaks so

play06:22

instead of writing for example 100 lines

play06:24

of code in each class you can cut that

play06:26

down to maybe 10 or 20 depending on what

play06:29

you're adding to each individual class

play06:31

or rather subclass the real power from

play06:34

inheritance is the ability to override

play06:37

now in Gd script you are only allowed to

play06:40

override functions

play06:41

you cannot override variables now

play06:43

overriding allows a subclass to provide

play06:46

a specific implementation of a method

play06:48

that is already provided by its

play06:50

superclass take this for example we have

play06:54

our animal class with the eat function

play06:57

and we have our horse class if we use

play06:59

the eat function what we're going to do

play07:01

is call the eat function inside our base

play07:04

class in this case we're gonna print out

play07:06

eats food but what if you want something

play07:08

else what if you want something

play07:09

different well in your horse class file

play07:11

what you do is you write it out in the

play07:14

code in this case your eat function and

play07:17

then you just write whatever you want in

play07:19

this case we're going to print out loves

play07:21

to eat hit to the console now every time

play07:24

you call the eat function instead of

play07:26

calling the eat function in your base

play07:28

class what you do is you end up calling

play07:30

the eat function in your current class

play07:32

in this case the horse class file now an

play07:35

important thing to note is that in Gd

play07:38

script you can have multiple layers of

play07:39

inheritance I mean by definition

play07:41

inheritance implies that you can you can

play07:44

have layers so let's take a look at an

play07:46

example let's say your superclass is the

play07:49

node class and then your animal class

play07:51

inherits from the node class and your

play07:53

horse class inherits from the animal

play07:55

class well your horse class will now

play07:57

have access to not only the virtual

play08:00

methods in your node class but it will

play08:03

also have access to methods and

play08:05

variables in the animal class let's go

play08:07

ahead and take a look at this example as

play08:10

you can see here we have the no class or

play08:11

base class we have the animal class the

play08:14

subclass and we have the horse class

play08:17

which is a subclass of the animal class

play08:19

which is the subclass of the node base

play08:21

class now in the animal class we extend

play08:24

our node so now we have access to the

play08:26

virtual methods node class provides and

play08:29

we've also declared a neat function now

play08:31

let's say in our horse and frog class we

play08:34

extend animal well now we have access to

play08:36

the virtual method ready provided from

play08:39

the node class and we can also call the

play08:41

eat function from the animal now some

play08:43

tips about inheritance inheritance is a

play08:46

powerhouse it's very powerful when you

play08:49

use it with class constructors and class

play08:51

objects now again it saves you a lot of

play08:54

time when writing

play08:56

class codes for example an animal class

play08:59

especially if you're deriving a lot of

play09:02

classes from your base class now again

play09:04

you use inheritance when you want to

play09:06

have lots of classes with similar code

play09:08

but additional tweaks to their behaviors

play09:10

either new or inherited methods now

play09:13

let's go ahead and take a look at some

play09:15

code so as you can see here we have our

play09:17

class animal you can see we're using the

play09:20

class name animal we have our member

play09:22

variable food only takes in string

play09:24

values and we have our initialize

play09:27

constructor method in this case we're

play09:31

allowing you to pass values down from

play09:33

your subclasses and if you choose not to

play09:35

pass any value down from your subclass

play09:37

we are also defaulting a literal string

play09:40

value something as you can see here

play09:42

through this code that is exactly what

play09:44

we're doing passing a value to a

play09:46

parameter use that parameter value

play09:48

assign it to our food and our food value

play09:51

is a member variable that only accepts

play09:53

string and we have one function called

play09:55

eat in this case we'll print out to the

play09:58

console eating something now I went

play10:01

ahead and created a second class file

play10:04

called frog and as you can see here we

play10:07

are extending from the animal class

play10:09

we're inheriting from the animal class

play10:11

we went ahead and gave a name to this

play10:14

class file to the Godot editor in this

play10:16

case we've named it frog and notice how

play10:18

all we have in this class file is the

play10:21

initialize function the initialize

play10:23

constructor method and we are passing a

play10:27

value down to our base class in this

play10:30

case the literal value bugs as you can

play10:33

see here take a notice this is only

play10:35

seven lines of code versus T 12 now this

play10:39

doesn't seem a lot because these are

play10:41

just example files but in the real or

play10:44

rather in your production code your

play10:46

scripts aren't gonna be only ten or

play10:48

twelve lines of code they may be 50 100

play10:51

maybe even 200 and instead of writing

play10:53

200 lines of similar code in our frog

play10:56

class we can just basically bring it

play10:59

down to just the essentials in our

play11:00

constructor method depending on how

play11:03

complex we want to write similar code or

play11:06

depending on how complex we want our

play11:08

subclasses to be in this case we're just

play11:11

passing through the initial method now

play11:13

let's go ahead and take a look at the

play11:14

horse class file in this class file we

play11:17

are extending from the animal class

play11:19

we're inheriting from the animal class

play11:20

we're giving this a class name that will

play11:23

show up in the Godot editor called horse

play11:25

but notice how we're adding an

play11:28

additional member variable not only do

play11:30

we want the food variable that we're

play11:33

going to inherit from the animal class

play11:35

we also want our own variable that we

play11:38

can use for this specific class now

play11:40

again one thing to keep in mind if we

play11:42

try to declare a variable with the same

play11:44

name that we inherit from our base class

play11:47

in this case the variable name called

play11:49

food we're gonna throw an error it's

play11:51

going to tell you this variable already

play11:53

exists moving on we have our initialized

play11:56

function and as you notice here not only

play11:59

do we take in a value for a name that we

play12:03

are going to assign to the horse class

play12:05

member variable called first name but we

play12:10

also want to take in a value for the

play12:12

food variable we call it food and we

play12:15

want to pass it down to our base class

play12:17

so our base class can use it in that

play12:19

case the base class is going to print

play12:22

out eats orange if we don't pass a value

play12:25

when we create a class object however

play12:26

check this out we have our function

play12:31

called eat what we're doing through this

play12:34

is we're overriding our base classes

play12:36

function so our base class has a

play12:38

function called eat and if we didn't

play12:40

declare this function we would use that

play12:42

class's method however because we're

play12:44

declaring a function in this class file

play12:46

when we call the function eat and for

play12:48

example class object what we're going to

play12:50

do is call this function and this

play12:52

function is going to print out a name

play12:54

followed by really wants to eat that and

play12:57

then the value food and on top of that

play13:01

we've also added an additional function

play13:03

called sleep where we print out the name

play13:05

followed by the words is sleeping now

play13:08

lastly putting all of this together we

play13:10

have our class object horse object and

play13:13

we're going to create a new horse in

play13:14

this case we're going to pass to the

play13:17

class object a name darth sidious

play13:20

and we're going to pass in

play13:22

value for the food variable called Apple

play13:24

and of course for a frog object we're

play13:26

just going to create a simple frog

play13:28

object now as you can see here through

play13:32

our class object if we call the eat

play13:34

function we're gonna print out darth

play13:37

sidious really wants to eat that Apple

play13:39

and when we call the sleep function

play13:42

we're gonna print out darth sidious is

play13:44

sleeping and when we call the frog

play13:48

objects eat function the Frog object

play13:50

doesn't have any function so therefore

play13:52

it's gonna use the base classes eat

play13:54

function and it's simply gonna put out

play13:56

or print out eating bugs so again for a

play14:00

more complicated horse class when you

play14:02

want to pass down values and change

play14:04

values use the constructor method if you

play14:06

want to pass down values from classes

play14:09

you've inherited you got to use the dot

play14:11

notation followed by the value you want

play14:13

to pass make sure that data types are

play14:16

acceptable and lastly if you want to

play14:18

change member variables through

play14:20

instantiation use the initial

play14:22

construction and then you just simply

play14:24

pass your variable or you simply pass

play14:27

your parameter value to your member

play14:29

variable one thing to note is that you

play14:31

can actually access your member

play14:34

variables directly from the code so even

play14:37

though we have the eat function we can

play14:39

also call the food member variable we

play14:41

can of course go ahead and assign it a

play14:44

different value well that's all I have

play14:46

for you in this episode go ahead and

play14:48

download the github file I put a link

play14:50

down in the description below when you

play14:53

download that file go ahead and play

play14:54

around with inheritance the more you

play14:56

practice the better you'll be at

play14:58

understanding how inheritance works and

play15:00

how you can use it and apply it to your

play15:02

programming in your projects thank you

play15:05

for joining me in this episode I look

play15:07

forward to seeing you in the next

Rate This

5.0 / 5 (0 votes)

Related Tags
Class InheritanceGodot ScriptingTutorial SeriesCode ReusabilityCustomizationOOP PrinciplesGDScriptSuperclassSubclassConstructor MethodInheritance Layers