+4 votes

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.

in Engine by (117 points)

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

Video Tutorial

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!

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

2 Answers

+1 vote

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.

by (5,264 points)
+1 vote

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.

by (7,922 points)

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?

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.

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.