Create a new inherited scene from GDScript?

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

Is it possible to create a new inherited scene from GDScript?
If so, how?

:bust_in_silhouette: Reply From: sash-rc

Inherited scene is a design-time concept used in editor.
In code, you just (pre)load a scene from file, instance and then add it somewhere to the tree.

Thank You for Your answer
Unfortunately in my game I have to make a lot of objects which inherits from common one. Its tedious to make them by hand. I guess i invented algorithm to do it in editor plugin but the problem is how in Godot we create inherited scenes. Unfortunately acording to this reddit post
Reddit - Dive into anything

it’s imposible to do from code. I hoped You know something they don’t.
Best Regards

VanDeiMin | 2022-01-30 22:43

Do you want to just rewrite/extend editor, or you have a final application?
Can you be more specific about your use-case?

Without that it sounds like XY-problem.

sash-rc | 2022-01-31 07:25

I’m trying to make tool (pun not intended) wchich help me adding new objects in the editor based on asset hierarchy I have.
I make game with a lot of cars. I have basic template from which every car type inherits.
Then I have specific car that inherits its base type.
All I want to do, is to maximally automate the process of adding new car and corresponding resources. It make process faster and more mistakeproof.
Now beacuse i cant make whole process automatic (inheritance must be done from editor, and i dont know C++ to make engine extention), I’m trying to make resources from code and add them to handmade car scenes.
Best Regards

VanDeiMin | 2022-01-31 19:07

Well, if you want to automate creation of derived objects, why do you need it in editor?

I still don’t understand your workflow: surprisingly, I also have a vehicle-themed game with cars, tanks etc. Each vehicle is inherited from a base scene, while customization is defined by exported (and thus editable in inspector) properties and implemented at runtime in a code: like load parameters, specific meshes and materials for parts, etc.

sash-rc | 2022-01-31 22:13

it is very probable that my approach is messy and convoluted. I believe my project structure is a mess too. My backround is artistic and I’m too old to learn programming fast (if its possible by first place). Anyway i’ll try explain what I had in mind.

My game is ‘simple’ 2D runner. I have to add about 1000 vehicles of 200 types. Each vehicle must be unique beacuse only one instance can be in game scene at once. I’ve learned inheritance from KidsCanCode and tried to apply his approach.
I have base car scene. Inside of it I put vehicles base Nodes( Logic, BehaviorTrees,etc.)
Then I have default car type. It inherits from default template. Here I Change collider shape. From cartype default specific cars inherits. They vary by animated_sprite frames resource. So every car in game is different object (Maybe here lies the problem?). I thought it’s possible to automate process of creating animatedSprites in editor and connect them to corresponding scenes.

my goal is to automate as much as I can. E.g. I have store module based on Nodes which is created pure from code and updates automaticly every time if I change list of player cars and reload editor. In this approach only I care is to name player car properly on global list. It elimanated a lot of mistakes I had made without the system and saves me a lot of time.

Now making new car type with testing takes me about half an hour. Multiply it by 200 and You’ll understand why I want to automate as much as possible, especially I real struggle in real game logic. So if I could have system which could make new inherited scenes and reseources based on assets I have I would more than happy. Less time I spend in editor and tweak exported variables from there is better for me.

Still I keep going and some day I will learn C++
Best Regards

VanDeiMin | 2022-01-31 23:29

What exactly do you mean by “automate process of creating animatedSprites”?
Why do you ever need animated sprites? To me - car is a physics and mechanical skeleton/hierarchy of parts. If you have such a varieties of types, wouldn’t it better to rely on physics engine like with bodies and joints? Even parametrized AnimationPlayer/ Tween suits better for this.

I believe my project structure is a mess too.

So it’s better to sort it out early, than put efforts in wrong approach.

Still I keep going and some day I will learn C++

Don’t underestimate complexities and overestimate benefits of this.
I’d recommend to learn general game programming patterns and techniques first.
GDScript will suffice for a this task.

sash-rc | 2022-02-01 07:49

Thank You for Your answers and advices Sir .

When I’m trying to explain how my game works I’ve realised how surprisingly complex it is.
And unfortunately must be.

My game is isometric 2D with in theory simple mechanics. Vehicle is class derived from Rigigbody2D. All physics is done by Godot Engine. AnimatedSprite is used only as visual representation which position is calculated by linear algebra transformations.

So de facto in the game, vehicle is represented by Rididbody2D’s collider but the Player sees visual representation in isometry only.

Maybe my approach to make every car as separate object was a bad idea but It helps me manage them in game.

