Is inheritance of scenes available?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robotex

Imagine I have a scene Soldier which has Sprite, collision shape and sensory area (area2d with another collision shape)

And I want to create a different soldiers with different abilities that need additional nodes in their scenes. For i.e. mortar soldier needs area2d for detect targets that available for attack and smaller area2d for detect targets that are too close for attack.

I can inherite the new Script from Soldier script but I still need to create the new scene with the same nodes + new nodes of this unit.

Is it possible to use inheritance in scenes to reuse the nodes of parent’s scene?

:bust_in_silhouette: Reply From: Andrew Wilkes

In Godot, you instance a saved scene as a node into a new scene to inherit it.

But will the GDScript that inherited from parent’s scene GDScript see all parent’s components on the same pathes?

Robotex | 2021-11-12 13:51

This doesn’t allow modification of each soldier individually since they still share all the same resources.

t8 | 2021-11-14 19:16

I don’t understand :frowning:

Robotex | 2021-11-15 08:47

:bust_in_silhouette: Reply From: t8

I suggest you look up the Godot documentation for scenes and inheritance. It should have a lot of the information you’re looking for! Here are some pages to get you started:

Inheritance of Scenes, per se, is not available, but you can do a lot with what you already have.

It sounds like you should set up the Soldier scene, then make multiple instances of it. In your Soldier script, create a method to change the type of soldier so you can specify what each instance of the soldier should be. For your mortar soldier, have that method create the two new Area2D nodes then add them as children of the soldier. It should also set up the appropriate signal connections for the new Area2D nodes.

Let me know if this helps!

:bust_in_silhouette: Reply From: michaelgundlach

The accepted answer is wrong. Use Scene → New Inherited Scene. Whatever nodes are in the Soldier scene will also appear in your inherited scene, and they’ll stay in sync.

I found this by accident, as I don’t see it anywhere in the docs.

Note that you need to save the super class for the changes to propagate to the derived class.

paulmis | 2023-01-03 09:09

:bust_in_silhouette: Reply From: JG

You can achieve inheritance of scenes in Godot 4 by doing the following (I recommend that each scene resides in its own folder) :

  1. create a base scene. Let’s call it base_scene. It usually is very basic. Attach a script to it even if empty. The script can comment on what this base_scene is for.

  2. create a new inherited scene from base_scene. Let’s call it better_scene. Godot will share the base_scene’s script with better_scene. You probably do not want that. Remove the linked script from better_scene.

  3. create a new script for better_scene and at the top of the script, make sure it extends the base_scene’s script. All the functionality that you first built into the base_scene’s script will be available to better_scene.

  4. repeat the process for a cool_scene that inherits from better_scene. The cool_scene script extends the better_scene script, meaning it now has all the functionality of both the base_scene script and the better_scene script.

In the scripts, you can override functions from parent scenes but you can also call a function from the parent class using the keyword super.

For example, you could have a setup() function in base_scene that does nothing. in better_scene you override setup(), call the base setup() and add to it. Just like this:

func setup():
super.setup() # calls the parent setup()
do something here…

With this design you can achieve pretty good practical inheritance. It makes it very easy to go back in the line of inheritance and make changes that will flow through.

Here is a folder structure for a practical example. Each folder contains a tscn scene file and a gd script:

— scenes

The base character has everything common to all characters in the game (for example health) then both friendly and enemy are inherited from base. Finally, mage is inherited from friendly while orc is derived from enemy.

Check GDScript reference — Godot Engine (latest) documentation in English for more