Godot Inheritance of Scenes and Scripts
Godot Inheritance 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.
Overview of Godot Inheritance
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 planed 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 Godot 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:
If you wish, you can always remove the inheritance from a given scene by right-clicking on the root node and click 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:
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. And, for a little more info on exporting variables, check my post on tips and best practice.
For more information on inheritance, you can find a little bit on inheritance here in the official documentation. Also, if you did find this information useful, let me know in the comments below. If you liked my post, you may want to check out this post on themes in Godot. Or this one about how to make custom key bindings in Godot.
To learn more about the Godot Engine I can recommend Godot Engine Game Development Projects by Chris Bradfield. If you are interested, you can follow the affiliate link to also support this website.