+1 vote

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?

in Engine by (274 points)

4 Answers

+3 votes
Best answer

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.

by (52 points)
selected by

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

–4 votes

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

by (202 points)

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

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

I don't understand :(

–2 votes

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!

by (52 points)
0 votes

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 basescene. It usually is very basic. Attach a script to it even if empty. The script can comment on what this basescene is for.

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

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

4) repeat the process for a coolscene that inherits from betterscene. The coolscene script extends the betterscene script, meaning it now has all the functionality of both the basescene script and the betterscene 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 basescene that does nothing. in betterscene 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
------characters
----------base
----------friendly
----------enemy
---------mage
---------orc

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 https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#referencing-functions: for more

by (29 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.