Anyway. My attemp to automate process of creating AnimatedSprites based on assets finished with success. My plugin creates AnimatedSprites ( and even simple script extending CarTemplate which changes car mass) saves them in target location (e.g. “res://Src/Cars/Resources/SpriteFrames/”) and then attaches them to desired Vehicles scenes.

If making inherited scenes would be possible from code, process of creating vehicles that way would be fully automatic and I would be perfectly happy. But besides of that, it’s big time saver whatsoever.

Thank You again for Your time.
Good Luck

VanDeiMin | 2022-02-01 19:25

Aside from technical details (still unknown to me), there’s a logical controversy in your approach : you want to automate things - what’s normally done in code, and in the same time you need to do this in Editor, which is intended for manual composition of scene tree. It could be I’m missing something important in your explanation - but this how it’s seen from my side.

But scenes are text files after all. So (I think) it’s still possible to assemble scene-file on disk, if you’re accepting risks of future incompatibilities and overall wrong structural approach.

sash-rc | 2022-02-02 09:11

Of course the editor is intended to do things manually. I never negate that and I fully appreciate powerful tools it brings. But all I know about computers is that, they are the best at doing repetitive things and i love to exploit that.

Imagine situation like this. You have car store. Every panel in Your store is composition of nodes (Panels, Sprites, Labels, etc. and the like). Suppose You have to add new car to the game. You must remember to add new panel to the store and adjust all its nessesery data. Of course You can do it manually simple by Ctrl+D and clicking and typing to change information on panels. Every time You want to change something in the store, You have to find it in the nodes structure, then click and type. And beacuse there is a lot of steps to do, it’s time consuming and there is always a chance You’ll make the mistake.

But if You have tool script which is feed by nessesery data, You can use it to update the store for You, whenever You add new car to the game or change something in car data. If You have such mechanism all You care is to update your base data file.

And this is the Idea I have used in my game for creating repetitive nodes in the editor.

When I made that mechanism I realised I could automate process for creating nessesery resources, e.g. SpritesFrames which I have a lot in my game. I made working editor plugin which makes nessesery resources based assets stored in the project, add attaches them to corresponding scenes and saves them to the disk.

But I dreamed about situation in which only thing I must do is to put the name to plugin and decide where to write it. Not only for resources ( which works) but for creating inherited scenes (which is currently impossible).

And thats it.
Best Regards

VanDeiMin | 2022-02-02 18:22

:bust_in_silhouette: Reply From: VanDeiMin

Beacuse creating new inherited scenes from code is currently impossible
I made proposal to add feauture to Godot engine and it has been accepted.

I hope They add it soon.

Can you give a TL;DR as to why you rely so heavily on inheritance that you need 1000-2000 siblings instead of using composition?
There are many different programming paradigms, each with strengths and weaknesses, to me it seems like what you are trying to is not something inheritance is meant for.

zhyrin | 2023-03-16 16:59

Sure.
First I’m very noob programer, my background is an Artist ( illustration, painting, sculpting etc), but life forced me to switch.
Second I work alone as a programer and I don’t know any teachers.
Third I didn’t knew of composition before You mentioned about it.

I wanted to maximally automate process of adding new cars to the game. Now what I must is only to choose path to the assets and click execute. I use the inheritance beacuse I know how to use it in Godot. Now If I want to add new type or change something I can do it blazing fast.
Anyway: Thank You for answer and for showing me new field of knowlegde. Cheers

VanDeiMin | 2023-03-17 00:16

:bust_in_silhouette: Reply From: mrkdji

I managed to find a workaround for this by creating an inherited scene from the editor, then duplicating it from code and saving the duplicate as a new .tscn file.
The duplicate scene maintains inheritance.

tool
extends EditorScript

const INHERITED_SCENE = preload("res://test/Inherited.tscn")

func _run():
	var new_scene = INHERITED_SCENE.duplicate();
	ResourceSaver.save("test/AnotherInherited.tscn", new_scene);
:bust_in_silhouette: Reply From: VanDeiMin

Atfter discussion I started here: Add a method to create a new inherited scene from code · Issue #3907 · godotengine/godot-proposals · GitHub, I’m using this solution:

func create_inherited_scene(_inherits: PackedScene, _root_name: String = "") -> PackedScene:
    if(_root_name == ""):
        _root_name = _inherits._bundled["names"][0];
    var scene := PackedScene.new();
    scene._bundled = {"base_scene": 0, "conn_count": 0, "conns": [], "editable_instances": [], 
			"names": [_root_name], "node_count": 1, "node_paths": [], 
			"nodes": [-1, -1, 2147483647, 0, -1, 0, 0], 
			"variants": [_inherits], "version": 2};
return scene;

If I want to my new inherited scene had new name I pass it in _root_name argument.