Can you inherit from a script?

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

I have multiple nodes, which implement and use identical methods.

So I would like to write them once in one script, and let all nodes, who need them “inherit?” from the script.

Is this possible? Or is there another solution for my problem?

:bust_in_silhouette: Reply From: DriNeo

That’s possible. Replace the node inheritance by your script.

extends "res://"

Thats great! But will I then be able to call functions like get_node(“MyNode”), get_pos() etc.? I mean I thought the extends Node2D part is for accessing these basic functions?

Yugo | 2016-03-31 19:12

In my example if “” extends from Node2D you have access to all Node2D functions in this script and also the functions you created in “”.
In the editor the “Classes” button shows the inheritance tree.

DriNeo | 2016-03-31 20:20

Thank you so much DriNeo!! :slight_smile:

Yugo | 2016-03-31 21:19

what happens if you override the _ready() or _init() functions. Is there a way to call the super class’s function? Something like:

func _ready():

edit: never mind! I found the answer! just call ._ready()to call the superclass’ method


func _ready():

batmanasb | 2016-04-03 07:52

@batmanasb you don’t have to call it (it would be called by default). Though, this might change in 3.0…

Bojidar Marinov | 2016-10-17 13:43