Can you inherit from scenes to share common logic and properties?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mydoghasworms
:warning: Old Version Published before Godot 3 was released.

Is it possible, in Godot, to create a “base” or abstract scene, e.g. “enemy” from which different scenes can be inherited?

The child scenes could just have different attributes, e.g. “speed”, “point value” and a different sprite, but the logic of movement, exploding etc. should be shared. The children might want to implement additional behaviour like shooting, or whatever.

I hope this can be of use to you and other people that may want to learn more about inheritance. :slight_smile:

Video Tutorial

Tybobobo | 2016-11-15 16:40

Hey, I only saw this now. Eleven seconds into the video I realised that you are answering this very question. Thank you so much, that is so cool!

mydoghasworms | 2016-12-09 14:06

Could I suggest that you actually supply this as an answer to the question here?

mydoghasworms | 2016-12-15 12:27

:bust_in_silhouette: Reply From: avencherus

Well not in any direct manner from gdscript, but you can instance a scene in the editor’s scene tree, and then add child nodes to it that can be structured to be effected by their parent.

:bust_in_silhouette: Reply From: eons

You can make scene inheritance but could be tricky if you don’t plan well your design, also it will use the main script and you will have to make corrections to the script or change the script on inherited scenes.

To start with it, I can say may be better to do just script inheritance (extends "base.gd") on similar scenes with another name, be careful with the implicit calls to ready and other methods.

ps: There’s a plugin to work with scene and script inheritance you can try on the asset library.

So what would your script “base.gd” in your example inherit from? (I’m assuming GDScript doesn’t do multiple inheritance).

The way I am currently structuring my scenes for a 2D side-scroller is to have Area2D as the root, with a sprite and collision and other things as children. Would the base class inherit from Area2D in this example?

mydoghasworms | 2016-11-14 05:40

Yes, the base should be Area2D (or CollisionObject2D if you plan to have areas and bodies).
Example (may contain syntax errors):

base-enemy.gd

extends Area2D

export(PackedScene) var shot

var speed = 0.0
var mov_direction = 0

func _ready(): #always run this when enter the tree
 set_fixed_process(true)
 init_vars()

func init_vars(): #dummy
 pass 

func shot(): #common shot
 do_shot_stuff()

sub-enemy.gd

extends "base-enemy.gd"

#there's a implicit call for parent ready on Godot 2.x

func init_vars(): #super ready uses this override
 speed = 100

func _fixed_process(delta):
 process_stuff()

This is not only useful for this kind of things, I’m experimenting with a FSM where each state is on a different script and switch scripts with a common base for different behavior.

eons | 2016-11-15 01:31