Inheritance in Godot 3
Inheritance in Godot 3 can be a powerful tool that helps you build, change and maintain complex projects. If you have a growing number of similar scenes (for example a lot of enemies, that share some key features) it can be an ever-growing task to change every single one of them, if some of those key features changes. Here, inheritance can help you save a lot of time and nerves.
Dear reader, please consider supporting my game ❤
Overview of Inheritance in Godot 3
If you are familiar with Object-Oriented Programming (OOP), you probably already know about the concept of inheritance. The idea is, that you have a parent and children, which take some features of that parent. And if you change something inside that parent, all its children are going to change as well. In addition, a child can also have unique features, that are not shared with its parent.
The main disadvantage of the concept of inheritance is, that the structure of your project should be planned in advance. Otherwise, it may result in a situation, where managing all different scenes and inherited scenes take up more time than just changing all different enemies manually would.
Scenes and Inheritance in Godot 3
Add Inheritance
You can create a new inherited scene from any scene you made. So, to do that, just go to Scene > New Inherited Scene… and chose the scene you want as the parent scene. Then save this scene with a new name.
Change something in an inherited scene
You can change features or values in an inherited scene at any time. Also, if you want to restore the value to the default one from the parent scene, you can click on the little round arrow next to the changed value:
Remove Inheritance
If you wish, you can always remove the inheritance from a given scene by right-clicking on the root node and clicking Clear Inheritance.
Scripts and Inheritance
Not only scenes can inherit and be inherited, but also scripts. So, to make a script, that inherits from another script, just call extends at the top of the child script: extends parent_script.gd
.
Child scripts can then call functions from the parent script and vice versa. Also keep in mind, that you easily can export different variables, so that they are visible in the editor. So, this makes it easier to work with inheritance.